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

**URL:** <https://www.ruby-forum.com/t/how-do-you-know-when-to-put-a-variable-between-pipes/116914>\
**Category:** Ruby\
**Created:** [October 11, 2007, 3:35am UTC](https://www.ruby-forum.com/t/how-do-you-know-when-to-put-a-variable-between-pipes/116914 "2007-10-11T03:35:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [October 11, 2007, 3:35am UTC](https://www.ruby-forum.com/t/how-do-you-know-when-to-put-a-variable-between-pipes/116914/1 "2007-10-11T03:35:43Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [October 11, 2007, 4:27am UTC](https://www.ruby-forum.com/t/how-do-you-know-when-to-put-a-variable-between-pipes/116914/2 "2007-10-11T04:27:19Z")

</div>

On Oct 10, 2007, at 9:35 PM, A Love of Surf wrote:

> 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

---

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [October 11, 2007, 4:46am UTC](https://www.ruby-forum.com/t/how-do-you-know-when-to-put-a-variable-between-pipes/116914/3 "2007-10-11T04:46:20Z")

</div>

A Love of Surf wrote:

> 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).

---

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [October 11, 2007, 4:52am UTC](https://www.ruby-forum.com/t/how-do-you-know-when-to-put-a-variable-between-pipes/116914/4 "2007-10-11T04:52:34Z")

</div>

Another way to explain, complementing our friend Morton, is that when  
you  
use a block, you are kind of inserting code inside another function. Let  
me  
give you an example to help you understand.

Let’s take the “each” method of an array. You use this method when you  
want  
to do something with every item inside the array.

You could call something like:  
my\_string = “”  
my\_array.each do |foo|  
puts foo  
my\_string += " AND #{foo}"  
end

Now, why did we place that |foo| on the block? Imagine that the “each”  
method on the array is implemented like this:

def each  
i = 0  
while i \<= self.size  
current\_item = self[i]  
yield(current\_item)  
end  
self  
end

Now notice two things: First, the “each” method itself has no  
parameters,  
though it could have. They would have nothing to do with the pipes,  
though.  
Second, look at the yield line. what yield do is to call the block that  
you  
gave to this function. Since the yield is inside the while statement, it  
will run as many times as there are items on your array. And, as i said  
it  
is just like putting your piece of code inside the “each” (or any other)  
method. It would be like you’ve written

def my\_each  
my\_string = “”  
i = 0  
while i \<= self.size  
current\_item = self[i]  
# ----  
puts current\_item  
my\_string += " AND #{foo}"  
# -----  
end  
self  
end

Now, the actual values that the block will take will clearly depend on  
the  
function that you are using. SInce yield can pass as many parameter as  
it  
wants (you could have something like yield a, b, c, d, e, …) you  
should  
see the documentation of the function to know which values are passed to  
the  
block.

It’s obviously not a technical explanation, and some things are  
intentionally wrong (the each method for array is implemented in C, for  
instance), but maybe it helps you understand.

Hope this helps

---

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [October 11, 2007, 8:05am UTC](https://www.ruby-forum.com/t/how-do-you-know-when-to-put-a-variable-between-pipes/116914/5 "2007-10-11T08:05:51Z")

</div>

On Oct 10, 7:46 pm, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> block. For example:  
> method is going to send one or more values to a block. Or, if the book  
> each element of the array to the block one at a time.  
> yield 20  
> variable in a method definition.
> 
> 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).
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

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

---

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [October 11, 2007, 4:54am UTC](https://www.ruby-forum.com/t/how-do-you-know-when-to-put-a-variable-between-pipes/116914/6 "2007-10-11T04:54:19Z")

</div>

My example was wrong. Where it reads

def my\_each  
my\_string = “”  
i = 0  
while i \<= self.size  
current\_item = self[i]  
# ----  
puts current\_item  
my\_string += " AND #{foo}"  
# -----  
end  
self  
end

it should read

def my\_each  
my\_string = “”  
i = 0  
while i \<= self.size  
current\_item = self[i]  
# ----  
puts current\_item  
my\_string += " AND #{current\_item}"  
# -----  
end  
self  
end
