1. Technology
Send to a Friend via Email
You can opt-out at any time. Please refer to our privacy policy for contact information.

How to Use and Create Blocks in Ruby


As the key to Ruby's expressiveness, no program is complete without a block or two. Though they confuse some new Ruby programmers at first, they're actually pretty simple. Blocks are merely nameless methods you can pass to other methods.

Why Use Blocks?

At first blocks seem rather useless, but nothing is further from the truth. The most purposeful use of blocks is to abstract common types of loops into more friendly forms. For example, in a C++ or Java program it's not uncommon to use a for loop to loop over an array and, in most cases, each element of the array is visited once and in order. The same can be done in Ruby with the for or while loop , but in Ruby arrays have a method called each that takes a block as a parameter. The tedious and repetitive for loop is hidden and a single, expressive word each is put in its place.

Blocks and the Times Loop

Blocks are used extensively in Ruby. The first use a Ruby programmer is likely to encounter is the times loop. The times loop is nothing but a method of the Integer class. It's used as a small, primitive loop that will repeat itself a set number of times. The block, which will be called repeatedly, is passed as a single argument. Here, the Ruby program uses the times loop to print a message a set number of times.

#!/usr/bin/env ruby

5.times do
  puts "Blocks are powerful"

Where's the Block?

The block is the portion of code between the do and end keywords. The curly braces { and } may be used as well, since these are the same as the do and end keywords. All the code between these keywords is assembled into an anonymous method (a method without any name). The Ruby interpreter remembers this method and, when the times method is called, passes this nameless method to the times method. Blocks used in this way emulate a type of loop. The following example illustrates the each loop--another common use which will call a block for every element in an array.

#!/usr/bin/env ruby

array = [

array.each do|name|
  puts name

Passing Arguments

The previous example introduced another feature of blocks--the ability to pass arguments. The names between the pipe characters after the do keyword are the arguments. They're just like the named arguments to any other method, but have a slightly different syntax. The each loop will visit each of the elements in the array in order and call the block for each element, passing the current element as a parameter to that block.

Using Blocks

Writing methods that can accept blocks is simple. There's no special syntax needed, you can simply write the method as you would any other. To call any block passed to the method, use the yield keyword. Like any other method call, when you use the yield keyword you can leave it as it is and call the block without any parameters. Or, if you wish, you can add a parameter list as you would with a method call. The following example illustrates a method that re-implements the times method. The defined method takes a single named argument, the number of times the block should be called. The block argument is invisible, but the yield keyword knows about it and will call the block correctly.

#!/usr/bin/env ruby

def this_many_times(num)
  counter = 0
  while counter < num
    counter += 1

this_many_times(5) do
  puts "Blocks are powerful"
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Ruby's Basic Features
  6. How to Use and Create Blocks in Ruby

©2014 About.com. All rights reserved.