|song| Not documented in Pickaxe Book

S

Sam'l B

Pickaxe Book (2nd Ed.), P. 49, 2nd example, line 3:

@songs.find {|song| title == song.name }

I can't find a definition of the "|song|" construction in the book.
It is intuitively obvious that it is a variable, and a sort of
"catcher's mitt" for the output of the method "@songs.find" -- and acts
that way for all other example in the book that I can find.

What I _can't_ find, though, is a discussion of the construction -- or
did I miss it somewhere?

Sam Bassett
 
T

Timothy Hunter

Sam'l B said:
Pickaxe Book (2nd Ed.), P. 49, 2nd example, line 3:

@songs.find {|song| title == song.name }

I can't find a definition of the "|song|" construction in the
book. It is intuitively obvious that it is a variable, and a sort of
"catcher's mitt" for the output of the method "@songs.find" -- and
acts that way for all other example in the book that I can find.

What I _can't_ find, though, is a discussion of the construction
-- or did I miss it somewhere?

Sam Bassett
In the Pickaxe 2nd Ed., look at the top of page 76.
 
S

Sam'l B

Nope -- P. 76 has a discussion of gsub, in the context of "Backslash
Sequences in the Substitution"
 
D

dblack

Hi --

Nope -- P. 76 has a discussion of gsub, in the context of "Backslash
Sequences in the Substitution"

Look in the middle of page 22.


David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

Jeremy McAnally

That is a parameter passed to a block. It allows the method that
yielded to the block to pass information into the block, which in this
case is a song title (I'm guessing?).

Think of it (sort of) as the method yielding to the block feeding it a
parameter like you would a method call: a value is passed in from the
caller and stored in a local instance to be used in the block.

--Jeremy
 
D

Devin Mullins

Giles said:
The online version of the first edition of Pickaxe has it here:

http://www.rubycentral.com/book/intro.html

Scroll down to "Blocks and Iterators." It's about 2/3rds down the page.

Here's an URL with an anchor link, for the especially lazy:
http://phrogz.net/ProgrammingRuby/language.html#blocksclosuresandprocobjects

Note the mention of the rules of parallel assignment, and click the
link. You'll get more info than you ever wanted to know.

Or, at least, *should* have wanted to know. With irb and Test::Unit, who
needs to remember these details?

Devin
 
S

Sam'l B

Jeremy said:
That is a parameter passed to a block. It allows the method that
yielded to the block to pass information into the block, which in this
case is a song title (I'm guessing?).

Think of it (sort of) as the method yielding to the block feeding it a
parameter like you would a method call: a value is passed in from the
caller and stored in a local instance to be used in the block.

That's what I had figured out intuitively -- it catches whatever the
method on the left spits out.

Sam'l
 
S

Sam'l B

R

rwtnorton

Pickaxe Book (2nd Ed.), P. 49, 2nd example, line 3:

@songs.find {|song| title == song.name }

I can't find a definition of the "|song|" construction in the book.
It is intuitively obvious that it is a variable, and a sort of
"catcher's mitt" for the output of the method "@songs.find" -- and
acts that way for all other example in the book that I can find.

What I _can't_ find, though, is a discussion of the construction --
or did I miss it somewhere?

Sam Bassett


Why, it's a chute, of course:
http://poignantguide.net/ruby/chapter-3.html (Section: Block
Arguments)

;-)

(Definitely, my all-time favorite intro-to-a-programming-language text.)

Richard W. Norton
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top