如果我有一个类:
class KlassWithSecret
def initialize
@secret = 99
end
end
并运行:
puts KlassWithSecret.new.instance_eval { @secret }
它会输出99,但如果我运行:
puts KlassWithSecret.new.instance_eval do
@secret
end
出现错误:`instance_eval': 错误的参数数量 (0 for 1..3) (ArgumentError)
为什么不能在instance_eval
中使用do/end块?
附注:我正在使用Ruby 2.1.0。
do..end
块的优先级低于{..}
块有关。 - David Unricputs
一起使用的,我的错。已编辑问题。 - Alexander Popoveach
works with brackets but not withdo
-end
(ruby), Block definition - difference between braces anddo
-end
?,Ruby multiline block withoutdo
end
,Usingdo
block vs brackets{}
,What is the difference or value of these block coding styles in Ruby?,… - Jörg W Mittagdo
/end
和{}
不总是等效的?,Ruby块中的奇怪缺陷,将块传递到方法-Ruby,instance_eval
未提供块?,以及块语法差异导致“LocalJumpError: no block given (yield)
”。 - Jörg W Mittag