CSC/ECE 517 Fall 2012/ch1b 1w60 ac

From PG_Wiki
Jump to: navigation, search


SaaS - 3.8 yield()

Introduction (Preface)

In order to properly understand and cover the idea of how the Ruby yield function works, it is necessary to understand a few concepts that do not exist in other object oriented languages.

Code Blocks & Closures

Unlike Java (and several other object oriented languages) you can not only pass values and references, but you can also pass blocks of code as parameters in Ruby. A block of code that is both passable and can maintain all variables in it’s original scope, is called a closure. Here is an example of using a code block:

codeblock = {puts "foo"}

Executing this code will print:


This is a very simple example, but it shows how code blocks can be defined, stored into a variable and called later. This is very important to understanding what the yield function does.

An unbound closure, meaning the body of a closure itself, is referred to as a code block. A code block gives a shortcut, without explicitly creating an object, to pass around a block of code to be executed. This enables a programmer to quickly pass a block of code that they might want to implement across several objects or at several points of another function.

The Map & Grep Functions

A great example of code blocks being used is in the map and grep functions. The Map function allows you to apply a function to every element of a list or collection, independent of the collection's type. A new collection is created based on the execution of the block on every element. An example would be:

# This code example subtracts 2 from every element of the list
mylist ={|x| x-2}

The Grep function allows you to search a collection for all elements that match a given criteria. Then a new collection is returned containing only those elements that match. An example would be.

# This code example returns all elements less than 5
arr = arr.grep(|x| x<5) 

The way in which these two functions work depends on being able to run a block of code across several objects without ever examining the objects themselves. In Ruby, rather than iterating through a series of elements and calling an operation on them, we can pass a code block as an argument to a function that will then perform that code block whenever the correct conditions are satisfied.

The yield() Function

What Is It?

Simply put, the yield function in Ruby passes control to a user-defined code block. As simple as that statement is, it can be quite confusing, so here is a quick example[1]:

def test
   puts "You are in the method"
   puts "You are again back to the method"
test {puts "You are in the block"}

Executing this code results in:

You are in the method
You are in the block
You are again back to the method
You are in the block

First, take a look at the last line of code. You'll notice that the parameter to the test function is actually a code block. Next, look at where the text "You are in the block" appears: after the "You are in the method" text then again after the "You are again back to the method" text. Notice how this corresponds to where the yield statements are in the code? That's because the yield statement is calling the code block, then returning control to the test method.


As you may have guessed from the previous example, the yield function's syntax is quite simple. In it's most basic form, the yield function can be called with:


In this form, the yield function will simply execute the code block passed to the function it's called from. You can also pass parameters to the yield function which passes those parameters to the code block, like so:

yield parameter

Passing parameters allows you to call the same code block, but with different input. For example[1]:

def test
   yield 5
   puts "You are in the method test"
   yield 100
test {|i| puts "You are in the block #{i}"}

Executing this code results in:

You are in the block 5
You are in the method test
You are in the block 100

See how the same code block results in different output yielded to the first and second times? That's because the values 5 and 10 are passed each time, respectively, and output by the code block.

Using yield for Iterating

The SaaS/Coursera video[2] focuses on explaining the yield function as it relates to iterating over collections and explains it like so:

The idea of using a yield statement to execute a block of code on an object is opposite of the concept used in several other OO languages, such as Java. Yielding turns the concept of iterators inside out, so that rather than iterating through a list to perform actions, you can pass the action to the list to perform. The comparison is made to Java as follows:

Java: Give me each element of a collection - I want to do something to each one.

// List defined above
listIterator litr = l.listIterator(); 
while(litr.hasNext()) {
    Object element =;;

Ruby: Here is some code - do this to each element of this collection.

# List defined above
list.each { |x| }

This idea of sending a function to an object, rather than sending object to function creates (what some believe to be) a much more seamless and efficient style of code. In the example of iterators, Ruby both uses much less code, and enables the programmer to be completely abstracted as to how the list is iterated through. This prevents you from needing to define several functions that precede and follow your operations, and allows you to not have to expose methods to a namespace when calling a block of code on them.


Tree Traversal


class Tree
  attr_reader :value
  def initialize(value)
    @value = value
    @children = []
  def <<(value)
    subtree =
    @children << subtree
    return subtree
  def each
    yield value
    @children.each do |child_node|
      child_node.each { |e| yield e }

t =
... # code to populate the tree
t.each {|x| puts x}

In this example, the yield function is used to allow the tree owner to do whatever they want to each element of a tree. This example just prints the name of each node of the tree, but passing a more complex code block would let you do whatever you like with each child. In this example, we create a class that includes Enumerable, providing access to the each method, among others.

Random Sequence


class RandomSequence
  def initialize(limit,num)
    @limit,@num = limit,num
  def each
    @num.times { yield (rand * @limit).floor }

i = -1,4).each do |num|
  i = num if i < num

This example comes from the SaaS video[2] on the Ruby yield statement. In this example, the yield statement is used to execute the user defined code block for each random number the class generates. Each time each yields to the code block, i will be updated if the parameter to yield is the smallest value thus far. This is a great example of how passing a parameter to the yield function allows for concise code.



module Quantifier
  def any?
    each {|x| return true if yield x}

  def all?
    each {|x| return false if not yield x}

class Array
  include Quantifier

puts [1, 2, 3].any? {|x| x == 5}
# false
puts [4, 3, 4].all? {|x| x == 4}
# false

puts [1, 2, 3].any? {|x| x == 3}
puts [4, 4, 4].all? {|x| x == 4}

In this example, covered in the Lecture 5 slides, we combine our knowledge of how iterators work with yield, and create our own versions of these iterators. The module Quantifier is created, that allows a collection (such as Array), to test whether any or all of a set of properties are held true. By examining any, you can see that for every element in the collection, you the yield function is called and it return true if a single element returns true. The all method returns false, if any yield call returns false. This extension of each using yield demonstrates how extensible yield is for iterating across elements.


As has been demonstrated in the previous examples and explanations, yield allows programmers to pass instructions to objects in Ruby that can be called at any time as if they were parameters to a function. This ability for Ruby to treat any block of code as an object, rather than just instantiations of classes, demonstrates true object orientation. This, as well as many other unique elements of Ruby, create a terse and simple implementation of extensive functions.

See Also


  1. 1.0 1.1 - 2012
  2. 2.0 2.1 - 2012
  3. - 2008
  4. - 2012
  5. - 2010
Personal tools