简体   繁体   English

无法将块传递给Ruby中的内核子方法

[英]can't pass block to Kernel sub method in Ruby

I'm trying to run a ruby one-liner in bash, and this is what happens: 我正在尝试以bash运行红宝石单线,这是发生的情况:

~$ echo a$'\n'b$'\n'c | ruby -pe 'sub(/./,"x")'
x
x
x
~$ echo a$'\n'b$'\n'c | ruby -pe '$_.sub!(/./){"x"}'
x
x
x
~$ echo a$'\n'b$'\n'c | ruby -pe 'sub(/./){"x"}'
-e:1:in `sub': wrong number of arguments (1 for 1..2) (ArgumentError)
        from -e:1:in `sub'
        from -e:1:in `<main>'

Why doesn't command #3 work exactly like command #2? 为什么命令3不能像命令2一样工作?

(This is ruby 1.9.2. The echo statement is there only to provide three lines of input) (这是ruby 1.9.2。echo语句仅用于提供三行输入)

It looks like this was a bug in 1.9.2 . 看来这是1.9.2中的错误 It was fixed for 2.0.0 (and backported to 1.9.3 ). 它已针对2.0.0进行了修复 (并反向移植到1.9.3 )。 The fix is simply to upgrade to a more recent version of Ruby. 解决方法只是升级到Ruby的最新版本。

You should probably upgrade if you can anyway, since support for 1.9.2 has just ended . 无论如何,您都应该升级,因为对1.9.2的支持才刚刚结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM