Can someone explain Ruby’s use of pipe characters in a block?

Braces define an anonymous function, called a block. Tokens between the pipe are the arguments of this block. The number of arguments required depends on how the block is used. Each time the block is evaluated, the method requiring the block will pass a value based on the object calling it.

It’s the same as defining a method, only it’s not stored beyond the method that accepts a block.

For example:

def my_print(i) 
  puts i

will do the same as this when executed:

{|i| puts i}

the only difference is the block is defined on the fly and not stored.

Example 2:
The following statements are equivalent

25.times &method(:my_print)

25.times {|i| puts i}

We use anonymous blocks because the majority of functions passed as a block are usually specific to your situation and not worth defining for reuse.

So what happens when a method accepts a block? That depends on the method. Methods that accept a block will call it by passing values from their calling object in a well defined manner. What’s returned depends on the method requiring the block.

For example: In 25.times {|i| puts i} .times calls the block once for each value between 0 and the value of its caller, passing the value into the block as the temporary variable i. Times returns the value of the calling object. In this case 25.

Let’s look at method that accepts a block with two arguments.

{:key1 => "value1", :key2 => "value2"}.each {|key,value| 
     puts "This key is: #{key}. Its value is #{value}"

In this case each calls the block ones for each key/value pair passing the key as the first argument and the value as the second argument.

Leave a Comment