Sunday, September 18, 2005

Ruby blocks, C# delegates, C function pointers and Java inner classes

Ruby blocks, C# delegates, C function pointers and Java inner classes

During the weekend I spent some time to look in to the Ruby 'Blocks'. Blocks are a Ruby language construct which adopts the concept of method references in a very useful manner. Even though this is not a new idea I haven’t seen a language adapting and using method references to the extent as Ruby does. Let’s look at how Ruby adapts this concept. One of the Ruby’s powers is that it takes fewer lines of codes compared to most of the other languages. For an example in Ruby iterating on an array and printing the elements will look like the following:

array.each { |animal| puts animal }

Actually what happens here is, to the "Array.each" method we pass the code block "{ |animal| puts animal }" so “array.each” method will call the passed code block for each element of the array (more information). As u may see this is not exactly passing methods as references. Instead of passing an external method as a parameter, in Ruby we write our code inline as a code block.

Let’s look at some other languages to see the way they allow developers to pass code blocks as parameters of a method.

In C# we may gain the same power with Delegates. Delegate is a type that describes a method signature. Methods with the same signature can be stored in a delegate and can be passed as parameters or be invoked. But delegates are more structured, complex and requires more coding compared to Ruby blocks.

Function pointers of C allows developers to pass functions as parameters to other functions. They are not type safe as C# delegates so the developers are able to pass functions with different signatures as parameters.

Java has no concept called method references and it does that using interfaces and classes. We need to define our code block inside a class and pass that class to the method as a parameter. For an example we use this in implementing listeners, callback routines, and to pass utility code blocks such as compare routines to ‘sorting’ algorithms. Mostly we see these code blocks are implemented as inner classes or anonymous inner classes in java.

If you are a regular reader of my blog you may remember the post “Intermediate Representations vs Software Performance”. There I have explained how to pass a code block to a DAO so that as and when the DAO encounters a database record it will invoke the passed code block. If you look at the way of Ruby’s “array.each” method works you can see both these implementations share the same concept to a some extent.

So I may say ruby has adopted the concept of passing code blocks to a method and made it a really useful construct in the language.

7 comments:

creed said...

In Ruby the block is tied to a single method. i.e it can be yielded within that method only. Is it the same behaviour with C# delegates and C function pointers where deletgate/function pointer can be passed only to a single method?

Hasith Yaggahavita said...

yea.. very valid point.. I'n general function pointers they are reusable. Same with java interfaces. But I guess Ruby blocks are not reusable but u can make them reusable easily by just simply calling a reusable function in side the block. So we may use it in either way..

88Pro said...

This might interest you.

http://www.intertwingly.net/blog/2005/04/18/Blocks-for-Box

Anonymous said...

Good design!
[url=http://qysbbioj.com/tznz/caqi.html]My homepage[/url] | [url=http://tqxiktut.com/nqnl/jtlz.html]Cool site[/url]

Anonymous said...

Nice site!
My homepage | Please visit

Anonymous said...

Good design!
http://qysbbioj.com/tznz/caqi.html | http://hjfnxplw.com/nkbj/goqq.html

Anonymous said...

Good design!
http://qysbbioj.com/tznz/caqi.html | http://hjfnxplw.com/nkbj/goqq.html