Ruby reflection 2

This is the second post related to ruby’s reflection API, the previous post was an extensive intro to this topic. While the current one will be lighter somehow, it would require you to focus a bit more on the content.
Here we go:

Setting, getting and removing instance variables :

The code above could be written in a simpler way, let’s define another attribute ‘v’ for example :

Now, let’s undefine the instance variable ‘i’

Setting, getting and removing class variables :

As instance_variable_get and instance_variable_set, there are another variations applied to class variables, but unfortunately they are private in ruby 1.8, so let’s use class_eval to bypass that:

Setting, getting and removing constants :

How about constants?

define_method

Now let’s move to a new topic:
Define a method dynamically using define_method:
As the API tells :

Defines an instance method in the receiver. The method parameter can be a Proc or Method object. If a block is specified, it is used as the method body. This block is evaluated using instance_eval, a point that is tricky to demonstrate because define_method is private.

When using a block, the block params will be the method params:

Let’s use it to redefine the ‘attr_access’ in another way rather than the way that was defined in the previous post

One limitation to ‘define_method’ is that: it always creates instance methods, thus if we want to use it to define class methods, then we need to invoke it on the singleton class, let’s define the ‘cattr_access’ method:

Notice how i defined the singleton_class as a local variable, it could be defined using a method, just like what we did in the previous post, however, notice: how complex it became to use the define_method to define a class method, i wouldn’t encourage at all such a complexity.

Undefining methods

How about undefining methods? That can be accomplished in 2 ways: either by using the ‘undef’ statement, or using the private ‘undef_method’ method:

Alias chaining

One last thing that deserves mentioning here: is the ‘alias_method’, it’s being used to have alias chaining:
1- Copy the original method and give it an alias to be used later.
2- Create a new method with the same name of the original one, do whatever changes you need, and use the alias to invoke the original method somewhere inside the new one.
Before i show you the example i have to mention the ruby statement ‘alias’ which can be used as ‘alias_method’ :

I hope you liked this post, please don’t hesitate to notify me about hidden mistakes or suggest new stuff.
I’ll blog on using ruby’s hooks(callbacks) in the next blog post, stay in touch…..

10 Replies to “Ruby reflection 2”

  1. The last line of the 3rd code box should be:

    There is no accessor method named i on the object.

  2. In Ruby, how do you determine the type of parameters to a method that you want to invoke dynamically on an object? I know ruby is dynamically typed, but a method is implemented to work with certain type of parameters. If one wants to invoke a method dynamically using reflection, how do you go about finding this information so it is invoked with right type of parameters? One can not expect to know this information before hand if a class/module is going to be dynamically loaded. In Java or C#, you can get this information on parameters so you can supply the right type of values to a method that is invoked dynamically.

    Any suggestion? Thanks.

  3. whoah this blog is wonderful i like reading your articles. Keep up the good paintings! You realize, many people are searching round for this info, you could help them greatly.

  4. Coming prepared to these consultations can help smooth out this process and allow you to focus on this key question.. My parents were involved in a two car accident in FL where they were struck by car2. “Bringing e readers, surfing the net, talking to their friends and updating their social media.
    cheap toms shoes,cheap toms,cheap toms shoe,discount toms shoes,discount toms,toms shoes,buy toms,toms shoes for sale,cheap toms shoes for sale,toms shoes outlet,toms shoes for sale,toms shoes,toms shoes outlet,toms shoes for sale,toms shoes cheap,toms shoes online,toms shoes for sale,cheap toms shoes for sale,toms shoes for cheap,pre order toms shoes,pre order cheap toms shoes,cheap toms shoes,buy cheap toms shoes,authentic toms shoes,authentic cheap toms shoes,buy toms shoes,buy cheap toms shoes,order toms shoes,order cheap toms shoes,toms shoes online,buy toms shoes online,toms shoes 2015,cheap toms shoes 2015 http://www.arabtimes.com/books/cheaptomsshoes/

Leave a Reply

Your email address will not be published. Required fields are marked *