# Defining object methods in initialize

**URL:** <https://www.ruby-forum.com/t/defining-object-methods-in-initialize/87614>\
**Category:** Ruby\
**Created:** [March 4, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/defining-object-methods-in-initialize/87614 "2007-03-04T14:59:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [March 4, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/defining-object-methods-in-initialize/87614/1 "2007-03-04T14:59:59Z")

</div>

I’d like to define methods on initialize that are specific for the  
created  
object. However it seems that the few lines of code below is defining  
them  
somewhere else since they are ‘overwriting’ each other:

============  
class Foo  
def initialize(method\_name, &block)  
self.class.send(:define\_method, method\_name, &block)  
end  
end

aa = Foo.new(:whoareyou) { puts “I am aa” }  
bb = Foo.new(:whoareyou) { puts “I am bb” }

aa.whoareyou

# returns: “I am bb” - But I expect: “I am aa”

bb.whoareyou

# returns: “I am bb”

Foo.whoareyou

# undefined method ===\> Good

============

Thanks!

martin

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [March 4, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/defining-object-methods-in-initialize/87614/2 "2007-03-04T15:12:01Z")

</div>

> However it seems that the few lines of code below is defining them  
> somewhere else since they are ‘overwriting’ each other:
> 
> ============  
> class Foo  
> def initialize(method\_name, &block)  
> self.class.send(:define\_method, method\_name, &block)  
> end  
> end

You’re absoultely right about the methods being defined “somewhere  
else”, they’re being defined in the class Foo, which both instances  
share. If you want to define specific and different implementations  
for the same method name you need to use singleton/instance  
inheritance. Like this:

class Foo  
def initialize(method\_name, &block)  
c = class \<\< self; self; end  
c.send(:define\_method, method\_name, &block)  
end  
end

That way each instance gets a private Class that they don’t share.

Take the above with a grain of salt, I don’t have an interpreter on  
this computer, so I can test it to see if it works… This blog  
article might be interesting if you care for a deeper explanation:

> **[Ola Bini: Programming Language Synchronicity](http://ola-bini.blogspot.com/search/label/singleton%20class)**
>
> Ruby, Java, Lisp, Io, JRuby. Programming language archeology, creation and discovery.

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [March 4, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/defining-object-methods-in-initialize/87614/3 "2007-03-04T15:32:02Z")

</div>

Thanks! Working!

---

<div class="post-metadata">

**Author:** ![pytlik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pytlik](https://www.ruby-forum.com/u/pytlik)\
**Post date:** [March 4, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/defining-object-methods-in-initialize/87614/4 "2007-03-04T20:35:38Z")

</div>

On 04.03.2007 14:59, Martin B. wrote:

> ============  
> Note, you do not necessarily need instance methods in this case:

irb(main):001:0\> Foo = Struct.new :whoareyou  
=\> Foo  
irb(main):002:0\> aa = Foo.new “I am aa”  
=\> #  
irb(main):003:0\> bb = Foo.new “A am bb”  
=\> #  
irb(main):004:0\> aa.whoareyou  
=\> “I am aa”  
irb(main):005:0\> bb.whoareyou  
=\> “A am bb”

Kind regards

robert
