# "Self" Reference

**URL:** <https://www.ruby-forum.com/t/self-reference/98345>\
**Category:** Ruby\
**Created:** [July 14, 2006, 12:49am UTC](https://www.ruby-forum.com/t/self-reference/98345 "2006-07-14T00:49:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [July 14, 2006, 12:49am UTC](https://www.ruby-forum.com/t/self-reference/98345/1 "2006-07-14T00:49:27Z")

</div>

Is it possible to define new classes that behave like standard datatypes  
when an instance is referred to? For example:

class ListOfBuddies  
def initialize  
self = []  
self.push(Guy.new(name))  
end  
end  
buddy\_list = ListOfBuddies.new  
.  
.  
.  
buddy\_list[0].type == Guy

Is this possible?

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [July 14, 2006, 1:01am UTC](https://www.ruby-forum.com/t/self-reference/98345/2 "2006-07-14T01:01:23Z")

</div>

Nathan O. wrote:

> Is it possible to define new classes that behave like standard datatypes  
> when an instance is referred to? For example:
> 
> class ListOfBuddies  
> def initialize  
> self = []  
> self.push(Guy.new(name))  
> end  
> end  
> buddy\_list = ListOfBuddies.new

Then, definitely, inheritance is the right choice:

class ListOfBuddies \< Array  
def initialize  
self.push(Guy.new(name))  
end  
end

buddy\_list = ListOfBuddies.new

lopex

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [July 14, 2006, 3:32am UTC](https://www.ruby-forum.com/t/self-reference/98345/3 "2006-07-14T03:32:31Z")

</div>

Nathan O. wrote:

> .  
> .  
> .  
> buddy\_list[0].type == Guy
> 
> Is this possible?

My word, the possibilites if you could! You could change an object’s  
“class” on the fly, hence easy implemention “roles”. I think of this as  
the “soul” of an object. What are other implications of soul shifting?

T.

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [July 14, 2006, 5:49am UTC](https://www.ruby-forum.com/t/self-reference/98345/4 "2006-07-14T05:49:06Z")

</div>

On Jul 13, 2006, at 7:00 PM, Marcin MielÅ¼yÅ?ski wrote:

> lopex

Ehhh, I wouldn’t necessarily choose inheritance. It depends on what  
he’s doing but ListOfBuddies doesn’t strike as having an isa  
relationship with an Array. I personally would choose delegation:

Couple of ways to this:

require ‘delegate’  
class ListOfBuddies \< DelegateClass(Array)  
def initialize  
**setobj** ([])  
push Guy.new(name)  
end  
end

or

require ‘forwardable’  
class ListOfBuddies  
extend Forwardable

def initialize  
@list = []  
push Guy.new(name)  
end

def\_delegators :@list, :push, :[]  
end

The last is my personal favorite. You get to pick exactly what  
methods get delegated to the delegate and unlike inheritance you  
don’t accidently pull in stuff that could possibly kill your  
invariants. (esp. with core classes like Array, they have a _lot_ of  
methods).
