Proc.new and return

S2 wrote:

I am sure this is a really easy question for most of you, but i was not able to find an answer to this, or to understand the difference between this two:

$ irb
irb(main):001:0> a = Proc.new {true}
=> #<Proc:0xb7cae308@(irb):1>
irb(main):002:0> b = a.call
=> true
irb(main):003:0> a = Proc.new {return true}
=> #<Proc:0xb7c9f768@(irb):3>
irb(main):004:0> b = a.call
LocalJumpError: unexpected return
        from (irb):3
        from (irb):4:in `call'
        from (irb):4
        from :0

why can't a block return something?

The result you're getting is for having a return keyword outside a method. If you instead do this, return will work.

···

---
def my_proc
     a = Proc.new {return true}
     puts "Call it"
     a.call
     puts "This will never happen"
     false
end

puts my_proc
---

=>
Call it
true

Best regards,

Jari Williamsson