Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Catching what a block returns (A newbie lesson learned)

Reply
Thread Tools

Catching what a block returns (A newbie lesson learned)

 
 
Richard Lawrence
Guest
Posts: n/a
 
      07-11-2007
Hello all,

This is a message intended to be from one newbie to another.

Until last night, I couldn't quite understand something about Ruby
blocks: namely, how to get anything back from them. I have read a
number of tutorials that try to explain blocks, and they always use
trivial examples like this:

def verbose_call(x)
puts "I am calling a block now!"
yield x
puts "I am done calling the block!"
end

and then you can do wonderful things like:

verbose_call(3) {|n| puts n + 1}
verbose_call("Jenny") {|name| puts name + " from the block."}

This is all well and good, but it seemed to me that this couldn't be all
there was to the much-acclaimed power of blocks. Then I saw things like:

[1, 2, 3].inject(4) {|sum, elt| sum + elt} # => 10

and became confused. Clearly, the value returned by the block is
secretly being caught by inject(), then fed back into the block...but
how? This seemed a long way from the trivial examples: there, blocks
were a dead end, but here, there was two-way communication happening
between the method and the block.

The solution (of course) was to realize that "yield" actually evaluates
to something. Thus,

def catch_block_value
result = yield
puts "I got this from the block: ", result
end

Realizing that "yield" evaluates to something I can assign to variables,
etc. was important for getting beyond the trivial examples. In
retrospect, I should have paid more attention to the lesson that
*everything* evaluates in Ruby. I think what I was hung up on is that
"yield" is a keyword (right? at least it looks like one), and hence I
thought of it as mere flow control, not a passage for information.

So, again, totally trivial for all you veterans out there, but I hope
that this clears some things up for someone like me. Is there any
documentation that I can help improve by doing a little write-up of this?

Richard
 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      07-11-2007
Hi --

On Thu, 12 Jul 2007, Richard Lawrence wrote:

> Realizing that "yield" evaluates to something I can assign to variables, etc.
> was important for getting beyond the trivial examples. In retrospect, I
> should have paid more attention to the lesson that *everything* evaluates in
> Ruby. I think what I was hung up on is that "yield" is a keyword (right? at
> least it looks like one), and hence I thought of it as mere flow control, not
> a passage for information.


I'm pleased you worked this out, and it does indeed point you to a
great deal of yield's coolness. And you're right that keyword
expressions, too, always evaluate to something (with the exception of
return, since doing this:

x = return 1

would at best just discard x). It's true even for class and method
definitions: class definition blocks evaluate to the last expression
inside them, and def blocks evaluate to nil. So this:

class C
def m
end
end

evaluates to nil, while this:

class C
1
end

evaluates to 1. (And yes, this feature does actually get used in
slightly more real situations


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning to 'try' block after catching an exception Karlo Lozovina Python 10 05-23-2008 12:57 AM
catching exceptions from an except: block Arnaud Delobelle Python 35 03-09-2007 12:53 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
block returns and hash element returns Trans Ruby 2 11-06-2005 12:15 PM



Advertisments