This morning's I went to a session called 'Ruby's Secret Sauce: Metaprogramming'. It was interactive and so I had my first opportunity to try out some of the neat things you can do in Ruby. Here are a few things that stuck out for me:
method_missing
DoThis()
DoThis2()
We also got to see the ActiveRecord class from Rails which does a ton of very cool things for you behind the scenes. Instead of having to (a) write SQL to create a table, (b) create a class to wrap the database, and (c) write find methods for various critiera, you can just derive your class, say Person, from ActiveRecord, and immediately get CRUD and find functionality for free. The sample class (taken directly from the presentation) looks like this:
class Person << ActiveRecord::Base has_one :address validates_length_of :first_name, :last_name, :on => :create, :minimum => 1 validates_length_of :ssn, :on => :create, :minimum => 9, :maximum => 9 validates_presence_of :first_name, :last_name, :ssn, :on => :create validates_uniqueness_of :ssnend
and you can then write code like this and it just works:
a_person = Person.find_by_ssn “123456789”smiths = Person.find_by_last_name “Smith”john_smiths = Person.find_by_first_name_and_last_name(“John”, “Smith”)
Obviously there's a lot going on behind the scenes there, and I'm not saying you want to do this for everything, but it became obvious quite quickly why Ruby is a prime candidate for writing DSLs.