def m1 &block
m2 &block
end
def m2
yield
end
m1 {puts "hello"} # hello
a = Proc.new {puts "world"}
m2 &a # world
def m3 block
instance_eval &block
end
m3 a # world
Tuesday, February 13, 2007
Convert Proc object to a block
Ruby methods like yield or instance_eval accept only block not Proc object. In order to use instance_eval in inner method, we need to convert Proc to block, the syntax is pretty simple just precede Proc object with ampersand.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment