Monday, November 27, 2006

barCamp Pune II

After successful barCamp Pune here comes second barCamp in Pune. Its on 16 - 17 Decemeber.

Please visit the following link for details like presentation, venue and of course of mentioning your T-shirt size :) if you are attending

barCamp Bangalore

After barCamp Delhi and barCamp Pune here comes barCamp Bangalore. Its on 2 - 3 Dec 2006.

Refer to the following link for more details on Presentations, Topics, Venue etc.

Wednesday, November 22, 2006

Executing ruby scripts without Ruby installation

RubyScript2Exe is an interesting project that helps you to run ruby scripts on a machine that doesn't have Ruby installed on it. This projects creates standalone applications for Windows, Mac and Linux which can than be executed.

It collects all the files that are required to run the script on other machine: the Ruby script, Ruby interpreter and the Ruby run time library. Because these files are gathered from the local installation RubyScript2Exe creates an executable for the platform it run's on. No cross compile.

For more information checkout the RubyScript2Exe project.

Friday, November 10, 2006

Using reflection in Ruby

Reflection in Ruby is a great way of doing the things at runtime. Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.

One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.

In Ruby using reflection you can get the following information:
1, What all class currently exists?
2. Their methods information
3. Their class hierarchy and lot more

Let consider the above problem and try to find out the solution using Reflection.

Ruby provide a module called "ObjectSpace" that lets you to use reflection and see all the above mentioned information.

so if you say

ObjectSpace.each_object { |x| puts x }

It will print all living, nonimmediate objects in Ruby process.
If you specify the type of objects that you want then you can specify it as option to each_object method. So,

ObjectSpace.each_object(Class) { |x| puts x}

will print all the classes that are there in the Ruby process.

So now the above problem becomes simple.
Iterate over all the classes compare their name. If name matches then create object and execute whichever function you like.

So, the code looks like

class ClassFromString
@@counter = 0
def initialize
@@counter += 1
def getCounterValue
puts @@counter

def createClassFromString(classname)
ObjectSpace.each_object(Class) do |x|
if == classname
object =
object =

Once you get the object you can use any method of that object. For e.g. you can use superclass method to get the name of the parent class and so on and can build complete hierarchy dynamically.
You can get the information about methods of a given class using methods like private_methods(), protected_methods() which are defined in Object class which is base class for each object.

Reflection is great thing but there is also some performance hit when you use Reflection.

Happy programming !!!