Thursday, March 23, 2006

Dynamic by nature

A few days ago I had a discussion with a friend about Ruby and its very dynamic nature (which I like a lot). He argued that with Reflection, ByteCode generation etc. you can do all the stuff Ruby does in Rails in Java too. Therefore he thinks it is just a matter of time until something equally cool as Rails will exist in the Java world. Well, I don’t think so…

Here we go. This little Ruby code snippet that doesn’t do anything much helpful but demonstrate a few things, that at least I don’t know how to emulate in Java (or C#, not to mention C++):


class Module
def method_added (id)
print "."
end
end

class C
print "1"
end

c=nil
i_am_a_string="c=C.new()"
eval i_am_a_string

class C
def x
print "2"
end
end
c.x

class C
def method_missing(i)
print "3"
end
end
c.a_not_existing_method


The output of this little Ruby script is of course “.1.2.3”. I don’t want to explain everything in there, but there is a callback method registered to get informed of any adding of methods to a class at runtime(!) which I do a little latter. It executes a String within the context of the main program and finally it deals with a call to an object that doesn’t have the method the caller thinks it has. Not that I say everything in there is necessary to do all the time (or should be), but the power is there. Therefore if you have to create a framework like Rails in Java, you might get one or the other problem ;-)

Ciao Alex

P.S.: Yes, I am aware that there are some "frameworks" out there that try to mimic
Ruby on Rails, but I very much doubt they can give the same "feel" Rails does.

P.P.S.: How can you post code snippets in Blogger with the correct indents? Any ideas?

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home