How To Understand And Use The Ruby Yield Keyword Within Ten Minutes

George Gbenle
4 min readFeb 15, 2021

This article was first published on www.codedli.com

I had a pretty hard time as a beginner in understanding the concept of “yield” in Ruby. It took me about a week and few days to grasp the keyword and its usage in real code.

In a lay man’s term, I’ll attempt to explain what yield is, how important it is and how to use it. By the end of this short article, you should be fully set and ready to start yielding the yieldable.

Ruby Block

To fully grasp the idea of yield, we need to first understand what a code block is. In Ruby, blocks are like methods but without a name. Yeah, I get it. We have not explained what methods are too. A method in Ruby (referred to as functions in other languages) is a series of expressions that returns a value. With methods, a developer can organize codes into smaller chunks that can be easily called from other segments of the program. I’ll do a more detailed coverage of methods in a separate post. For now, let’s make do with this explanation.

Now, back to blocks. Consider a block as a method without a name. A single line block uses curly braces as below:

Given an array. array = [1, 2, 3]
array.each { |i| puts i + 1 }

This outputs 2, 3, 4.

A multi-line block looks like this:

Given an array.

array = [1, 2, 3]
array.each do |i|
puts i + 1
end

This also outputs 2, 3, 4.

Now, if I wanted to define a method that adds 1 to a series of numbers in an array just like the above, I’ll do something like this:

def add
array = [1, 2, 3]
array.each do |i|
puts i + 1
end
end

add

What I have done here is defined a method called “add” and just simply copied and pasted my code block from earlier into the method. By just calling the method “add”, we’ll get the same output 2, 3, 4 as always.

Ruby Yield

Now that we’ve cleared the air (at least, a little) on what blocks and methods are (you now have an idea), we can explore the power of the yield keyword. Before we apply it to our example above, Let’s take a look at an even more simplified example.

def add
puts "This the method"
yield
puts "Welcome back into the method"
end

add {puts "This is the block"}

This outputs :

This the method
This is the block
Welcome back into the method

Quickly, let’s take a look at what’s happening here. I defined a method with the name “add” just as I did earlier with a couple of puts statements inside the method. You’ll notice the keyword “yield” nutmegged in between the two puts statements and the last puts statement outside the method just after I called the “add” method. I am yielding the puts statement (puts “This is the block”) to the “add” method.

If I replaced the put statement (puts “This is the block”) with my initial code block with the array above, it’ll do the same thing. The block will return its value to the method. Look at it in action below:

def add
puts "You are in the method"
yield
puts "You are again back to the method"
end

array = [1, 2, 3]
add {array.each do |i|
puts i + 1
end}

This outputs:

You are in the method
2
3
4
You are again back to the method

The whole essence of using the yield keyword is that I don’t have to repeat that code again and again in multiple methods that use the same code block as part of its functionalities. I can simply yield the block to my desired method anywhere in my program.

Ruby Yield With Parameters

We can give our yield keyword arguments in a method while defining the parameters in the block. In a nutshell, any parameter in the block is capable of being inherited by the yield keyword. Let’s take a look at the example below:

def me
puts "This is the method"
yield("George", 75)
end

me do |name, age|
puts "I am #{name} and I'm #{age} years"
end

It outputs:

This is the method
I am George and I’m 75 years.

Of course, I’m not 75 years old. But if you looked closely, I used parameters “name” and “age” in the block and specified the values as arguments in the method. As simple as that.

Local Jump Error in Ruby

What happens if you use the yield keyword without a code block? You’ll get a local jump error like below:

def me
puts "This is the method"
yield("George", 75)
end

me

You’ll get an output that looks like this:

This is the method
Traceback (most recent call last):
1: from yield.rb:6:in <main>' yield.rb:3:inme’: no block given (yield) (LocalJumpError)

This is telling you that no block was given to be yielded.

There you have it. If you do not understand this at first read, or anything coding related at all, don’t beat yourself, give it time. Remember, you’re probably seeing this for the first time and may need time to assimilate and internalize the concept.

If you find yourself struggling to understand any concept, take a break, take a cup of coffee, take a walk, see a movie or do anything that tickles your fancy. You can then come back to it with fresh eyes and refreshed mind. I studied long hours trying to understand this concept without understanding. The articles and tutorials I saw made it look so simple and that added to my frustration.

See, coding is hard! Experienced developers make it look so easy to beginners and that’s often unfair. Once a beginner realizes they are not understanding a concept as easy as an experienced coder makes it look, the frustration starts piling up. A lot of beginners have given just by this singular factor. I’ve heard words like, “Well, coding is not just for me”.

I trust that you’re not going to quit. Understand that coding can be sometimes abstract which makes it a relatively difficult subject. Stick with it, your efforts will eventually pay off.

--

--

George Gbenle

An ex-banker turned Software Engineer. Loves writing and attempts to solve everyday problems with simple software solutions. https://twitter.com/GeorgeShammar