All Posts Tagged “reflection”

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…..

Ruby reflection

If you are here, then most probably you want to know more about ruby reflection interface. Well that’s true, but I always find myself in need to explain few things before I get started with my posts, and this time I find myself in need of explaining few things related to ruby’s OOP.

I’m pretty sure that you always heard that “in ruby: everything is an object.” , but have you ever thought of that carefully?
The first thing that comes to a one’s mind is something like :

But have you thought of your class as an object? Well that seems odd, but that’s how ruby works:

What does the above snippet of code mean exactly?
It means 2 things : Foo is a constant and that constant holds(refers to) an object of Class type.

Let me prove that 2 you:

As you can see, I got a warning because I tried to initialize the constant Foo again.

So ,when you define some class ‘Foo’ in ruby, all you are doing is:
1-instantiating an object of type Class.
2-initializing a constant Foo that refers to that created object .

So bear in mind that when I say “object” ,then I do mean any object; an object of Class type, or any object of any type.

Now, let’s move to another point. What about the ‘Singleton Class’, did you have the chance to work with it before?
Simply it’s the class that holds all singleton methods of an object, whether it’s a class object , or any other object.

Let’s start by defining 2 class methods (a class method is nothing but: a singleton method of an object of Class type) :

You could also have written that in this way:

The above inner class mentioned with “class << self" is what we call : a singleton class. Let's define a method that returns back the singleton class for us :

Now try this :

As you can see, the singleton class is the class that holds all singleton methods. We will use the singleton class later in this series, so keep the concept in mind. Let's now get back to our topic on ruby's reflection api , i will introduce 3 methods in this post: eval , instance_eval and class_eval.

eval

‘eval’ is a method that evaluates a ruby string :

eval can also work in the scope of bindings ; a binding is an object that encapsulates the execution context at some particular place in the code and retain this context for future use. Look at this example of using bindings with eval :

Also can work with proc objects :

instance_eval

This method works in the context of the object :

And could be used to define singleton methods :

you can pass it a block instead of the string :

class_eval

Evaluates a string or a block in the context of the receiver

And it defines instance methods when called on some object

And as instance_eval, a block instead of the string could be passed

You can use class_eval to dynamically use private methods, for example to use the private method ‘include’:

Now let’s make use of our knowledge,let’s try to redefine the attr_accessor method in our way, I will make a similar method called attr_access :

in a similar way we can define class attribute accessors :

Or with we can use the singleton class :

And in both cases we can do :

Give it a try and try to define attr_reader and attr_writer for both object and class variables.

I think it’s enough for this post, the next post will contain more methods to look at. See you then.

Update 1: fixing some typos.
Update 2: second part is here.