|how do you know when to put a variable between pipes?|

A

A Love of Surf

Hi i am at about page fifty in the pickaxe book, and i have a
question. I see a lot of examples of a block that starts with |
SomeVar| and I dont really understand how you know when you can do
this or what the value will be that is passed to the variable. What
is the name of this construct or the var that goes in pipes? Thank
you :)
 
M

Morton Goldberg

Hi i am at about page fifty in the pickaxe book, and i have a
question. I see a lot of examples of a block that starts with |
SomeVar| and I dont really understand how you know when you can do
this or what the value will be that is passed to the variable. What
is the name of this construct or the var that goes in pipes? Thank
you :)

In the PDF version of Pickaxe, this is explained beginning on p. 47
(p.50 in the printed text). Read the explanation that goes along with
the fib_up_to example. More info can be found in Chapter 22 under the
heading: Blocks, Closures, and Proc Objects.

A quick and dirty explanation is the the vertical bars at the
beginning of a block delimit the block's formal arguments. When the
block is evaluated by a yield expression, the actually arguments
passed to the yield will be assigned to the formal arguments in a
manner similar to a function call. Just as you can't call a function
without knowing what arguments it takes, you must know what arguments
a method accepting a block passes to the block in order to write a
block for that method. It is a matter of documentation.

Regards, Morton
 
7

7stud --

A said:
Hi i am at about page fifty in the pickaxe book, and i have a
question. I see a lot of examples of a block that starts with
|SomeVar|
and I dont really understand how you know when you can do
this

Whenever a method specified to the left of the block has a yield
statement in its definition, the method is going to pass a value to the
block. For example:

def my_method
yield 10
end

my_method {|num| puts num * 2} #20

How do you know if one of ruby's methods has a yield statement in its
definition? You could look at the source code, but for now just
remember that when the book says that a method is an iterator, then the
method is going to send one or more values to a block. Or, if the book
introduces a method and also uses a block after the method call, then
you know the definition of the method has a yield statement in it, which
just means it is going to send one or more values to the block. What
you should be trying to figure out is:

1) what values the method call sends to the block

2) what the return value of method call is

The most common iterator is 'each'. When called on an array, each sends
each element of the array to the block one at a time.

You should make frequent use of the reference section in the back of the
book to look up methods and read about what values they send to a block,
and what the return value of the method call is.
or what the value will be that is passed to the variable.

It depends on how the method you call is defined:

def my_method
yield 20
yield 30
end

my_method {|num| puts num * 2} #40 60
What
is the name of this construct or the var that goes in pipes?

The construct is exactly like a method definition, and it is called a
"block". The thing in the pipes is the same thing as a parameter
variable in a method definition.

You are calling a method:

my_method

with a block specified on the right:

{|num| puts num * 2}

The block is just like a method, but it doesn't have a name.

Backing up a little, you can define methods that call each other like
this:

def my_method
calculate(10)
end

def calculate(num)
puts num * 2
end

my_method #20

The same thing is happening with a method call that is followed by a
block: you are calling a method that sends values to another method(the
block).
 
A

A Love of Surf

Whenever a method specified to the left of the block has a yield
statement in its definition, the method is going to pass a value to the
block. For example:

def my_method
yield 10
end

my_method {|num| puts num * 2} #20

How do you know if one of ruby's methods has a yield statement in its
definition? You could look at the source code, but for now just
remember that when the book says that a method is an iterator, then the
method is going to send one or more values to a block. Or, if the book
introduces a method and also uses a block after the method call, then
you know the definition of the method has a yield statement in it, which
just means it is going to send one or more values to the block. What
you should be trying to figure out is:

1) what values the method call sends to the block

2) what the return value of method call is

The most common iterator is 'each'. When called on an array, each sends
each element of the array to the block one at a time.

You should make frequent use of the reference section in the back of the
book to look up methods and read about what values they send to a block,
and what the return value of the method call is.


It depends on how the method you call is defined:

def my_method
yield 20
yield 30
end

my_method {|num| puts num * 2} #40 60


The construct is exactly like a method definition, and it is called a
"block". The thing in the pipes is the same thing as a parameter
variable in a method definition.

You are calling a method:

my_method

with a block specified on the right:

{|num| puts num * 2}

The block is just like a method, but it doesn't have a name.

Backing up a little, you can define methods that call each other like
this:

def my_method
calculate(10)
end

def calculate(num)
puts num * 2
end

my_method #20

The same thing is happening with a method call that is followed by a
block: you are calling a method that sends values to another method(the
block).

Thank you so much for your help, I was having a tough time with that
but you cleared it up for me.
 

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,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top