[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

proc.new and return

S2

2/28/2008 12:37:00 PM

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?
13 Answers

Arlen Cuss

2/28/2008 12:54:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Feb 28, 2008 at 11:39 PM, S2 <email@fin.ta> 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?


Since it isn't a function! :( Now, it can return sometimes -- if it's in a
function defition. And you can see that it could come in handy:

def slow_include arr, el
arr.each do |e|
return true if e == el
end
false
end

p slow_include([1, 2, 3], 3)
p slow_include([1, 2, 3], 4)

Result:

true
false

(of course, you can just use [1, 2, 3].include?(3), but this is for demo)
So, the block there returns from the function, like you'd expect. It's up to
the controlling function (Array#each in this case) to select a return value.
For instance:

[2, 4, 6].map {|q| return q * 2}

What's the expected output? Do we simply get "4" as the entire evaluation?
Remember that Array#map itself is supposed to return a value (the array with
values mapped!). So, blocks can't really return anything - it makes no
sense.

HTH,
Arlen

Jari Williamsson

2/28/2008 1:01:00 PM

0

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

S2

2/28/2008 1:09:00 PM

0

Arlen Cuss wrote:
> Since it isn't a function!

doh! of course. i feel really stupid now :)


[cut of interesting stuff]

> So, blocks can't really return anything - it makes no
> sense.

thanks

S2

2/28/2008 1:13:00 PM

0

Arlen Cuss wrote:
> [2, 4, 6].map {|q| return q * 2}

with your post and that of Jari i now understand:

irb(main):001:0> [2, 4, 6].map {|q| return q * 2}
LocalJumpError: unexpected return
from (irb):1
from (irb):1:in `map'
from (irb):1


as expected. but if i put it inside a method

irb(main):002:0> def hello
irb(main):003:1> [2, 4, 6].map {|q| return q * 2}
irb(main):004:1> end
=> nil
irb(main):005:0> hello
=> 4

ThoML

2/28/2008 1:16:00 PM

0

> as expected. but if i put it inside a method

AFAIK the way return inside a block works has changed in ruby 1.9
though (it then returns from the block).

Arlen Cuss

2/28/2008 1:20:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Fri, Feb 29, 2008 at 12:15 AM, S2 <non.sto.gioando@nien.te> wrote:

> with your post and that of Jari i now understand:
>
> irb(main):001:0> [2, 4, 6].map {|q| return q * 2}
> LocalJumpError: unexpected return
> from (irb):1
> from (irb):1:in `map'
> from (irb):1
>
>
> as expected. but if i put it inside a method
>
> irb(main):002:0> def hello
> irb(main):003:1> [2, 4, 6].map {|q| return q * 2}
> irb(main):004:1> end
> => nil
> irb(main):005:0> hello
> => 4
>

Excellent. :) Glad we could help. [I speak too on Jari's behalf here, it
seems. I'm sure he's glad too. :)]

Arlen

S2

2/28/2008 1:21:00 PM

0

ThoML wrote:
>> as expected. but if i put it inside a method
>
> AFAIK the way return inside a block works has changed in ruby 1.9
> though (it then returns from the block).

so in 1.9
Proc.new { true }
and
Proc.new { return true }
are the same?

Arlen Cuss

2/28/2008 1:29:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Fri, Feb 29, 2008 at 12:19 AM, ThoML <micathom@gmail.com> wrote:

> > as expected. but if i put it inside a method
>
> AFAIK the way return inside a block works has changed in ruby 1.9
> though (it then returns from the block).
>

Hmm... I don't see this behaviour, but I may be out of date.


irb(main):004:0> RUBY_VERSION
=> "1.9.0"
irb(main):005:0> Proc.new {return true}.call
LocalJumpError: unexpected return
from (irb):5:in `block (3 levels) in irb_binding'
from (irb):5:in `call'
from (irb):5
from /usr/lib/ruby/1.9/irb.rb:150:in `block (2 levels) in
eval_input'
from /usr/lib/ruby/1.9/irb.rb:259:in `signal_status'
from /usr/lib/ruby/1.9/irb.rb:147:in `block in eval_input'
from /usr/lib/ruby/1.9/irb.rb:146:in `eval_input'
from /usr/lib/ruby/1.9/irb.rb:70:in `block in start'
from /usr/lib/ruby/1.9/irb.rb:69:in `catch'
from /usr/lib/ruby/1.9/irb.rb:69:in `start'
from /usr/bin/irb1.9:13:in `<main>'
irb(main):006:0>

I can't find any relevant documentation that says it should be otherwise.

Arlen.

Mark Bush

2/28/2008 1:34:00 PM

0

ThoML wrote:
> AFAIK the way return inside a block works has changed in ruby 1.9
> though (it then returns from the block).

Not in my copy:

irb(main):003:0> a = Proc.new {return true}
=> #<Proc:0x3ccda0@(irb):3>
irb(main):004:0> a.call
LocalJumpError: unexpected return
from (irb):3:in `block (4 levels) in irb_binding'
from (irb):4:in `call'
from (irb):4
from /Users/bush/Src/Ruby/Ruby1.9/lib/ruby/1.9.0/irb.rb:149:in `block
(2 levels) in eval_input'
from /Users/bush/Src/Ruby/Ruby1.9/lib/ruby/1.9.0/irb.rb:262:in
`signal_status'
from /Users/bush/Src/Ruby/Ruby1.9/lib/ruby/1.9.0/irb.rb:146:in `block
in eval_input'
from /Users/bush/Src/Ruby/Ruby1.9/lib/ruby/1.9.0/irb.rb:145:in
`eval_input'
from /Users/bush/Src/Ruby/Ruby1.9/lib/ruby/1.9.0/irb.rb:69:in `block
in start'
from /Users/bush/Src/Ruby/Ruby1.9/lib/ruby/1.9.0/irb.rb:68:in `catch'
from /Users/bush/Src/Ruby/Ruby1.9/lib/ruby/1.9.0/irb.rb:68:in `start'
from ../Ruby1.9/bin/irb:12:in `<main>'

ruby 1.9.0 (2008-02-21 revision 15557) [i686-darwin9.1.0]

--
Posted via http://www.ruby-....

ThoML

2/28/2008 2:46:00 PM

0

> so in 1.9
> Proc.new { true }
> and
> Proc.new { return true }
> are the same?

Okay, it seems I mixed something up here. I think what I actually
referred to (I have to admit this currently is as unclear as it is to
you) probably is the change that proc has undergone.

def foo(y)
x = lambda {|x| return x}
x.call(y)
return y * 2
end

def bar(y)
x = proc {|x| return x}
x.call(y)
return y * 2
end

p [foo(1), bar(1)]

With ruby19 you get [2, 1]. With ruby18 it's [2, 2].