Ruby and Metaprogramming

According to wikipedia, Metaprogramming:

is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at runtime that would otherwise be done at compile time.

Well, nice to hear cause Ruby is a lang that supports Metaprogramming effectively, and might be the best in that field. If you want to explore more on that topic, and how ruby can help you, then check this list of posts i wrote recently, i hope you will enjoy them:

If you have any question regarding this topic, please don’t hesitate to contact me, i would really be pleased to help you.

Ruby and Internal DSLs

A Domain-specific language(DSL) is a computer language that’s targeted to a particular kind of problem, rather than a general purpose language that’s aimed at any kind of software problem. Domain specific languages have been talked about, and used for almost as long as computing has been done. Regular expressions and CSS are 2 examples of DSLs.

Any software language needs a parser and an interpreter(or compiler or a mix), but in DSLs, we have 2 types: external ones which need parsers and interpreters, and internal ones which rely on the hosting language power to give the feel of a particular language, and thus they don’t require their own parsers.

Ruby is a very convenient language for writing internal DSLs, it has several powerful techniques that enables you easily to write internal DSLs, and many famous products that we use are nothing but internal DSLs: Haml, Builder and Rake .

Lemme show you a very simple example on how an internal DSL might look like using ruby:

As you can tell, this is a very basic internal DSL, written to describe basic tasks for a robot.
There is one task at the moment called ‘stack’ where the robot should do 2 things: fetch the box, and then package it.
Several ruby techniques are used to bring this basic DSL:
1- Blocks: everything between ‘do .. end’ keywords.
2- Parenthesesless methods: like ‘lock’ and ‘seal’.
3- Passing hash values as method arguments without the need of using curly braces: like doing ‘pick :speed => ‘slow’,:height => 15′.

Now all i need is a simple functionality to execute this simple internal DSL:

And the output:

In addition to the 3 points we mentioned earlier, another 2 ones should be added, as they are the heart of the above executer:

4- Reflection techniques: the one we used inside the class method ‘start’, exactly this line “new.instance_eval(&block)”.
5- method_missing: all undefined methods are received by ‘execute’, the alias of method_missing.

As i mentioned earlier, this is a very basic internal DSL, if you are looking for an advanced article covering a more advanced one, then don’t hesitate to check this rich one by Daniel Spiewak.

Ruby callbacks

This blog post is about ruby’s callbacks(hooks): what are the available ones,and how practically we can use them?


obj.method_missing(symbol [, *args] ) => result might be the most famous hook in ruby, and is being used a lot by ruby developers :

Invoked by Ruby when obj is sent a message it cannot handle. symbol is the symbol for the method called, and args are any arguments that were passed to it. By default, the interpreter raises an error when this method is called. However, it is possible to override the method to provide more dynamic behavior.

Here is a simple example where the user can mix colors using the mix method :

Now, how about letting the user do something like: ‘c.greenAndBlue’ or ‘c.grayAndYellow’ ?, let’s see how to do so:


const_missing : invoked when a reference is made to an undefined constant.

I will use it to show the user a more informative message when he tries to use a non existing constant:

included and extended

included and extended are fired when the module is included or extended :

However let’s move to a more practical use, if u look at the module documentation, you will find the following definition:

A Module is a collection of methods and constants. The methods in a module may be instance methods or module methods. Instance methods appear as methods in a class when the module is included, module methods do not.

Well, as you can see, you can’t include the module methods inside your class, then what to do?
I saw some people use a nice trick to do so, they split their module into 2 inner modules, one called InstanceMethods and the other called SingletonMethods, then they include the former, and extend the later, look at the following snippet of code :

method_added and singleton_method_added

method_added and singleton_method_added are another 2 callbacks that are fired when a new instance or singleton method is added.
In the code snippet bellow, I’m trying to prevent a developer from monkey patching(reopen) my colors class:

method_removed and method_undefined

method_removed: fired when an instance method is removed.
method_undefined: fired when an instance method is undefined.

singleton_method_removed and singleton_method_undefined

singleton_method_removed: fired when a singleton method is removed.
singleton_method_undefined: fired when a singleton method is undefined.


inherited is called when some class is being inherited:

A practical use will be in preventing subclasses from being created :

Well, that’s all for this post. The next post series will be on writing internal DSLs using ruby.
See you then…..

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?


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’ 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 :


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 :


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.

Arabs who are interested in Merb and Merb-book

So many developers from various countries had corresponded to Matt Aimonetti’s article on open merb book, and an early version of the book is out there.

However lot’s of work is still needed in both directions: contribution and translation ,so: dear Arabic developers wherever you are, if you are interested in Merb framework, have the ability to write in native Arabic, and willing to help contributing or translating the merb-book , then please don’t hesitate to send me an email. Your efforts is gonna be highly appreciated by Merb community, and you personally gonna gain lot’s of knowledge in Merb framework, how to contribute to open source, and how to write a book.

Waiting to here from you…..

What i liked most about rails 2.2

