O'Reilly logo

Learning Sinatra by Manoj Sehrawat, Sudeep Agarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Code blocks

Code-blocks in Ruby are pieces of multiline code written in curly brackets or within a do..end block that can be passed around to methods such as parameters.

While discussing arrays, we saw the following:

arr.each do |e|
  puts e
end

Here, lets see what the preceding code will do:

do |e|
  puts e
end

This is a code-block which is passed as a parameter to the arr.each function.

Apart from passing code, code-blocks can be used to implement iterators and callbacks.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required