Why do these two code blocks have different behavior?

Y

yuricake

I am not a total newbie in ruby, but I am still hazy on how things
like blocks, proc objects and contexts work under the covers. I have
experience with Lisp lambdas, so feel free to get technical.

My question is - why do the following two functions have different
behavior?

require "pp"

@@a = [1, 2, 3]

def q1
@@a.inject(0) {|sum, x|
pp x
return sum + x
}
end

def q2
@@a.inject(0) {|sum, x|
pp x
sum + x
}
end
 
G

Gary Wright

My question is - why do the following two functions have different
behavior?

require "pp"

@@a = [1, 2, 3]

def q1
@@a.inject(0) {|sum, x|
pp x
return sum + x
}
end

def q2
@@a.inject(0) {|sum, x|
pp x
sum + x
}
end

Because the return statement terminates the method q1 and not the
block that is associated with the call to inject. You are effectively
bailing out of inject on the first iteration.

In q2, when control runs off the end of the block, control is returned
to inject, which can continue with its iterations.

Gary Wright
 
J

Jano Svitok

I am not a total newbie in ruby, but I am still hazy on how things
like blocks, proc objects and contexts work under the covers. I have
experience with Lisp lambdas, so feel free to get technical.

My question is - why do the following two functions have different
behavior?

require "pp"

@@a = [1, 2, 3]

def q1
@@a.inject(0) {|sum, x|
pp x
return sum + x
this return will jump out of the method altogether. So the method will
print x once, and then return sum + x, that is 0 + 1 = 1
}
end

def q2
@@a.inject(0) {|sum, x|
pp x
sum + x
}
end

in this case, there's no return to interrupt the inject loop, so the
sum will be calculated properly and return value from the method will
be the return value of inject, i.e. the sum 6.
 
S

Sylvain Joyeux

def q1
@@a.inject(0) {|sum, x|
pp x
return sum + x
}
end
I'll add one thing to the other's explanations: 'next' does what you
thought 'return' was doing: it terminates the block, returning the value
given as argument

def q1
@@a.inject(0) do |sum, x|
pp x
next sum + x
end
end

will also work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top