I've just began learning about blocks and using method_missing
in Ruby classes, and I've noticed the general formula is
def method_missing(sym, *args, &block)
My question is if it's possible to execute the &block
in the output. For example:
class Foo
def method_missing(sym, *args, &block)
puts "#{sym} was called with #{args} and returned #{block.call(args)}"
end
end
bar = Foo.new
bar.test(1,2,3, lambda {|n| n + 2} )
Is there a way to make this work so that the block returns a new array?
I think maybe you want this:
Execution result:
Updated: yield can be used as below.