February 01, 2005

Definition-time, execution-time, anytime!

From "Programming Ruby", by David Thomas and Andrew Hunt:
The important thing to remember about Ruby is that there isn't a big difference between “compile time” and “runtime.” It's all the same. You can add code to a running process. You can redefine methods on the fly, change their scope from public to private, and so on. You can even alter basic types, such as Class and Object.

To this, I would like to add that Ruby blurs the distinction between definition-time and runtime. While definition-time may resemble, and frequently overlap, compile-time, there's a subtle difference.

In Java and other compiled languages, it is the compiler who interprets the class definition and produces an internal representation that the runtime portions will use. In contrast, in Ruby, it is the interpreter the one who does this. But more importantly, the class and method definitions are executed. As opposed to being parsed to extract the definitions before the code starts to execute.

In that sense, a class and method definition is part of the executable code, and the line between definition and actual code begins to fade.

It is this simple concept that enables Ruby to programs to create classes, add methods and attributes to classes and objects at runtime.

To illustrate these points, consider this piece of code:

c =
     class SomeClass
         attr :value
         self
     end


Before analyzing the code, remember that in Ruby every statement returns a value. In the case of a class definition, the value returned is the result of the last evaluated line.

When the Ruby interpreter sees this above code, it defines a class named SomeClass, creates an getter and setter for an attribute named value and assigns the result of evaluating self(i.e., a reference to SomeClass itself) to c.

And then we can also do the following:

obj = c.new
def obj.aMethod
     puts "Custom-made method"
end


This example reinforces the notion that definitions are executable code, and can intermingled arbitrarily with regular code(emphasized because in Ruby there's no such thing as regular code).

Executable definitions is a simple, but powerful concept. I'm still trying to understand all its implications and possibilities it opens.

Labels:

4 Comments:

Blogger Isaac Gouy said...

"In Java and other compiled languages"

Try this java flag
-Xint interpreted mode execution only

(Search hard enough on the web and you'll find C interpreters as well.)

February 02, 2005 8:16 AM  
Blogger Martin said...

Even when using Java -Xint, you still need to compile the classes into bytecode using javac.

Anyway, when I said "compiled" there I was referring to the fact that those languages have two distinct phases: interpreting the definitions (resolving dependencies, etc) and then executing the code. In Ruby, the definitions *are* the executable code.

February 02, 2005 9:17 AM  
Anonymous Anonymous said...

Yeah, the thing to really get hold of here is that classes in ruby are just objects, and a class definition is just a procedure that creates a new class object. It's a sort of syntax sugar to make a prototype based implimentation look like a class based language.

This example makes that very clear:

class Test; puts "inside the definition of test"; end
puts "below definition"
t = Test.new
puts t.class
puts t.class.class
puts t.class.class.class

February 05, 2005 6:06 PM  
Anonymous Anonymous said...

Nike shoes
Cheap nike shoes
Discount nike shoes
Nike shox r4
nike shox shoes
puma cat
cheap sport shoes
cheap nike shox
cheap nike max
nike tn dollar
nike running shoes
nike air max tn
puma shoes
discount puma shoes
puma mens shoes
puma running shoes
puma shoes
ed hardy clothes
ed hardy shirts
ed hardy jackets
ed hardy hoodies
ed hardy boots
ed hardy polo shirts
ed hardy shoes
ed hardy jeans
ed hardy outerwear
ed hardy long sleeve shirts
ed hardy bags
ed hardy winter boots
ed hardy handbags
ed hardy love kills slowly shirts
ed hardy love kills slowly shoes
ed hardy love kills slowly boots
ed hardy trousers
ed hardy mens
ed hardy womens
ed hardy t shirts
ed hardy sunglasses
ghd hair straighteners mk4
hair straightners
ghd iv styler hair straightener
ghd hair straightners
cheap ghd hair straighteners

December 09, 2009 5:02 PM  

Post a Comment

<< Home