1
我有下面的類實例化實例變量爲塊
class Increasable
def initializer(start, &increaser)
@value = start
@increaser = increaser
end
def increase()
value = increaser.call(value)
end
end
如何用塊初始化?這樣做
inc = Increasable.new(1, { |val| 2 + val})
在irb
我得到
(irb):20: syntax error, unexpected '}', expecting end-of-input
inc = Increasable.new(1, { |val| 2 + val})