Archive for January, 2006

Ruby Blocks 101

One of the difficulties you might have with jumping into Ruby on Rails is that you need to learn the Ruby language (yes, you do 😉 ). And if you come from the PHP background as I am, the single weirdest thing in Ruby would be the notion of blocks. I have never seen the blocks in any other language. The concept seems beautiful, but completely unrelated to anything I know about programming.

The concept of blocks is logical and intuitive, but most likely very different from the way you experienced programming. Blocks are like alien logic – something so basic, that it changes the meaning of everything. Think of how an amphibian race of super-intellectual beings would differ from humans – this is how different the blocks concept is from your PHP functions.

As blocks are the basic structures of Ruby, chances are you have already seen them and did not really understand what was going on. I hope this post will be able to help you out with getting the hang of the concept really quickly.

So let’s start with an easy example. Run irb (cmd irb.bat) and define the method:

def aliens
i = 1
j = 2
yield(i, j)

This method just assigns two variables and then passes those variables to the block. Now let’s call the block:

aliens { |x, y| puts x, y }

It works as follows: aliens method is called, with Ruby recognizing that this method has a block attached to it. Once the yield is encountered within the aliens, it is passed to the block together with any parameters in the brackets. As i and j are local variables, we need to define the first and second variable to be used to hold i and j values. Finally we have the Ruby code that does something with those parameters. Puts just displays the values of i and j on screen. But you can do something more complicated:

aliens { |x, y| puts x+y*y }

This should display 5.

If you are not confused at this moment, great – you have almost mastered the blocks ;). If you are, think about the aliens as a function that allows three parameters – x, y and some arbitrarily complex Ruby code. Yield is the place where this code is executed. To make sure that this is the case, try this: (copy the code, save it to blocks.rb in your project directory and run ruby blocks.rb)

def aliens
i = 1
j = 2
yield(i, j)
i = “magick”
return nil

aliens { |x, y| puts x }

The script should print “1”. This is easy stuff. (Pedantic note. You can ignore “return nil“, it is there because Ruby returns the last value in the function (=method) as the result of the function, so if you run the code above in irb without “return nil”, you will see that aliens actually returns “magick”)

Another cool feature is the ability to test whether the method has a block. Check out this code:

def aliens
if block_given?
  i = 1
  j = 2
  yield(i, j)
 else puts “magick”

aliens { |x, y| puts x }

In that case if the block is provided to the method, we execute the if statement, otherwise we execute the else statement. So, as a result the script should print “1” and “magick”.

How cool is that? 🙂

Updated in 2009:

Now that you know how blocks work, I would like to give you some implications for working with them in Ruby and Rails:

1 – It’s up to a method (or to method’s author) to have or not have a support for a block. If in the code above for method aliens there was no yield method, the block would just be ignored. If however, the yield is there and blockgiven? is not checked, you will receive an error – no block given on yield. A block is not a feature that you can use for any method any time with any parameters you can imagine. If a programmer thought of such a use for a block, you will have it, if he didn’t – you’re out of luck.

2 – It is up to the caller to understand what sort of blocks can be attached to a method. In the aliens example above, if you would try and call aliens { |x, y, z| puts x, y, z }, you will get the first two variables and nil for the third, and there will be no warning that you tried to get a variable (z), that is not really being passed (BUT – if you would try the other way round by having a block with only one variable x, Ruby will warn you and assign the merged value to a single variable x).

Also, consider an example when the information yielded is not really interchangeable. Let’s say that from my method I do an yield(x,y,zoom) for drawing a map. In my block I need to understand the correct sequence of variables in the yield and assign them correctly in my block. The only way to understand it is by looking up a documentation for this method, or sometimes more brutally – by going into the code and looking at the yield call.


So, what does this mean? This means, that blocks and yields are not magical, although they might seem so.It also means that sometimes in Rails it is very hard to understand which methods will accept a block and for those that accept the block what is the information passed to the block. However, usually there are no more than 2 variables being passed to a block, and you get used to working with the methods that way.

Hope it helps 😉

January 20, 2006 at 1:00 am 25 comments

Another good introduction article from Curt Hibbs

Curt Hibbs’ tutorials are the only accessible tutorials that I’ve read on Ruby on Rails. Curt has also written a great introductory article on Rails which you can find here.

January 16, 2006 at 7:51 pm 4 comments

Newbies beware!

So, I am trying to get deeper in the understanding of Ruby and Rails and things are progressing nicely. However, I have to say – the information on Rails and Ruby is not up to notch. Unless you are a seasoned programmer or a smart hacker, chances are you will be lost when trying to understand how to use the Rails API or what exactly you are supposed to do after you have installed Ruby on Rails and did an ONLamp tutorial. A good sentiment that every newbie will recognize instantly is expressed here.

January 16, 2006 at 7:48 pm Leave a comment

TextMate v1.5 released

TextMate by the look of it is a great editor for Ruby developers. New version was released just recently, but there are still no plans to release a PC version – only Mac users need to celebrate.

January 10, 2006 at 9:13 pm 1 comment

Ruby on Rails Cheatsheet

Blaine Kendall released a very nice 14-page PDF that summarizes nicely gotchas, syntax and some basic building blocks of both Ruby and Rails. Get the pdf from his blog post here.

January 10, 2006 at 9:01 pm 1 comment

Installing Rails on Windows (step-by-step tutorial)

20 July 09 Note – The new version of the Rails Windows installation guide is there. And the great news is that I even did it on the Rails v.2.3.3, released today! Please follow the instructions in this new guide, they are the most up to date ones!

28 June 09 Note – If you are new to Ruby / Rails, you should also get yourself acquainted with the Ruby syntax here.

Ok, so this will basically be somewhat a repeat of the information made by Curt Hibbs in this great hands-on tutorial. However, the versions of all products changed from the time Curt made his tutorial, and in some areas I felt that additional description was required. So, in this tutorial you’ll get a step-by-step instructions on installing Rails on Windows 2000 Server (Windows XP would be very similar).


January 9, 2006 at 9:33 pm 204 comments

Do you need instant rails?

I have written about the Instantrails package before, but having tried it for a day, I decided to go with a proper installation. The reason for this is that I’ve never used to run either MySQL or Apache on my local computer, and as I have all sorts of firewalls and quirks on my computer, when things went wrong, I did not understand how I could fix them. Instantrails does not have any real documentation and I was not sure what exactly went wrong.

Also, Instantrails comes with phpmyadmin for MySQL admin, and for this it also needs to install php, which I thought was excessive – I’d rather go with my old trusted MySQL Control Center or the new Query Browser. So, if you are not a power user of either MySQL or Apache, I would recommend against using InstantRails.

Finally, for development you do not need to install Apache, as Rails comes with WEBrick, which should do the job good enough.

January 8, 2006 at 4:41 pm 1 comment

Older Posts

Recent posts

Starting to learn Rails?


Get Kindle - the best e-book reader, that I personally use, and the only one that you can read on the beach - very useful: Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation)