# Operator Overloading \<\<

**URL:** <https://rubytalk.org/t/operator-overloading/21222>\
**Category:** ruby-talk\
**Created:** [29 September 2005 23:11 UTC](https://rubytalk.org/t/operator-overloading/21222 "2005-09-29T23:11:43Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Hulse](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@Matt\_Hulse](https://rubytalk.org/u/Matt_Hulse)\
**Post date:** [29 September 2005 23:11 UTC](https://rubytalk.org/t/operator-overloading/21222/1 "2005-09-29T23:11:43Z")

</div>

Is there a way to overload '\<\<' in the Array class?

Or perhaps there's a better approach to my problem. I have a class  
that inherits from Array. I want the user to be able to use my class  
just like a standard array. However, I want to be able to force some  
code to run every time a user of my class appends a new item. I was  
able to overload the '+' operator to do this but it doesn't fit with  
standard array usage.

Thanks in advance!

Matt  
[matt.hulse@gmail.com](mailto:matt.hulse@gmail.com)

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [29 September 2005 23:15 UTC](https://rubytalk.org/t/operator-overloading/21222/2 "2005-09-29T23:15:04Z")

</div>

It's generally not considered a good idea to overload. Consider using  
Delegate instead.

-austin

> **···**
>
> On 9/29/05, matt.hulse@gmail.com \<matt.hulse@gmail.com\> wrote:
> 
> > Is there a way to overload '\<\<' in the Array class?
> > 
> > Or perhaps there's a better approach to my problem. I have a class  
> > that inherits from Array. I want the user to be able to use my class  
> > just like a standard array. However, I want to be able to force some  
> > code to run every time a user of my class appends a new item. I was  
> > able to overload the '+' operator to do this but it doesn't fit with  
> > standard array usage.
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Bob\_Hutchison1](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@Bob\_Hutchison1](https://rubytalk.org/u/Bob_Hutchison1)\
**Post date:** [30 September 2005 00:20 UTC](https://rubytalk.org/t/operator-overloading/21222/3 "2005-09-30T00:20:20Z")

</div>

Hi,

> Is there a way to overload '\<\<' in the Array class?
> 
> Or perhaps there's a better approach to my problem. I have a class  
> that inherits from Array. I want the user to be able to use my class  
> just like a standard array. However, I want to be able to force some  
> code to run every time a user of my class appends a new item. I was  
> able to overload the '+' operator to do this but it doesn't fit with  
> standard array usage.

I'm not sure what you mean by overloading. You don't mean like this?...

class MyArray \< Array  
&nbsp;&nbsp;&nbsp;def \<\<(v)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "before #{v}: #{self.inspect}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(v)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "after #{v}: #{self.inspect}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
&nbsp;&nbsp;&nbsp;end  
end

a = MyArray.new  
a \<\< 1 \<\< 2 \<\< 3  
p a

Cheers,  
Bob

> **···**
>
> On Sep 29, 2005, at 7:11 PM, matt.hulse@gmail.com wrote:
> 
> > Thanks in advance!
> > 
> > Matt  
> > matt.hulse@gmail.com
> 
> ----  
> Bob Hutchison -- blogs at \<[http://www.recursive.ca/hutch/&gt](http://www.recursive.ca/hutch/&gt);  
> Recursive Design Inc. -- \<[http://www.recursive.ca/&gt](http://www.recursive.ca/&gt);  
> Raconteur -- \<[http://www.raconteur.info/&gt](http://www.raconteur.info/&gt);

---

<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 September 2005 07:51 UTC](https://rubytalk.org/t/operator-overloading/21222/4 "2005-09-30T07:51:46Z")

</div>

Austin Ziegler wrote:

> > Is there a way to overload '\<\<' in the Array class?
> > 
> > Or perhaps there's a better approach to my problem. I have a class  
> > that inherits from Array. I want the user to be able to use my class  
> > just like a standard array. However, I want to be able to force some  
> > code to run every time a user of my class appends a new item. I was  
> > able to overload the '+' operator to do this but it doesn't fit with  
> > standard array usage.
> 
> It's generally not considered a good idea to overload. Consider using  
> Delegate instead.

Pardon me, but this is nonsense: Matt has \*inherited\* Array. Of course  
you can argue about whether \*that\* is reasonable, but once you did that  
providing your own implementation of a super class method in a derived  
class is completely standard OO. There's even a keyword to support this  
("super").

If you wanted to question the practice of inheriting classes like Array  
and String then I'm completely with you. This doesn't make sense more  
often that it does. But I know too little of this particular case to  
further comment on that.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;-r 🙂

> **···**
>
> > On 9/29/05, matt.hulse@gmail.com \<matt.hulse@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Matt\_Hulse](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@Matt\_Hulse](https://rubytalk.org/u/Matt_Hulse)\
**Post date:** [30 September 2005 14:56 UTC](https://rubytalk.org/t/operator-overloading/21222/5 "2005-09-30T14:56:45Z")

</div>

\<\<Austin\>\>You can't overload in Ruby, you can override and you have to  
call super to make it work, or you have to call it directly with  
aliasing.

Yes, override is the term I should have used. Bob and Daniel, thanks  
for the examples and Austin, thanks for the guidance in the right  
direction. I will go learn about delegates and see how I fare. Aside  
from being new to Ruby I'm still pretty new to programming in general.  
(Just a couple of C++ classes under my belt. Enough to be dangerous  
😉 ) But I am loving Ruby!!!

Are delegates common to all OO languages? I don't remember learning  
about them in my C++ class.

Thanks again.

Matt

---

<div class="post-metadata">

**Author:** ![Brian\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brian\_Mitchell](https://rubytalk.org/u/Brian_Mitchell)\
**Post date:** [30 September 2005 07:56 UTC](https://rubytalk.org/t/operator-overloading/21222/6 "2005-09-30T07:56:01Z")

</div>

It seems like this thread ought to consider the differences between  
overloading and overriding. They are most certainly not the same.

Brian.

> **···**
>
> On 9/30/05, Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > Austin Ziegler wrote:  
> > \> On 9/29/05, matt.hulse@gmail.com \<matt.hulse@gmail.com\> wrote:  
> > \>\> Is there a way to overload '\<\<' in the Array class?  
> > \>\>  
> > \>\> Or perhaps there's a better approach to my problem. I have a class  
> > \>\> that inherits from Array. I want the user to be able to use my class  
> > \>\> just like a standard array. However, I want to be able to force some  
> > \>\> code to run every time a user of my class appends a new item. I was  
> > \>\> able to overload the '+' operator to do this but it doesn't fit with  
> > \>\> standard array usage.  
> > \>  
> > \> It's generally not considered a good idea to overload. Consider using  
> > \> Delegate instead.
> > 
> > Pardon me, but this is nonsense: Matt has \*inherited\* Array. Of course  
> > you can argue about whether \*that\* is reasonable, but once you did that  
> > providing your own implementation of a super class method in a derived  
> > class is completely standard OO. There's even a keyword to support this  
> > ("super").
> > 
> > If you wanted to question the practice of inheriting classes like Array  
> > and String then I'm completely with you. This doesn't make sense more  
> > often that it does. But I know too little of this particular case to  
> > further comment on that.
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;-r 🙂

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [30 September 2005 12:04 UTC](https://rubytalk.org/t/operator-overloading/21222/7 "2005-09-30T12:04:04Z")

</div>

> Austin Ziegler wrote:  
> \>\> Is there a way to overload '\<\<' in the Array class?  
> \>\> Or perhaps there's a better approach to my problem. I have a class  
> \>\> that inherits from Array. I want the user to be able to use my class  
> \>\> just like a standard array. However, I want to be able to force some  
> \>\> code to run every time a user of my class appends a new item. I was  
> \>\> able to overload the '+' operator to do this but it doesn't fit with  
> \>\> standard array usage.  
> \> It's generally not considered a good idea to overload. Consider using  
> \> Delegate instead.  
> Pardon me, but this is nonsense: Matt has \*inherited\* Array. Of course  
> you can argue about whether \*that\* is reasonable, but once you did that  
> providing your own implementation of a super class method in a derived  
> class is completely standard OO. There's even a keyword to support this  
> ("super").

And all of that still works with Delegate. I know, because I \*just\*  
did it for some code that won't see the light of day for a while.

> If you wanted to question the practice of inheriting classes like Array  
> and String then I'm completely with you. This doesn't make sense more  
> often that it does. But I know too little of this particular case to  
> further comment on that.

That's really what I was trying to say here. Yes, the idea is \*don't\*  
inherit from Array. You can't overload in Ruby, you can override and  
you have to call super to make it work, or you have to call it  
directly with aliasing.

-austin

> **···**
>
> On 9/30/05, Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > \> On 9/29/05, matt.hulse@gmail.com \<matt.hulse@gmail.com\> wrote:
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Bob\_Hutchison1](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@Bob\_Hutchison1](https://rubytalk.org/u/Bob_Hutchison1)\
**Post date:** [30 September 2005 15:27 UTC](https://rubytalk.org/t/operator-overloading/21222/8 "2005-09-30T15:27:25Z")

</div>

In class based languages like C++ and Ruby it is a technique (or pattern). In delegate/prototype based languages (like self and JavaScript) it is the mechanism used to implement inheritance.

You should be able to find some good examples in Ruby by googling around a bit. One thing to read would be the Pragmatic Programmers page: \<[http://www.rubycentral.com/book/lib\_patterns.html&gt](http://www.rubycentral.com/book/lib_patterns.html&gt);

Cheers,  
Bob

> **···**
>
> On Sep 30, 2005, at 10:56 AM, Matt Hulse wrote:
> 
> > Are delegates common to all OO languages? I don't remember learning  
> > about them in my C++ class.
> 
> ----  
> Bob Hutchison -- blogs at \<[http://www.recursive.ca/hutch/&gt](http://www.recursive.ca/hutch/&gt);  
> Recursive Design Inc. -- \<[http://www.recursive.ca/&gt](http://www.recursive.ca/&gt);  
> Raconteur -- \<[http://www.raconteur.info/&gt](http://www.raconteur.info/&gt);

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [30 September 2005 15:28 UTC](https://rubytalk.org/t/operator-overloading/21222/9 "2005-09-30T15:28:35Z")

</div>

Delegation is a design pattern and in that sense, yes, it's common to most languages:

> **[Delegation (computing)](https://en.wikipedia.org/wiki/Delegation_%28programming%29)**
>
> In computing or computer programming, delegation refers generally to one entity passing something to another entity, and narrowly to various specific forms of relationships. These include:

In Ruby, we have two standard libraries that cover delegation. I just finished documenting them and am trying to get them up on ruby-doc.org, but until then, here's some sample code from the docs of delegate.rb:

> **···**
>
> On Sep 30, 2005, at 9:56 AM, Matt Hulse wrote:
> 
> > Are delegates common to all OO languages? I don't remember learning  
> > about them in my C++ class.
> 
> #  
> # The primary interface to this library. Use to setup delegation when defining  
> # your class.  
> #  
> # class MyClass \< DelegateClass( ClassToDelegateTo ) # Step 1  
> # def initiaize  
> # super(obj\_of\_ClassToDelegateTo) # Step 2  
> # end  
> # end  
> #
> 
> and
> 
> #  
> # === DelegateClass()  
> #  
> # Here's a sample of use from \<i\>tempfile.rb\</i\>.  
> #  
> # A \_Tempfile\_ object is really just a \_File\_ object with a few special rules  
> # about storage location and/or when the File should be deleted. That makes for  
> # an almost textbook perfect example of how to use delegation.  
> #  
> # class Tempfile \< DelegateClass(File)  
> # # constant and class member data initialization...  
> #  
> # def initialize(basename, tmpdir=Dir::tmpdir)  
> # # build up file path/name in var tmpname...  
> #  
> # @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)  
> #  
> # # ...  
> #  
> # super(@tmpfile)  
> #  
> # # below this point, all methods of File are supported...  
> # end  
> #  
> # # ...  
> # end  
> #
> 
> Hope that helps.
> 
> James Edward Gray II

---

<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 September 2005 08:26 UTC](https://rubytalk.org/t/operator-overloading/21222/10 "2005-09-30T08:26:44Z")

</div>

Brian Mitchell wrote:

> **···**
>
> > On 9/30/05, Robert Klemme \<bob.news@gmx.net\> wrote:
> > 
> > > Austin Ziegler wrote:
> > > 
> > > > On 9/29/05, matt.hulse@gmail.com \<matt.hulse@gmail.com\> wrote:
> > > > 
> > > > > Is there a way to overload '\<\<' in the Array class?
> > > > > 
> > > > > Or perhaps there's a better approach to my problem. I have a class  
> > > > > that inherits from Array. I want the user to be able to use my  
> > > > > class just like a standard array. However, I want to be able to  
> > > > > force some code to run every time a user of my class appends a new  
> > > > > item. I was able to overload the '+' operator to do this but it  
> > > > > doesn't fit with standard array usage.
> > > > 
> > > > It's generally not considered a good idea to overload. Consider  
> > > > using Delegate instead.
> > > 
> > > Pardon me, but this is nonsense: Matt has \*inherited\* Array. Of  
> > > course you can argue about whether \*that\* is reasonable, but once  
> > > you did that providing your own implementation of a super class  
> > > method in a derived class is completely standard OO. There's even a  
> > > keyword to support this ("super").
> > > 
> > > If you wanted to question the practice of inheriting classes like  
> > > Array and String then I'm completely with you. This doesn't make  
> > > sense more often that it does. But I know too little of this  
> > > particular case to further comment on that.
> > > 
> > > Kind regards
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;-r 🙂
> > 
> > It seems like this thread ought to consider the differences between  
> > overloading and overriding. They are most certainly not the same.
> 
> True. We used the wrong term. But if I'm not mistaken everybody was  
> actually talking about overriding so far - even if they used the wrong  
> word. Thanks for reminding us!
> 
> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Bob\_Hutchison1](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@Bob\_Hutchison1](https://rubytalk.org/u/Bob_Hutchison1)\
**Post date:** [30 September 2005 13:24 UTC](https://rubytalk.org/t/operator-overloading/21222/11 "2005-09-30T13:24:07Z")

</div>

Where do you stop? Why inherit at all? How do you decide?

[I sympathise with this, I've always preferred composition to inheritance. Worse still, I went to the OOPSLA'87 (Orlando anyway, think it was 1987) conference because I didn't 'get' OO and was hoping to figure it out. I mentioned this to someone I was sitting beside and was treated to a lesson over lunch (maybe breakfast, can't remember). This person was David Unger, one of the key people responsible for self and the alternate OO model, delegation/prototype, to Ruby's class-based OO model. So I was indoctrinated early by one of the best 🙂 The "Treaty of Orlando" \<[http://citeseer.ist.psu.edu/stein89shared.html&gt](http://citeseer.ist.psu.edu/stein89shared.html&gt); came out of that conference.]

Cheers,  
Bob

> **···**
>
> On Sep 30, 2005, at 8:04 AM, Austin Ziegler wrote:
> 
> > On 9/30/05, Robert Klemme \<bob.news@gmx.net\> wrote:
> > 
> > > Pardon me, but this is nonsense: Matt has \*inherited\* Array. Of course  
> > > you can argue about whether \*that\* is reasonable, but once you did that  
> > > providing your own implementation of a super class method in a derived  
> > > class is completely standard OO. There's even a keyword to support this  
> > > ("super").
> > 
> > And all of that still works with Delegate. I know, because I \*just\*  
> > did it for some code that won't see the light of day for a while.
> > 
> > > If you wanted to question the practice of inheriting classes like Array  
> > > and String then I'm completely with you. This doesn't make sense more  
> > > often that it does. But I know too little of this particular case to  
> > > further comment on that.
> > 
> > That's really what I was trying to say here. Yes, the idea is \*don't\*  
> > inherit from Array. You can't overload in Ruby, you can override and  
> > you have to call super to make it work, or you have to call it  
> > directly with aliasing.
> 
> ----  
> Bob Hutchison -- blogs at \<[http://www.recursive.ca/hutch/&gt](http://www.recursive.ca/hutch/&gt);  
> Recursive Design Inc. -- \<[http://www.recursive.ca/&gt](http://www.recursive.ca/&gt);  
> Raconteur -- \<[http://www.raconteur.info/&gt](http://www.raconteur.info/&gt);

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [30 September 2005 13:48 UTC](https://rubytalk.org/t/operator-overloading/21222/12 "2005-09-30T13:48:07Z")

</div>

Sorry, but this really doesn't have anything to do with my statement.  
One shouldn't inherit from Array, String, and Hash in Ruby because there  
are certain things that Don't Work the way that you'd expect them to  
because they are written in the C side.

In general, I don't have a problem with inheritance -- it should be used  
smartly -- but in this specific case, inheriting from certain of Ruby's  
core classes is enough of a problem that you do \*not\* want to do that.

-austin

> **···**
>
> On 9/30/05, Bob Hutchison \<hutch@recursive.ca\> wrote:
> 
> > On Sep 30, 2005, at 8:04 AM, Austin Ziegler wrote:
> > 
> > > On 9/30/05, Robert Klemme \<bob.news@gmx.net\> wrote:
> > > 
> > > > Pardon me, but this is nonsense: Matt has \*inherited\* Array. Of  
> > > > course you can argue about whether \*that\* is reasonable, but once  
> > > > you did that providing your own implementation of a super class  
> > > > method in a derived class is completely standard OO. There's even a  
> > > > keyword to support this ("super").
> > > 
> > > And all of that still works with Delegate. I know, because I \*just\*  
> > > did it for some code that won't see the light of day for a while.
> 
> > > > If you wanted to question the practice of inheriting classes like  
> > > > Array and String then I'm completely with you. This doesn't make  
> > > > sense more often that it does. But I know too little of this  
> > > > particular case to further comment on that.
> > > 
> > > That's really what I was trying to say here. Yes, the idea is \*don't\*  
> > > inherit from Array. You can't overload in Ruby, you can override and  
> > > you have to call super to make it work, or you have to call it  
> > > directly with aliasing.
> > 
> > Where do you stop? Why inherit at all? How do you decide?
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [30 September 2005 15:46 UTC](https://rubytalk.org/t/operator-overloading/21222/13 "2005-09-30T15:46:43Z")

</div>

Hi --

> Where do you stop? Why inherit at all? How do you decide?
> 
> [I sympathise with this, I've always preferred composition to inheritance. Worse still, I went to the OOPSLA'87 (Orlando anyway, think it was 1987) conference because I didn't 'get' OO and was hoping to figure it out. I mentioned this to someone I was sitting beside and was treated to a lesson over lunch (maybe breakfast, can't remember). This person was David Unger, one of the key people responsible for self and the alternate OO model, delegation/prototype, to Ruby's class-based OO model. So I was indoctrinated early by one of the best 🙂 The "Treaty of Orlando" \<http:// citeseer.ist.psu.edu/stein89shared.html\> came out of that conference.]

I sometimes wonder how class-based Ruby's model is. Or maybe it's a  
hybrid. I don't know any purely prototyped languages, so I can't  
compare directly, but I always think of a class in Ruby as a mechanism  
for bootstrapping objects into object-space -- at which point the  
objects have nothing further to do with the class, except that some  
subset of the class's instance methods may continue to serve as a  
subset of the methods of the object. (But it never has to; that's  
just a convenient default.)

It's the old "class != type in Ruby" thing. I wonder whether Ruby's  
potential to be regarded as a prototyped language simply has yet to be  
fully explored.

&nbsp;&nbsp;&nbsp;module Kernel  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;undef :kind\_of?, :is\_a? # maybe a few others  
&nbsp;&nbsp;&nbsp;end

would be an interesting experiment 🙂

David

> **···**
>
> On Fri, 30 Sep 2005, Bob Hutchison wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Bob\_Hutchison1](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@Bob\_Hutchison1](https://rubytalk.org/u/Bob_Hutchison1)\
**Post date:** [30 September 2005 13:51 UTC](https://rubytalk.org/t/operator-overloading/21222/14 "2005-09-30T13:51:33Z")

</div>

What classes are difficult? Seriously asked. It would be very useful to know.

> **···**
>
> On Sep 30, 2005, at 9:48 AM, Austin Ziegler wrote:
> 
> > inheriting from certain of Ruby's  
> > core classes is enough of a problem that you do \*not\* want to do that.
> 
> ----  
> Bob Hutchison -- blogs at \<[http://www.recursive.ca/hutch/&gt](http://www.recursive.ca/hutch/&gt);  
> Recursive Design Inc. -- \<[http://www.recursive.ca/&gt](http://www.recursive.ca/&gt);  
> Raconteur -- \<[http://www.raconteur.info/&gt](http://www.raconteur.info/&gt);

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [30 September 2005 14:22 UTC](https://rubytalk.org/t/operator-overloading/21222/15 "2005-09-30T14:22:09Z")

</div>

In addition to this, I'd also say be very careful about  
redefining methods in core classes. Same reason - they are  
written in C. Many times the C code bypasses the standard  
method call mechanism and calls other C code methods directly  
so if you redefine you may not get what you want. An extreme  
example is Regexp#=~. See what happens when you try to  
override it - you'll get inconsistent results even if only ruby  
code calls it.

> **···**
>
> --- Austin Ziegler \<halostatue@gmail.com\> wrote:
> 
> > Sorry, but this really doesn't have anything to do with my  
> > statement.  
> > One shouldn't inherit from Array, String, and Hash in Ruby  
> > because there  
> > are certain things that Don't Work the way that you'd expect  
> > them to  
> > because they are written in the C side.
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [30 September 2005 16:42 UTC](https://rubytalk.org/t/operator-overloading/21222/16 "2005-09-30T16:42:55Z")

</div>

It looks like ruby does prototyping just fine since each object  
can have its own meta class:

class Object  
&nbsp;&nbsp;def meta\_class  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self;self;end  
&nbsp;&nbsp;end  
end

a = Object.new  
a.meta\_class.class\_eval { def foo;"a";end }  
b = a.clone  
p a.foo # =\> "a"  
p b.foo # =\> "a"

b.meta\_class.class\_eval { def foo;"b";end }  
p a.foo # =\> "a"  
p b.foo # =\> "b"

a.meta\_class.class\_eval { def foo;"A";end }  
p a.foo # =\> "A"  
p b.foo # =\> "b"

I'm not sure how memory efficient this would be compared to a  
pure prototyping language though. Not sure if initially b  
(directly from a.clone) shares the "foo" definition or not.

> **···**
>
> --- "David A. Black" \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Fri, 30 Sep 2005, Bob Hutchison wrote:
> > 
> > \> Where do you stop? Why inherit at all? How do you decide?  
> > \>  
> > \> [I sympathise with this, I've always preferred composition  
> > to inheritance.  
> > \> Worse still, I went to the OOPSLA'87 (Orlando anyway, think  
> > it was 1987)  
> > \> conference because I didn't 'get' OO and was hoping to  
> > figure it out. I  
> > \> mentioned this to someone I was sitting beside and was  
> > treated to a lesson  
> > \> over lunch (maybe breakfast, can't remember). This person  
> > was David Unger,  
> > \> one of the key people responsible for self and the  
> > alternate OO model,  
> > \> delegation/prototype, to Ruby's class-based OO model. So I  
> > was indoctrinated  
> > \> early by one of the best 🙂 The "Treaty of Orlando"  
> > \<http://  
> > \> citeseer.ist.psu.edu/stein89shared.html\> came out of that  
> > conference.]
> > 
> > I sometimes wonder how class-based Ruby's model is. Or maybe  
> > it's a  
> > hybrid. I don't know any purely prototyped languages, so I  
> > can't  
> > compare directly, but I always think of a class in Ruby as a  
> > mechanism  
> > for bootstrapping objects into object-space -- at which point  
> > the  
> > objects have nothing further to do with the class, except  
> > that some  
> > subset of the class's instance methods may continue to serve  
> > as a  
> > subset of the methods of the object. (But it never has to;  
> > that's  
> > just a convenient default.)
> > 
> > It's the old "class != type in Ruby" thing. I wonder whether  
> > Ruby's  
> > potential to be regarded as a prototyped language simply has  
> > yet to be  
> > fully explored.
> > 
> > &nbsp;&nbsp;&nbsp;module Kernel  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;undef :kind\_of?, :is\_a? # maybe a few others  
> > &nbsp;&nbsp;&nbsp;end
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do You Yahoo!?  
> Tired of spam? Yahoo! Mail has the best spam protection around
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [30 September 2005 14:08 UTC](https://rubytalk.org/t/operator-overloading/21222/17 "2005-09-30T14:08:20Z")

</div>

Previously named: Array, Hash, and String are the most problematic. In  
implementing PDF::Core versions of these types, I used Delegate. I  
also did the same with Symbol.

I'd say that subclassing anything that is written in C is likely to be  
difficult and have surprising results.

-austin

> **···**
>
> On 9/30/05, Bob Hutchison \<hutch@recursive.ca\> wrote:
> 
> > On Sep 30, 2005, at 9:48 AM, Austin Ziegler wrote:  
> > \> inheriting from certain of Ruby's  
> > \> core classes is enough of a problem that you do \*not\* want to do that.  
> > What classes are difficult? Seriously asked. It would be very useful  
> > to know.
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [30 September 2005 17:03 UTC](https://rubytalk.org/t/operator-overloading/21222/18 "2005-09-30T17:03:01Z")

</div>

Hi --

> **···**
>
> On Sat, 1 Oct 2005, Eric Mahurin wrote:
> 
> > It looks like ruby does prototyping just fine since each object  
> > can have its own meta class:
> > 
> > class Object  
> > def meta\_class  
> > &nbsp;&nbsp;&nbsp;class \<\< self;self;end  
> > end  
> > end
> 
> Yes I know -- see [RCR 231: Kernel#singleton\_class](http://www.rcrchive.net/rcr/show/231) 🙂 That's  
> what I mean: Ruby objects have a class but they also have their own  
> life beyond their birth-class.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Daniel\_Amelang](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Daniel\_Amelang](https://rubytalk.org/u/Daniel_Amelang)\
**Post date:** [1 October 2005 02:39 UTC](https://rubytalk.org/t/operator-overloading/21222/19 "2005-10-01T02:39:52Z")

</div>

> Ruby objects have a class but they also have their own  
> life beyond their birth-class.

Unlike Slashdot-reader objects, who never even move out of the  
basement of their birth-class.

C'mon, it's friday!

Dan Amelang
