Its better for Rails developer to master Metaprogramming

Some may ask the question that weather its really necessary for a Rails  developer to know meta programming. Well to get started with Rails, meta programming is not that essential, but as you solve an application some times it might help to reduce a great deal of code.

Currently I am coding a Learning Management System. I wanted to give a good UI in which a users role could be changed between Admin, Teacher and Student. The UI looks like this:

At first the code needed to use it seems too big, but with meta programming I was able to write a helper that took the user and role (as symbol) and generated the label above. It also checked if the user was Admin, Teacher or Student and ticked the appropriate label accordingly!

Ofcourse it could have been done without meta programming, but with it it took only 5 lines of code to implement, rather than 20 to 30 lines that will otherwise take.

Yes you can code Rails apps without knowledge of metaprogramming, but if you know it, it would be of great great help and save you from writing lot of code. Why not exploit wonderful Ruby features when its there for us?

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: