3 ms·
You can pass an anonymous function or lambda: def foo(thing, block): thing = "cruel " + thing block(thing) # like `yield thing' or 'block.call'
by rtomayko 17y ago
You can pass an anonymous function or lambda:
def foo(thing, block):
thing = "cruel " + thing
block(thing) # like `yield thing' or 'block.call' in ruby
def bar(thing):
return "hello " + thing
foo('world', bar)
# or:
foo('world', lambda x: "hello " + x)
Ruby's blocks are really just sugar for passing an anonymous function in the last argument and yield is just sugar for calling that function. The example above in Ruby would be:
def foo(thing)
yield thing
end
foo('world') { |thing| "hello " + thing }