Rails 在 ruby 命令中为符号添加反斜杠
Rails adding a backslash to symbols on a ruby command
我正在使用来自 https://github.com/dignoe/graphicsmagick
的 GraphicsMagick 包装器
它有效,但是当我添加任何符号时,ruby 添加一个反斜杠,这会在 运行 命令时导致错误。我怎样才能避免这种情况?
代码:
img.crop('360x504+432+72').resize('125x177!').write("public/#{path}/xs-" + filename)
生成的错误消息:
GraphicsMagick::UnknownOptionError (gm mogrify -crop 360x504\+432\+72 -resize 125x177\! public//media/xs-cccc.JPG failed: gm mogrify: Option '-crop' requires an argument or argument is malformed.
):
我应该一开始就猜到你的问题是Windows。 Windows 总是很有趣。
Ruby 的 Shellwords 模块,the graphicsmagick gem uses, isn't designed to work with Windows (per the docs, it "manipulates strings according to the word parsing rules of the UNIX Bourne shell"—there's a long-standing issue 为其打开)。
假设我无法说服您切换到更适合 Ruby 开发的 OS,我能为您提供的最好方法就是 hack。使用 Module#prepend
更改 Shellwords.escape
的行为以删除某些字符的反斜杠:
require "shellwords"
module UglyShellwordsHack
def escape(*args)
super.gsub(/\([+^])/, '')
end
end
Shellwords.singleton_class.prepend(UglyShellwordsHack)
puts Shellwords.escape("360x504+432+72")
# => 360x504+432+72
当然,就像所有黑客一样,这几乎可以保证在未来的某个时候被破解。
P.S。您可能应该在 graphicsmagic issue 中提及您正在使用 Windows.
我正在使用来自 https://github.com/dignoe/graphicsmagick
的 GraphicsMagick 包装器它有效,但是当我添加任何符号时,ruby 添加一个反斜杠,这会在 运行 命令时导致错误。我怎样才能避免这种情况?
代码:
img.crop('360x504+432+72').resize('125x177!').write("public/#{path}/xs-" + filename)
生成的错误消息:
GraphicsMagick::UnknownOptionError (gm mogrify -crop 360x504\+432\+72 -resize 125x177\! public//media/xs-cccc.JPG failed: gm mogrify: Option '-crop' requires an argument or argument is malformed.
):
我应该一开始就猜到你的问题是Windows。 Windows 总是很有趣。
Ruby 的 Shellwords 模块,the graphicsmagick gem uses, isn't designed to work with Windows (per the docs, it "manipulates strings according to the word parsing rules of the UNIX Bourne shell"—there's a long-standing issue 为其打开)。
假设我无法说服您切换到更适合 Ruby 开发的 OS,我能为您提供的最好方法就是 hack。使用 Module#prepend
更改 Shellwords.escape
的行为以删除某些字符的反斜杠:
require "shellwords"
module UglyShellwordsHack
def escape(*args)
super.gsub(/\([+^])/, '')
end
end
Shellwords.singleton_class.prepend(UglyShellwordsHack)
puts Shellwords.escape("360x504+432+72")
# => 360x504+432+72
当然,就像所有黑客一样,这几乎可以保证在未来的某个时候被破解。
P.S。您可能应该在 graphicsmagic issue 中提及您正在使用 Windows.