Part one: Understanding Ruby
Meta programming, who hasn’t heard of it these days? You see it everywhere, you perhaps do so your self without even knowing. But what exactly is this thing called Metaprogramming. In Ruby; it’s probably you best friend. And if it’s not, I’m pretty damn sure it will be! It’s hotter then your girlfriend or perhaps your boyfriend. Hell, it’s probably even hotter then your sister hottest girlfriend and that other girlfriend .. well you catch my drift! But in all seriousness it’s somewhat makes Ruby the awesome programming language as it is. But before we can actually use metaprogramming in language, you must understand the very basics of how Ruby works.
Metaprogramming in one sentence
Metaprogramming — Writing code that writes code.
Something which you have to understand about programming is that, mostly, or in some cases you try to make things a little easier for your self or perhaps others, no matter what language you program in. Take C for example; C is pretty close to machine code and thru machine code you try build abstract bridges from the silicon world to the more fluid word we inhabit. However the distance between those two world are rather large. But what if there were languages that were closer to application domain? Take Ruby for example, Ruby provides a much higher-level abstraction and lets you write code which is much closer to the target domain, which is why people consider higher-level languages. Who can blame them? Have you ever seen C code (I remember so well; I was so against high-level languages when I was doing C because it wasn’t cool enough)?
But when you metaprogram, you are no longer limited to the set of abstractions built to your programming language. Instead, you can create new abstractions that are integrated into the host language. In effect, you’re creating a new, domain-specific programming language — one that’s designed to let you express the concepts you need to solve your particular problem.
Programming Ruby 1.9 — The pragmatic Programmers’ Guide
Ruby actually makes metaprogramming rather easy. Most advanced Ruby programmers mostly use metaprogramming in order to simplify their code or more dynamic.
Meet the Classes and Objects
Ruby is mostly all about Classes and Objects. There are a lot of different kind of types: classes, objects, class objects, instance methods, class methods, singleton classes, and virtual classes. But actually, Ruby is just basic. It has only one single underlying class and object structure
A Ruby object is nothing simply nothing more than: a set of flags, instance variables, and an associated classes. A Ruby class is an object of class Class. It has a set of method definitions and a reference to a superclass, which is a class itself. That’s basically all there is to a Ruby class.
Me, self and I .. and Methods … and calling them, too
Something which you might not know about ruby is that it has the concept of current object. The current object in Ruby is a read-only variable and you’ve probably been using it a lot. The current object is referenced by self.
The current object is taking care of a couple things. First off it controls how Ruby calls methods. In Ruby, as you might know, all method calls are done on some object. The object is also called the receiver. Second; the current object also controls how Ruby finds instance variables. When you access a variable it’s looked up in the current object by Ruby.
For example, if you make a method calls such as puts “Hi there”, there’s no explicit receiver. In this case, Ruby looks for the method “puts” in the current object as the receiver. It goes to self’s class and looks up the method (“puts”). If it can’t find puts in self’s class it’s being looked up in the superclass and so on. But what happens is that if you specify an implicit receiver such as item.size the process is very similar as calling a method without an implicit receiver. The only change is — however a vital one — is that the current object is being changed, for the duration of the call. Before the method lookup self is set to the receiver by Ruby (the object which is being referenced by the method). Once the method returns self is set back to what it once was.
Here’s a simple program
class Test
def variable_test
@var = 100
two
end
def two
@var
end
end
test = Test.new
test.variable_test
This produces:
100
On the first line we create a new class of Test, assigning the return value, a new instance of Class Test, to test. On the second line we call variable_test with an implicit receiver which is test. When method variable_test is invoked, Ruby sets the current object to test. Inside variable_test we create a new variable @var and assign 100 to the newly created variable. @var Is created on the current object, and since the current object wasn’t changed since it’s set to instance test it will be created on instance test. After assigning @var method two is called, and since it has no explicit receiver it’s invoked on the current object, which is still our instance of class Test.
So now you know how Ruby looks up methods and assigns new variables. In the next in the Metaprogramming series I’ll discuss Class definitions, Singletons and probably also Inheritance. I hope you enjoyed the first one, and hopefully it has helped you a little bit in understanding the strange world of Ruby and Metaprogramming!
Tags: 1.9, metaprogramming, ruby, Ruby 1.9, ruby19

