Wednesday, August 10, 2011

Metaprogramming in Ruby - Basic

What's metaprogramming?

It's writing code that manipulates language constructs at runtime. language construct could be classes, modules and instance variable,etc.

In Ruby, we can write new code at runtime and execute the new code without restarting the program.

Example:

add custom method in Ruby classes, this just an example, no any specific purpose for this custom method

class Array
def customsize
   self.size + 1
end
end
puts [1, 2, 4, 2].customsize

Be careful before you do any changes in open ruby class. It may end up with bugs.

No comments:

Post a Comment