Freitag, November 11, 2005

Ruby rocks

First, let me show some code:
require 'magic'

a = Person.new
a.first_name = "Daniel"
a.last_name = "Mitterdorfer"

puts "My name is #{a.first_name} #{a.last_name}."

Well, that's not that interesting, but what if I tell you that I have not defined #first_name and #last_name? To be honest, there is even no definition of Person at all.

How does it work? The code that makes this example work is in magic.rb. It utilizes Module#contant_missing to define a proxy for missing classes and Proxy#method_missing for the dynamic definition of methods. Unfortunately, magic.rb is, - hmm - let's say 'quite in progress' and not very sophisticated yet. However, with a more sophisticated version of magic.rb I could probably write a DSL for the insertion of arbitrary records into a database or an O/R mapping layer that does not need any classes. Stay tuned.

1 Comments:

Anonymous Anonym said...

I just remebered a quote from some MTD friends of mine after I showed them that Eclipse can create your getters and setters:
But I don't want Eclipse to do that! That's the only thing I can write on my own!

;)

8:47 PM  

Kommentar veröffentlichen

<< Home