Home arrow Ruby-on-Rails arrow Page 2 - Code Blocks and Iteration
RUBY-ON-RAILS

Code Blocks and Iteration


Code blocks can be very confusing to newcomers to Ruby, despite the fact that many computer languages have something that functions in a similar manner. This article, the first of three parts, introduces you to code blocks. It is excerpted from chapter eight of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
March 01, 2007
TABLE OF CONTENTS:
  1. · Code Blocks and Iteration
  2. · 7.1 Creating and Invoking a Block
  3. · 7.2 Writing a Method That Accepts a Block
  4. · 7.3 Binding a Block Argument to a Variable

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Code Blocks and Iteration - 7.1 Creating and Invoking a Block
(Page 2 of 4 )

Problem

You want to put some Ruby code into an object so you can pass it around and call it later.

Solution

By this time, you should familiar with a block as some Ruby code enclosed in curly brackets. You might think it possible to define a block object as follows: 

  aBlock = { |x| puts x }      # WRONG

  # SyntaxError: compile error

That doesn’t work because a block is only valid Ruby syntax when it’s an argument to a method call. There are several equivalent methods that take a block and return it as an object. The most favored method isKernel#lambda:*

  aBlock = lambda { |x| puts x }    # RIGHT

To call the block, use thecallmethod:

  aBlock.call "Hello World!"
 
# Hello World!

Discussion

The ability to assign a bit of Ruby code to a variable is very powerful. It lets you write general frameworks and plug in specific pieces of code at the crucial points.

As you’ll find out in Recipe 7.2, you can accept a block as an argument to a method by prepending&to the argument name. This way, you can write your own trivial version of thelambdamethod:

  def my_lambda(&aBlock)
    aBlock
  end

  b = my_lambda { puts "Hello World My Way!" }
 
b.call
 
# Hello World My Way!

A newly defined block is actually aProcobject.

  b.class                   # => Proc

You can also initialize blocks with theProcconstructor or the methodKernel#proc. The methodsKernel#lambda,Kernel#proc, andProc.newall do basically the same thing. These three lines of code are nearly equivalent:

  aBlock = Proc.new { |x| puts x }
  aBlock = proc { |x| puts x }
  aBlock = lambda { |x| puts x }

What’s the difference?Kernel#lambdais the preferred way of creating block objects, because it gives you block objects that act more like Ruby methods. Consider what happens when you call a block with the wrong number of arguments:

  add_lambda = lambda { |x,y| x + y }

  add_lambda.call(4)
  # ArgumentError: wrong number of arguments (1 for 2)

  add_lambda.call(4,5,6)
  # ArgumentError: wrong number of arguments (3 for 2)

A block created withlambdaacts like a Ruby method. If you don’t specify the right number of arguments, you can’t call the block. But a block created withProc.newacts like the anonymous code block you pass into a method likeEnumerable#each:

  add_procnew = Proc.new { |x,y| x + y }

  add_procnew.call(4)
  # TypeError: nil can't be coerced into Fixnum

  add_procnew.call(4,5,6)            # => 9

If you don’t specify enough arguments when you call the block, the rest of the arguments are givennil. If you specify too many arguments, the extra arguments are ignored. Unless you want this kind of behavior, uselambda.

In Ruby 1.8,Kernel#procacts likeKernel#lambda. In Ruby 1.9,Kernel#procacts likeProc.new, as better befits its name.

See Also
  1. Recipe 7.2, “Writing a Method That Accepts a Block”
  2. Recipe 10.4, “Getting a Reference to a Method”


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials