You can also use the use of the method in Ruby, using the length of the arguments with arity:
class foo
attr_accessor :list
def initialize
@list=[]
end
def bar(&blk)
blk.arity>0 ? blk.call(self) : instance_eval(&blk)
end
end
x = Foo.new
x.bar do list <1 list <2 list <3 end
x.bar do | foo | foo.list <4 foo.list <5 foo.list <6 end
puts x.list.inspect
sasuke
source
share