Tuesday, May 22, 2012

Blocks in rails 3

Blocks are generally simple code wrapped in a do/end construct.

Example

books = %w{book1 book2 book3 book4}
books.each do |name| print "Book name :: #{name}\n" end

In this example code between do and end is Blocks. what it does is that iterates an array using the each method and passes in each element(i.e name) to the code block

No comments:

Post a Comment