Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 将单行脚本转换为单行命令的方法?_Ruby - Fatal编程技术网

Ruby 将单行脚本转换为单行命令的方法?

Ruby 将单行脚本转换为单行命令的方法?,ruby,Ruby,下面是我的“一行”脚本 #!/usr/bin/ruby puts ARGF.read.gsub(/\\caption\{((?:[^{}]+|\{\g<1>\})+)\}/m) { |xx, yy| Regexp.last_match[0].gsub(/([^\\])#/,'\1\\#') } 我得到了双引号 -e:1: premature end of char-class: /([^\])#/ 问题 因此,问题是如何使它在ruby-pe'中工作?使用%q |而不是一行中

下面是我的“一行”脚本

#!/usr/bin/ruby

puts ARGF.read.gsub(/\\caption\{((?:[^{}]+|\{\g<1>\})+)\}/m) { |xx, yy|
  Regexp.last_match[0].gsub(/([^\\])#/,'\1\\#') }
我得到了双引号

-e:1: premature end of char-class: /([^\])#/
问题


因此,问题是如何使它在
ruby-pe'
中工作?

使用
%q |
而不是一行中的单引号,它实际上是相同的,但不会与命令行单引号混淆:

puts ARGF.read.gsub(RE1) { Regexp.last_match[0].gsub(/([^\\])#/,%q|\1\\#|) }

使用
%q | |
而不是在一行中使用单引号,这实际上是相同的,但不会与命令行单引号混淆:

puts ARGF.read.gsub(RE1) { Regexp.last_match[0].gsub(/([^\\])#/,%q|\1\\#|) }

我不知道您想做什么,但是在shell中转义字符串的标准方法如下:

require "shellwords"

`some_command #{Shellwords.escape(some_command_written_in_ruby_string)}`

我不知道您想做什么,但是在shell中转义字符串的标准方法如下:

require "shellwords"

`some_command #{Shellwords.escape(some_command_written_in_ruby_string)}`

它包含单引号,因此当您将其嵌入命令行上的单引号中时,需要转义内部的单引号。或者使用另一种引用机制。我刚刚尝试并更新了OP,但出现了错误。@mudasobwa您是正确的,注释已更新。它包含单引号,因此当您将其嵌入命令行的单引号中时,需要转义内部单引号。或者使用另一种引用机制,我刚刚尝试并更新了OP,但出现了错误。@mudasobwa您是正确的,注释已更新。