0.0 Introducing Blocks

Diving into blocks

Blocks are often the first Ruby-ism that new Rubyists run into, and are confused by. What are blocks? Why do they exist? How do I use them?

I'll do my best to answer all these questions and more in this lesson.

What are blocks?

Blocks aren't unique to Ruby. The official (language agnostic) definition of blocks is "A section of code which is grouped together." Of course, I'm guessing this doesn't help you much.

A simpler way to describe blocks is “A block is code that you can store in a variable like any other object and run on demand.”

Let me help you build a mental model for this by showing you some code, then refactoring it to a block in Ruby. We can start by writing some code that does something trivial, but meaningful.

Maybe add two numbers?

Example Code:

Output Window

Sweet. That works! However, this only covers the first part of the definition - it's a section of code. It isn't "grouped together" though, nor is it stored in a variable.

Let's work on it some more to make it more generic before we "group it."

Example Code:

Output Window

Great. That works - we've replaced the numbers with variables. The code that does the addition, however, is still not stored in a variable.

Let's make it happen!

Example Code:

Output Window

And there you have it all nice and grouped - a block!

The `lambda` keyword is what is most commonly used to create a block in Ruby. There are other ways to do it, but lets keep things simple for now.

At this point if you're thinking "Wait, that looks pretty much like a method, except there's no class or object" then you're absolutely right. Try thinking of it like this: A block is like a method, but one that isn’t associated with any object.

Let’s examine blocks more closely.

Are blocks objects? Yes, like almost everything else in Ruby, they are.

Example Code:

Output Window

As you can see, the block we just created has an object_id, belongs to the class Proc (which is what a block is called in Ruby), which is itself a subclass of Object.

We can even flip the definition around and define methods in terms of blocks. A method is simply a block bound to an object, with access to the object's state.

Let me illustrate the idea by reverse engineering a Ruby method to produce a block. Here's a more traditional approach to the previous problem (and please forgive my crappy object modeling):

Example Code:

Output Window

There, that works as you'd expect. Now lets take it further.
Example Code:

Output Window

And there you have it - a regular, old fashioned method converted to a fancy-pants block!

It's your turn now! In the next section, we'll expand on this problem with an exercise that gets your feet wet.

Block your code!

Lets build four blocks one each for addition, subtraction, multiplication and division. Each block should accept two values, perform the operation and return the result.

We've already done addition for you, so feel free to use it as an example for the rest.

Output Window

Congratulations, guest!

% of the book completed


This lesson is Copyright © 2011-2024 by Jasim A Basheer