F
Fredrik
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???
Best regards,
Fredrik
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???
Best regards,
Fredrik