From: Phrogz Date: 2009-12-09T06:05:20+09:00 Subject: Re: Example Ruby Code On Dec 8, 10:30 am, Christopher Davidson 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.