Well, finally it’s there, many important improvements were added to rails 2.2, and yet new things are on the way, you can check this blog post to check what’s new, while i’m gonna list here what i really liked about the new version of rails 2.2:

Thread safe

If you really don’t know what it means, then most probably you want to check this post by Charles Nutter, however in short terms: threaded rails with a native threaded implementation of ruby (jruby in our case) will save your memory , make your app more scalable, and most importantly it will make the app deployment very easy, specially when glassfish gem or even with jetty-rails gem is used.

And you Just have to make sure your app is thread safe, follow this post by Pratik Naik.

Well documented

So many people complained about this point before, but this a fact is from past now 🙂 , just fire your terminal , go to ur app path, and run :

This will create doc/guides in your project root , with a comprehensive rails guide to learn from.

Internationalization or I18n

A support for i18n is there now, so  you don’t have to install any extra gem or plugin, a comprehensive demo is put there, just be aware that you don’t need to install the plugin when following the demo, cause it’s already there in rails 2.2 .

Also check the I18n screencast by Ryan Bates.

Performance + JRuby compatibility

  • Connection pooling is added in this version of rails , which will save the time of opening(open connection with the db server +authentication) and closing the connection for each http request, so now a pool of connections will be used to serve incomming http requests, instead of opening a new connection for each request.
  • There were efforts to enhance the erb system , which will affect the rendering process time.
  • Rails 2.2 is fully compatible with JRuby which is the fastest ruby implementation at the current moment, and that will give you better performance when JRuby is used ,specially that the JRuby team is continually and actively(a release every 3-4 weeks) working on optimizing the overall performance and for rails usage as well.
  • Memoization term was introduced to cache values, check this screencast by Ryan Bates.
  • ETags and saving your server the hassle : if u don’t know about ETags then it’s time for that , check this post by  Ryan Daigle.What matters here is that the new support from rails to ETags  will give you 2 enhancements :
    1. Frontend enhancement: the client browser will use it’s cache to render the needed data and thus won’t need to issue a new request to your server to bring the data, thus a faster rendering on client side.
    2. Backend enhancement: your ror servers won’t need to handle some logic to respond back to your client browser requests, thus free the processor to handle other logic.
  • Template views will be cached now for production mode.

Caching actions without their layouts

Now it’s possible to cache an action without it’s corresponding layout:

This is really cool, cause in many cases our layout will contain some info related to user status in the website.

More additions might be very interseting for you

As for me, those enhancements and additions listed above , where the most benifitical for my work, other things might attract your attention, for more information about what’s new, i strongly urge you to look at Ruby on Rails 2.2 PDF by By Carlos Brando.

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 😉

Changing database encoding from latin1 to UTF8

Now a days, UTF-8 is the most used data encoding format, and the fact that your database is not using UTF8 encoding is really annoying, specially additionally when it comes to integrating different systems, that has no one unified encoding format.
So if you think it’s time to change your data encoding to utf8 format, then here what this post is all about.
I’ll list here the steps to do so, i just have to clarify that the main data encoding here is windows-1256 (which is the main Arabic encoding used in web applications), but it’s saved in latin1 encoding in the database (mydata ->windows-1256 -> latin1) ,also note that i’m using Mysql database.
Here are the steps:

  1. Export (only) the schema of the db,without “set Names” phrase in the outputted sql file, this will bring you back the data in the original encoding (windows-1256)
  2. Export the data of the db without “set Names” phrase in the outputted sql file, this will bring you back the data in the original encoding (windows-1256):
  3. Change the encoding of both files from arabic to utf8 -check the notes if you r using windows
  4. Open the file ‘db_name_schema.sql’ with any editor and replace each “DEFAULT CHARSET=latin1” phrase with “DEFAULT CHARSET=utf8” one
  5. Make a new db ,encoded in utf8:
  6. Import the schema and data in utf8 encoding:


  • If you are wondering why to separate schema from data upon exporting , then the answer is that the operation of replacing “DEFAULT CHARSET=latin1” phrase with “DEFAULT CHARSET=utf8” one , is taking place only on schema files, so it’s recommended to separate them so that you dont stuck when loading the big data files.
  • If you are a windows user and can’t use iconv , then u can use any editor to do the job for u, try scite or note++ or even dreamweaver


Grails and Arabic localization

A little problem that i faced with grails , was setting the locale dynamically, i followed this nice article , and here is how i could manage it :

  1. I made a new file called that contains the arabic localization under the i18n folder.
  2. I made a simple beforeinterceptor:

Of course you can use grails/Filters to save yourself repeating the same code in every controller.

However what i want to mention is the locale object, which in case of Arabic localization, was seeking the country argument ( “JO” in our case) :

While for other localizations like German one it doesn’t seek this second argument, so this snippet of code will work for them 🙂

Also you can change this country argument value to match this rule :

The country argument is a valid ISO Country Code. These codes are the upper-case, two-letter codes as defined by ISO-3166. You can find a full list of these codes at a number of sites, such as:

I hope that was helpful 🙂 , enjoy!