ruby block and returning something from block
Posted
by dorelal
on Stack Overflow
See other posts from Stack Overflow
or by dorelal
Published on 2010-05-14T18:08:48Z
Indexed on
2010/05/14
20:24 UTC
Read the original article
Hit count: 281
ruby
I am using ruby 1.8.7.
p = lambda { return 10;}
def lab(block)
puts 'before'
puts block.call
puts 'after'
end
lab p
Above code output is
before
10
after
I refactored same code into this
def lab(&block)
puts 'before'
puts block.call
puts 'after'
end
lab { return 10; }
Now I am getting LocalJumpError: unexpected return.
To me both the code are doing same thing. Yes in the first case I am passing a proc and in the second case I am passing a block. But &block converts that block into proc. So proc.call should behave same.
And yes I have seen this post http://stackoverflow.com/questions/2325471/using-return-in-a-ruby-block
© Stack Overflow or respective owner