Ruby Chronicles #1: Blocks and Yield

A “wtf” at lunch today while reading this book. There is a section on the 2 types of blocks in Ruby’s methods, and a description of the “yield” method. Totally jacked; it’s basically like sending an anonymous function in ActionScript seperate from a method’s parameters. The function then in turn runs that block of code for every yield statement. The yield statement can also pass parameters to this code block. Why you’d do this is beyond me, but when I yield, I’ll be using yield… imagine that?

For example, here’s Ruby calling a method, passing a block that the yield will run inside of the method:

def method
   puts "start of method"
   yield("this'll be arg1", "this'll be arg2")
   yield("this'll be arg1", "this'll be arg2")
   puts "end of method"

method {|arg1, arg2| puts arg1 + ", " + arg2}

It’ll output (I think):

start of method
this’ll be arg1, this’ll be arg2
this’ll be arg1, this’ll be arg2
end of method

The equivalent in ActionScript 1 is:

function method ( someFunc )
   trace("start of function");, "this'll be arg1", "this'll be arg2");, "this'll be arg1", "this'll be arg2");
   trace("end of function");

method(function(arg1, arg2){ trace(arg1 + ", " + arg2); });

Which outputs:

start of function
this’ll be arg1, this’ll be arg2
this’ll be arg1, this’ll be arg2
end of function

The only difference is the ActionScript example sends it as an argument whereas in Ruby, it appears to be sent as a seperate entity from the methods invocation parameters.

Don’t get me started on the Archeologist degree one needs to read printf statements…

Anyway, weird stuff.

5 Replies to “Ruby Chronicles #1: Blocks and Yield”

  1. Hey there.

    (disclaimer — I’m not a Ruby programmer)

    You’re right. Ruby blocks are similar to AS closures. What I like about the Ruby block syntax is that it makes it easy to define things that feel like part of the language. And the yield statement can make it easier to retain state as you pass control from one block to another, which is great for iterators.

    For example, let’s say you wanted to create an array class that has something like ‘foreach’ but only if the array value matches a certain value.

    class MyArray
       def eachMatchingIndex(obj)
          for i in 0..length
            if (self[i] == obj)
  2. Hmmm burning the midnight oil on ruby. Im loving it as a data source for flex2. But much like you I have found several things that make me go huh?

  3. RoR is pretty hot theses days. That’s good since Ruby has a lot of advantages over others dynamicly typed programming languages (like PHP). However it’s still dynamicly typed. Did you have a look at haXe Remoting Proxys ? They are a good way to do typeful communications between the Flash Client and the Server.

  4. Blocks made me go ‘huh?’ at first, too. However, I know some Java programmers that use them quite often. There is a difference between a block and a closure, though. There are some great examples of their uses later on in the Agile book (2nd edition coming soon) and in the Recipes book.

Comments are closed.