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

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 iend

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.


The pipes specify arguments that are populated with values by the function that calls your block. There can be zero or more of them, and how many you should use depends on the method you call.

For example, each_with_index uses two variables and puts the element in one of them and the index in the other.

here is a good description of how blocks and iterators work


Block arguments follow all the same conventions as method parameters (at least as of 1.9): you can define optional arguments, variable length arg lists, defaults, etc. Here's a pretty decent summary.

Some things to be aware of: because blocks see variables in the scope they were defined it, if you pass in an argument with the same name as an existing variable, it will "shadow" it - your block will see the passed in value and the original variable will be unchanged.

i = 1025.times { | i | puts i }puts i #=> prints '10'

Will print '10' at the end. Because sometimes this is desirable behavior even if you are not passing in a value (ie you want to make sure you don't accidentally clobber a variable from surrounding scope) you can specify block-local variable names after a semicolon after the argument list:

x = 'foo'25.times { | i ; x | puts i; x = 'bar' }puts x #=> prints 'foo'

Here, 'x' is local to the block, even though no value is passed in.