All Posts in “metaprogramming”

Ruby introspection 2

I wanted to start blogging on ruby reflection api, but i just realized that i have to give a second part of my previous article on ruby introspection .
So here we go:

Also i strongly recommend that you look at the ObjectSpace module which contains a number of routines that interact with the garbage collection facility and allow you to traverse all living objects with an iterator, however this is a little example taken from the official api documentation :

Also if you feel like you are eager to see low level introspection , then check this great post.

In my next article, am going to blog on ruby reflection api , hope to finish it soon 😉

Ruby dynamic method calling

I’m pretty sure that you have heard lots about ruby, specially as being a dynamic language, you can create methods on the fly, add instance variables, define constants and invoke existing methods dynamically , and that’s what this post is all about :

As you know in ruby you can call a public instance method directly ,ex :

One way to invoke a method dynamically in ruby is to send a message to the object :

A second way is instantiate a method object and then call it:

And the third way is to use the eval method:

Well, when to use what?

look at this script, it will be used to benchmark the 3 ways of calling :

Well as you can see, instantiating a method object is the fastest dynamic way in calling a method, also notice how slow using eval is.

Also when sending a message to an object , or when instantiating a method object , u can call private methods of that object :

Ruby introspection

Hi, this is my first blog post!, i’m already done of reading this artilce on groovy’s lang introspection , and i wanted to submit the equivalent one for ruby, so all you need now is to fire your irb and follow me 🙂 :

As for the Dynamic method calling introduced in that article , check this post 😉

*Update : Check the second part article of ruby introspection, for more info on this topic.