I am writing a program that utilizes define_method, but I don't understand how I could define a method like this one:
def mymethod(variable) do
puts variable
puts yield
end
Which can be called by:
mymethod("hi") do
# this is yield
end
You cannot use yield. You need to receive it as a proc object.
define_method(:mymethod) do |variable, &block|
puts variable
puts block.call
end
mymethod("foo"){"bar"}
# foo
# bar
mymethod("foo") do "bar" end
# foo
# bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With