# Q: inheriting from Array, overloading \[\]

**URL:** <https://rubytalk.org/t/q-inheriting-from-array-overloading/19265>\
**Category:** ruby-talk\
**Created:** [30 June 2005 01:05 UTC](https://rubytalk.org/t/q-inheriting-from-array-overloading/19265 "2005-06-30T01:05:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_C](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Matt\_C](https://rubytalk.org/u/Matt_C)\
**Post date:** [30 June 2005 01:05 UTC](https://rubytalk.org/t/q-inheriting-from-array-overloading/19265/1 "2005-06-30T01:05:43Z")

</div>

New to Ruby. Probably my terminology will be a bit off.

In the Pickaxe book (first edition) they show a clever little trick with  
the SongList class, where you can use [] to pull a song from the list using  
either the integer index or the name of the song.

I wanted to try something like that, but with a subclass of Array rather  
than writing a wrapper around an array. I had the idea I might be able to  
use super:

class TalkerArray \< Array #will hold a collection of Talkers.

&nbsp;&nbsp;def [](key)  
&nbsp;&nbsp;&nbsp;&nbsp;if key.kind\_of?(Integer)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return super[key]  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.find{|t| [t.name](http://t.name) = key} #talkers have .name property.  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

end

but it didn't work (\*). I bet there is a simple way to make something like  
this work while inheriting from Array, though. Can anyone show me?

Thanks,  
Matt

\*: The error message I got was strange:

E:/matt/ruby/usr/ww/chat.rb:173:in `[]': undefined method `[]' for #  
\<Talker:0x2a5be68 @name="bob", @last\_msg=0\> (NoMethodError)

Why the heck is it looking at the Talker object? I must not understand  
what 'super' does. Anyway, I put this as a postscript because it's kind of  
a side issue for me. I'm guessing I don't really need 'super', or  
understanding 'super', to override [] the way I want to. So I can worry  
about that some other day. Which is not to say I won't listen if someone  
wants to explain, it's just not my priority of the moment. Ok, really  
done typing now.

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [30 June 2005 01:12 UTC](https://rubytalk.org/t/q-inheriting-from-array-overloading/19265/2 "2005-06-30T01:12:58Z")

</div>

Hi,

> **···**
>
> In message "Re: Q: inheriting from Array, overloading " on Thu, 30 Jun 2005 10:05:43 +0900, Matt C \<canimal@invalid.com\> writes:
> 
> > class TalkerArray \< Array #will hold a collection of Talkers.
> > 
> > def (key)  
> > &nbsp;&nbsp;&nbsp;if key.kind\_of?(Integer)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return super[key]  
> > &nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.find{|t| t.name = key} #talkers have .name property.  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > end
> 
> super[key] means (super(key)[key]).
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Devin\_Mullins](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Devin\_Mullins](https://rubytalk.org/u/Devin_Mullins)\
**Post date:** [30 June 2005 01:27 UTC](https://rubytalk.org/t/q-inheriting-from-array-overloading/19265/3 "2005-06-30T01:27:54Z")

</div>

Change super[key] to super(key) or super key. Remember, [] is just another method, and like other methods, it accepts the typical form.

class A  
&nbsp;&nbsp;def [](key)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "key: #{key}"  
&nbsp;&nbsp;end  
end

a = A.new  
a.[](5)

Outputs:  
key: 5

As matz posted, super defaults to passing the same values passed to you, while super() passes zero args.

class A  
&nbsp;&nbsp;def put(a)  
&nbsp;&nbsp;&nbsp;&nbsp;puts a  
&nbsp;&nbsp;end  
end  
class B\<A  
&nbsp;&nbsp;def put(n)  
&nbsp;&nbsp;&nbsp;&nbsp;if n == 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super "zero"  
&nbsp;&nbsp;&nbsp;&nbsp;elsif n \< 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super()  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end  
b = B.new  
b.put 0  
b.put 5  
b.put -1

Outputs:  
zero  
5  
-:11:in `put': wrong number of arguments (0 for 1) (ArgumentError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -:11:in `put'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -:20

So your code was invoking the super method with key as a parameter, taking the return value, and then invoking #[](key) on that.

Devin

---

<div class="post-metadata">

**Author:** ![Matt\_C](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Matt\_C](https://rubytalk.org/u/Matt_C)\
**Post date:** [30 June 2005 04:35 UTC](https://rubytalk.org/t/q-inheriting-from-array-overloading/19265/4 "2005-06-30T04:35:42Z")

</div>

Devin Mullins \<twifkak@comcast.net\> wrote in news:42C34B5F.3050300  
@comcast.net:

> Change super[key] to super(key) or super key. Remember, is just  
> another method, and like other methods, it accepts the typical form.

....

> So your code was invoking the super method with key as a parameter,  
> taking the return value, and then invoking #(key) on that.
> 
> Devin

It took me a little while, but I get it now. It all comes together. Very  
useful example.

Thanks to both of you.

Matt

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [30 June 2005 12:40 UTC](https://rubytalk.org/t/q-inheriting-from-array-overloading/19265/5 "2005-06-30T12:40:42Z")

</div>

Matt C wrote:

> Devin Mullins \<twifkak@comcast.net\> wrote in news:42C34B5F.3050300  
> @comcast.net:
> 
> > Change super[key] to super(key) or super key. Remember, is just  
> > another method, and like other methods, it accepts the typical form.  
> > ... So your code was invoking the super method with key as a  
> > parameter, taking the return value, and then invoking #(key) on  
> > that.
> > 
> > Devin
> 
> It took me a little while, but I get it now. It all comes together.  
> Very useful example.
> 
> Thanks to both of you.
> 
> Matt

There is also a spelling error - compare these:

return self.find{|t| t.name = key} # original  
return self.find{|t| t.name == key} # fake - but better

🙂

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Matt\_C](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Matt\_C](https://rubytalk.org/u/Matt_C)\
**Post date:** [30 June 2005 12:50 UTC](https://rubytalk.org/t/q-inheriting-from-array-overloading/19265/6 "2005-06-30T12:50:42Z")

</div>

"Robert Klemme" \<bob.news@gmx.net\> wrote in news:3ii7agFl8o74U1  
@individual.net:

> There is also a spelling error - compare these:
> 
> return self.find{|t| t.name = key} # original  
> return self.find{|t| t.name == key} # fake - but better
> 
> 🙂
> 
> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

Whoops! Thanks.

Matt
