I want to return the output of yield but also execute the code after yield, is there a more "right" way?:
def myblock
yield_output = yield
puts 'after yield'
yield_output
end
myblock {'my yield'}
# after yield
# => my yield
You could use tap:
def myblock
yield.tap { puts 'after yield' }
end
myblock { 'my yield' }
# after yield
#=> my yield
(yield 5).tap { puts 'after yield' }.yield is not a method, I'd put the parentheses around the argument(s), i.e. yield(5).tap { ... }, but either way works.'my yield') in your code: tap passes the receiver to the block, so you can write yield.tap { |result| ... }