blocks, scope/context confusion

C

Corey

I'm reading pages 358 & 359 in the pickaxe2 and having a tough time
wrapping my mind around what exactly is going on.

I've got a few simple questions that I'm hoping will help clarify my
confusion.


What's the difference between the two methods below?

def meth1
yield
end

def meth2(&b)
b.call
end


What's the difference between the next two blocks? Does 'next' in any way
make any sort of functional or subtle difference?

blk = meth1 { 99 }
puts blk

blk = meth1 { next 99 }
puts blk

and...
Why does the following cause an exception?

meth { return 99 }


As far as the above goes, I'm really struggling to understand how the
scope and context works.

"A return from a block whose original context is not longer valid raises
an exception ( LocalJumpError )."

In the following:

meth { return 99 }

_Where_/_What_ exactly _is_ the "original context", and how exactly does
it become "no longer valid" with the 'return' statement?


I've got some other questions, but I'm hoping that the above will help
make some things 'click' for me.


Many thanks!
 
C

Corey

meth { return 99 }

Quick correction/clarification -- in my post, the references/questions
regarding that block, should have of course been to 'meth1'; not 'meth'.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: blocks, scope/context confusion"

|Why does the following cause an exception?
|
|meth { return 99 }

Because "return 99" at the toplevel causes an exception as well.
Try

def foo
meth1 { return 99 }
end
p foo

matz.
 
G

gwtmp01

I'm reading pages 358 & 359 in the pickaxe2 and having a tough time
wrapping my mind around what exactly is going on.

I'm still climbing up the Ruby learning curve so I hope someone
will gently correct my answers if I get them wrong :)

In all these examples and my descriptions below I'm talking about what
the book calls "raw procs" as opposed to "lambdas" which have slightly
different semantics.
What's the difference between the two methods below?

def meth1
yield
end

def meth2(&b)
b.call
end

There is no semantic difference. The first example is
syntactic sugar for the second example. I believe there is
a small "implementation" difference though. In the first example
Ruby never converts the block associated with a call to meth1 to
a full-blown instance of Proc. In the second example, the block is
packaged up into a Proc object and then the method lookup for "call"
results in the execution of the block. So the first example saves
Ruby the trouble of allocating an object and then dispatching the
method,
and then the garbage collector eventually having to reclaim the
discarded
Proc.
What's the difference between the next two blocks? Does 'next' in
any way
make any sort of functional or subtle difference?

blk = meth1 { 99 }
puts blk

blk = meth1 { next 99 }
puts blk

Also no semantic difference but that is just because the next statement
happens to be the last expression in the block (from both a syntax and
execution standpoint). Another way to say this is that the result of
execution falling off the end of a block is for ruby to execute an
implicit

next val

where 'val' is the value of the last expression in the block.
Why does the following cause an exception?

meth { return 99 }

Because in this particular example, the block is defined in the
top-level context and at the top-level there is no active method
and so there is nothing to "return" from. You get the same error
if your entire ruby program was just:

return 99

When the block is defined within a method context you don't get the
error:

def foo
meth1 { return 99 }
return "impossible"
end

foo # 99

Note that the 'return "impossible"' statement is never executed.
"A return from a block whose original context is not longer valid
raises
an exception ( LocalJumpError )."
In the following:

meth { return 99 }

_Where_/_What_ exactly _is_ the "original context", and how exactly
does
it become "no longer valid" with the 'return' statement?

I think it is a bad example because the original context was *never*
valid
to start with. Here is a different example where the context exists
and is
valid at the time the block is defined but doesn't exist (and so is no
longer valid) at the time the block is executed:

def level1
level2 { return "return from level1" }
end

def level2(&block)
return block
end

bogus_block = level1
bogus_block.call # ERROR


Lambdas: The main difference with a block that is converted into a
lambda
is that a 'return' statement in a lambda is associated with the
lambda itself
and not the method that was active at the time associated block was
defined.
So within a lambda, 'return' has the same semantics as 'next' as
described
above.

def level1
level2 { return "return from lambda" }
end

def level2(&block)
return (lambda &block)
end

valid_lambda = level1
valid_lambda.call # "return from lambda"



Gary Wright
 
C

Corey

|Why does the following cause an exception?
|
|meth { return 99 }

Because "return 99" at the toplevel causes an exception as well.

Aha! That makes sense, thanks!
 
C

Corey

I'm still climbing up the Ruby learning curve so I hope someone
will gently correct my answers if I get them wrong :)
<snip>


Wow... that was a phenomenaly informative explanation for each of my
questions!

Many, many thanks - that was _extremely_ helpfull.


Beers!

Corey
 
G

Gavin Kistner

--Apple-Mail-2-611845666
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

Does anyone know of any good articles/blogs/whatever that further
describes
and explains closures/blocks and their use in specific ruby terms/
idioms?

Not quite what you are looking for, but Jibber Jim has a good
technical article on closures in Javascript at:
http://jibbering.com/faq/faq_notes/closures.html


--Apple-Mail-2-611845666--
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top