# Example Ruby Code

**URL:** <https://www.ruby-forum.com/t/example-ruby-code/179952>\
**Category:** Ruby\
**Created:** [December 8, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/example-ruby-code/179952 "2009-12-08T18:30:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [December 8, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/example-ruby-code/179952/1 "2009-12-08T18:30:56Z")

</div>

Can anybody explain how this code actually works in simple terms if  
possible :).

def animals  
yield “Tiger”  
yield “Giraffe”  
end

animals { |x| puts “Hello, #{x}” }

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [December 8, 2009, 6:39pm UTC](https://www.ruby-forum.com/t/example-ruby-code/179952/2 "2009-12-08T18:39:21Z")

</div>

Christopher D. wrote:

> Can anybody explain how this code actually works in simple terms if  
> possible :).
> 
> def animals  
> yield “Tiger”  
> yield “Giraffe”  
> end
> 
> animals { |x| puts “Hello, #{x}” }

Go read about blocks in Ruby. If you don’t understand something once  
you’ve done that, hopefully you’ll have a more specific question to  
ask…

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [December 8, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/example-ruby-code/179952/3 "2009-12-08T18:57:33Z")

</div>

On 08.12.2009 18:30, Christopher D. wrote:

> Can anybody explain how this code actually works in simple terms if  
> possible :).
> 
> def animals  
> yield “Tiger”  
> yield “Giraffe”  
> end
> 
> animals { |x| puts “Hello, #{x}” }

The code defines a method “animals” which accepts an anonymous callback  
function (the block). That callback is invoked via “yield” and  
arguments to “yield” are passed to the block.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [December 8, 2009, 10:03pm UTC](https://www.ruby-forum.com/t/example-ruby-code/179952/4 "2009-12-08T22:03:00Z")

</div>

Marnen Laibow-Koser wrote:

> Christopher D. wrote:
> 
> > Can anybody explain how this code actually works in simple terms if  
> > possible :).
> > 
> > def animals  
> > yield “Tiger”  
> > yield “Giraffe”  
> > end
> > 
> > animals { |x| puts “Hello, #{x}” }
> 
> Go read about blocks in Ruby. If you don’t understand something once  
> you’ve done that, hopefully you’ll have a more specific question to  
> ask…
> 
> Best,  
> –Â  
> MarnenÂ Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

I read about Blocks and thought I understood this quite well, I  
understand how this is working nearly, the only thing I don’t understand  
is how the |x| can be calling the defined yields earlier? Is |x| a  
special scenario?

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [December 8, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/example-ruby-code/179952/5 "2009-12-08T19:50:49Z")

</div>

Hi,

Am Mittwoch, 09. Dez 2009, 02:30:58 +0900 schrieb Christopher D.:

> Can anybody explain how this code actually works in simple terms if  
> possible :).
> 
> def animals  
> yield “Tiger”  
> yield “Giraffe”  
> end
> 
> animals { |x| puts “Hello, #{x}” }

That’s short for

def animals &block  
block.call “Tiger”  
block.call “Giraffe”  
end

b = proc { |x| puts “Hello, #{x}” }  
animals &b

Bertram

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [December 8, 2009, 10:07pm UTC](https://www.ruby-forum.com/t/example-ruby-code/179952/6 "2009-12-08T22:07:12Z")

</div>

On Dec 8, 10:30 am, Christopher D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Can anybody explain how this code actually works in simple terms if  
> possible :).
> 
> def animals  
> yield “Tiger”  
> yield “Giraffe”  
> end
> 
> animals { |x| puts “Hello, #{x}” }

In case you know JavaScript, and are comfortable with functions as  
first-class values, you might write this as:

function animals( doSomething )  
{  
doSomething( “Tiger” );  
doSomething( “Giraffe” );  
}

animals( function(x){ console.log( “Hello, %s”, x ) } );

//or more explicitly  
var myLogger = function(x){  
console.log( “Hello, %s”, x );  
}  
animals( myLogger );

As Robert said, the notation for “blocks” in Ruby defines an anonymous  
callback, and yield invokes it. As Bertram points out, there is an  
alternate way of defining a method so that the block is explicitly  
associated with a variable.

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [December 9, 2009, 12:19am UTC](https://www.ruby-forum.com/t/example-ruby-code/179952/7 "2009-12-09T00:19:28Z")

</div>

On Tuesday 08 December 2009 03:03:18 pm Christopher D. wrote:

> > > animals { |x| puts “Hello, #{x}” }
> > 
> > Go read about blocks in Ruby. If you don’t understand something once  
> > you’ve done that, hopefully you’ll have a more specific question to  
> > ask…
> 
> I read about Blocks and thought I understood this quite well, I  
> understand how this is working nearly, the only thing I don’t understand  
> is how the |x| can be calling the defined yields earlier? Is |x| a  
> special scenario?

No, that’s not what’s happening.

If you understand the Javascript example Phrogz gave, go with that. I’m  
going  
to try an example in Ruby…

A block is a block of code. It’s like a function or a method, only it’s  
not  
tied to anything yet. Let’s rework this example. I’m going to show you a  
step-  
by-step reworking, from stuff I’m guessing everyone already knows, to  
this.  
Since I have no idea how much you know, I’m not going to explain too  
much, so  
stop me where you get confused:

def say\_hello(x)  
puts “Hello, #{x}”  
end

def animals  
say\_hello “Tiger”  
say\_hello “Giraffe”  
end

Just a simple method. If you understand how animals works, you should  
understand how say\_hello works in the above.

SayHello = proc {|x|  
puts “Hello, #{x}”  
}

def animals  
SayHello.call “Tiger”  
SayHello.call “Giraffe”  
end

Here, SayHello is just a global constant which holds that proc. You can  
see  
where it’s kind of like a method, but not really. You can’t just do  
SayHello(“Tiger”), you have to do SayHello.call(“Tiger”).

But if you know what constants are, you know it can also be a variable:

def animals(block)  
block.call “Tiger”  
block.call “Giraffe”  
end

say\_hello = proc {|x|  
puts “Hello, #{x}”  
}

animals(say\_hello)

That’s still doing the same thing, but using a variable instead of a  
constant.  
And of course, you could do that last part shorter:

say\_hello = proc {|x| puts “Hello, #{x}” }  
animals(say\_hello)

And why do you need to assign it to a variable? You can just pass it in:

animals(proc {|x| puts “Hello, #{x}” })

So where are we?

def animals(block)  
block.call “Tiger”  
block.call “Giraffe”  
end

animals(proc {|x| puts “Hello, #{x}” })

Ruby actually has a little bit of syntactic sugar here, which Bertram  
Scharpf  
showed you. Let’s take a step back:

def animals(block)  
block.call “Tiger”  
block.call “Giraffe”  
end

say\_hello = proc {|x| puts “Hello, #{x}” }  
animals(say\_hello)

Now, it turns out that the following is almost exactly the same:

def animals(&block)  
block.call “Tiger”  
block.call “Giraffe”  
end

say\_hello = proc {|x| puts “Hello, #{x}” }  
animals(&say\_hello)

The difference is, that ampersand is telling Ruby that this is _the_  
block for  
the animals function. That means you can make it a lot shorter:

def animals  
yield “Tiger”  
yield “Giraffe”  
end

say\_hello = proc {|x| puts “Hello, #{x}” }  
animals(&say\_hello)

You’re almost there – see, those yields are just block.call. And the  
block is  
still there inside animals, it’s just invisible. And the final step is  
just:

def animals  
yield “Tiger”  
yield “Giraffe”  
end

animals {|x| puts “Hello, #{x}” }

See, when you call animals that way, it uses that block as the default  
block.  
And when you call yield, you’re calling the default block.

Now, I lied. It’ll actually be a lambda, not a proc. But proc is close  
enough,  
it makes sense here, and it’s shorter to write.
