Last active
October 14, 2015 08:19
-
-
Save jlecour/7ff6de9ff14ca6b2cc36 to your computer and use it in GitHub Desktop.
Is there a better way to have a first method (`prelude` here) accept an optional block, pass it to another method (`final` here) and have it yielded in the end, without having to yield also in the first method ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prelude | |
if block_given? | |
final { |name| | |
yield(name) | |
} | |
else | |
final | |
end | |
end | |
def final | |
if block_given? | |
yield("world") | |
else | |
puts "- no block given to #final" | |
end | |
end | |
puts "# prelude with no block" | |
prelude | |
puts "" | |
puts "# prelude with a block" | |
prelude { |name| | |
puts "Hello #{name} from prelude block" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to @jjaffeux for the solution :
I'va previously tried that, but forgot to put the
&
also when callingfinal(&block)
so the block was passed without being converted to a Proc (or something like this).