From: Phillip Gawlowski Date: 2008-04-29T17:06:55+09:00 Subject: Re: Array#first is not Array#[0] -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Fredrik wrote: | I made a class that is a subclass of Array : | | class MyClass < Array | def [](i) | "Here you go: #{super(i)}" | end | end | | and I thought that Array#first and Array#last was just syntactic sugar | that would translate to Array#[0] and Array#[-1] respectively. But | it's not: | | irb> g = MyClass.new([1,2,3]) | => [1, 2, 3] | irb> g[0] | => "Here you go: 1" | irb> g.first | => 1 | | Now, isn't that really, really crazy? Am I supposed to define | MyClass#first and MyClass#last separately??? C:\scripts>ruby temp.rb Here you go: 1 1 C:\scripts>cat temp.rb class MyClass < Array ~ def [](i) ~ "Here you go: #{super(i)}" ~ end end g = MyClass.new([1,2,3]) puts g[0] puts g.first C:\scripts>ruby -v ruby 1.8.6 (2008-03-03 patchlevel 114) [i386-mswin32] Hm.... - -- Phillip Gawlowski Twitter: twitter.com/cynicalryan Blog: http://justarubyist.blogspot.com ~ Calvin: Can you make a living playing silly games? His Dad: Actually, you can be among the most overpaid people on the planet. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkgW1xUACgkQbtAgaoJTgL8ozACfeThkZk1S/FDCSSYeSBEhog6V PA4AoIQJjVJlB092JSYBPAXA4jF7UYfE =c2AX -----END PGP SIGNATURE-----