Metaprogramming in Rails

Meta-programming means your programming language is writing code for you. And for sure Ruby is a great language having all cool features.Rails being a great ruby framework encashes all features of ruby. Rails itself uses meta-programming every where.

For example in ActiveRecord, we have dynamic method on attributes name like

find_by_something(some_possible_values) 
find_all_by_something(spv) 
find_by_something_and_otherthing(some_value, other_value)

In this topic we will see how we can meta-program in Rails. Using Meta Programming one can reduce LOC to dramatic level and it also reduce headache in maintaining code base.

Here I am not going into details for class_eval and instanse_eval because there are many blog post out there. But I do give some examples.

Consider following:

Let me explain that class_eval evaluates a string or code block in context of class or module it is called upon.

instance_eval can evaluate a string or a code block in the context of the receiver.

The third way to define a method in class or module is define_method.

Now we can spice our project with metaprogramming. See how? Suppose in our project we had a model to maintain users

We also have a Role model that belongs to user. Schema for role is suppose

Role(id: integer, name: string, user_id: integer)

At the initiation of project the role known were admin and user. So I have defined two method in User model named as is_admin? and is_user?. But after some time we need to add some other roles as we need to define same function for them as well.

So to get rid of this we flavoured our project with bits of metaprogramming. See here how