Ruby for Rails: Blocks


Ruby for Rails

This post is the first in a series of blog posts about Ruby for Rails beginners. These are specifically for Rails developers who want to solidify their grasp of the Ruby language.

More than a decade ago, I started learning Rails before I knew any Ruby. Rails is written in Ruby of course, so when writing Rails code you are writing Ruby code. But at the beginning, I didn't fully understand how Ruby worked. And that's fine. A lot of people started learning Rails and picked up Ruby along the way.

The "Ruby for Rails" series will help you learn Ruby to make you a better Rails programmer.

In this blog post, we'll talk about blocks. Ruby blocks can be confusing to beginners but it's important that you learn them because you'll keep on using them on your Rails application.

One of the first code you'll see with blocks is with loops.

(1..10).each do |i|
  puts i

The block is inside do and end. This code displays the numbers 1 to 10. When you read it, it kind of makes sense. From 1 to 10, for each i, puts i. If you have experience with other programming languages, you might think this is just a more complicated way to do loops.

each isn't only for numbers. You can also iterate through an array.

["Alice", "Bob", "Eve"].each do |name|
  puts name

This code iterates through each element and prints the name. In Rails, you commonly use each when iterating through your model.

Post.all.each do |post|
  puts post.title

Say you have a Post model with a title. When you call each on Post.all, you iterate on each of those posts and the value is assigned to post which is inside | |. Since post is a Post object, you can call post.title inside the block.

The examples so far show that blocks can be used to iterate through some objects. But the iteration is a by product of the each method. Blocks aren't just for iteration. Here's another common code in Rails.

<%= form_with do |form| %>
  <%= form.text_field :title %>
  <%= form.text_area :body %>
<% end %>

We use <% %> because this code appears on ERB templates. form_with is used to display an HTML form. Here, we call the form_with method and passes the parameter. form_with yields an object which we assign to form. You can use any variable inside | |. We could have used f here instead of form.

In the examples above that used each, we yield each of the objects we're iterating through. In form_with, we yield exactly one object which is the FormBuilder associated with the model. Inside the block you can use FormBuilder's methods to generate fields like text_field, text_area, check_box, button, etc.

All the fields inside the block are captured and displayed inside the HTML form element.

These are just a few examples of blocks. You'll see more when writing Rails code. So don't get blocked out when coding!

Try a Free Trial:

Engine Yard is so much more than just a Ruby on Rails PaaS platform. But don't just take our word for it. Request a free trial of Engine Yard platform today, and one of our Engineers will be in contact within one business day to get you going.

Ruby for Rails

Related posts

RubyConf Round Up

November 27, 2018

The City: Los Angeles, California

Read More

It's Almost Time For Ruby Conf 2018!

November 6, 2018

It’s autumn and November is right around the corner! We all know what this means…

Read More

Jekyll and Engine Yard: A Match Made in The Clouds

October 29, 2018

Blogging is without a doubt the most important way to get your ideas out to the world, whether

Read More

Christopher Rigor

Christopher Rigor is a Senior Technical Evangelist at Engine Yard. He’s a long time Rails user, system administrator, and recently became a contributor of RailsInstaller. Previously, he was the DevOps Support Manager for Asia-Pacific at Engine Yard.
Find me on:


Subscribe to our Blog