Feeling at home in Ruby (if you're coming from C)

Posted by flori Fri, 03 Aug 2007 22:13:00 GMT

class Fixnum
  alias __oldref :[]

  def [](obj)
    if Fixnum === obj
      __oldref(obj)
    else
      obj[self]
    end
  end
end

if $0 == __FILE__
  2[0] # => 0
  2[1] # => 1
  a = [2,3,5,7,11]
  a[2] # => 5
  2[a] # => 5
end

Tags , , ,  | no comments