Gotta love ruby

July 29th, 2006

Show me a language where you can pull stunts like this in the same amount of code:


# Pretends to be someone else  
class Poser

  instance_methods.each do |m|  
    undef_method m unless m =~ /(^__|^nil\?|^send)/  
  end

  def initialize(args)  
    @target = args  
  end

  def method_missing (method, *args, &block)  
    puts "Long lookup operation (missing #{method})"  
    @target.send(method, *args, &block)  
  end

  def proxied_method  
    puts "Short proxied operation"  
    @target.size  
  end
 
end

p = Poser.new "Ruby Rocks"  
# => "Ruby Rocks"

p.class
# Long lookup operation (missing class)  
# => String

p.object_id  
# Long lookup operation (missing object_id)  
# => 263042

p.__id__  
# => 263032

p.proxied_method  
# Short proxied operation  
# => 10
 

Leave a comment