# Define\_method for a single instance?

**URL:** <https://www.ruby-forum.com/t/define-method-for-a-single-instance/61529>\
**Category:** Ruby\
**Created:** [May 24, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/define-method-for-a-single-instance/61529 "2006-05-24T20:19:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake\_McArthur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@Jake\_McArthur](https://www.ruby-forum.com/u/Jake_McArthur)\
**Post date:** [May 24, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/define-method-for-a-single-instance/61529/1 "2006-05-24T20:19:27Z")

</div>

I’m trying to define a method for a specific instance of a class  
rather than for every instance of the class. Basically, something  
like this:

class A  
def add\_wibble\_method  
# don’t know what to do here  
end  
end

foo = A.new  
bar = A.new  
foo.add\_wibble\_method  
foo.wibble # should work

# bar.wibble should not work

Any help would be appreciated.

- Jake McArthur

---

<div class="post-metadata">

**Author:** ![Jake\_McArthur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@Jake\_McArthur](https://www.ruby-forum.com/u/Jake_McArthur)\
**Post date:** [May 24, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/define-method-for-a-single-instance/61529/2 "2006-05-24T20:33:55Z")

</div>

Jake McArthur wrote:

> I’m trying to define a method for a specific instance of a class  
> rather than for every instance of the class. Basically, something  
> like this:
> 
> class A  
> def add\_wibble\_method  
> # don’t know what to do here  
> end  
> end
> 
> foo = A.new  
> bar = A.new  
> foo.add\_wibble\_method  
> foo.wibble # should work
> 
> # bar.wibble should not work

module A  
def A.add\_wibble\_method( obj )  
class \<\< obj  
define\_method( :wibble ) { puts “Wibble!” }  
end  
end  
end

k = Object.new  
A.add\_wibble\_method( k )

k.wibble

---

<div class="post-metadata">

**Author:** ![Jake\_McArthur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@Jake\_McArthur](https://www.ruby-forum.com/u/Jake_McArthur)\
**Post date:** [May 24, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/define-method-for-a-single-instance/61529/3 "2006-05-24T20:39:22Z")

</div>

On May 24, 2006, at 2:18 PM, Jake McArthur wrote:

> foo = A.new  
> bar = A.new  
> foo.add\_wibble\_method  
> foo.wibble # should work
> 
> # bar.wibble should not work
> 
> Any help would be appreciated.
> 
> - Jake McArthur

% cat wibble.rb  
class A  
def add\_wibble  
def self.wibble  
puts “I’m wibbling!”  
end  
end  
end

class B  
def add\_wibble  
(class \<\< self; self; end).class\_eval do  
define\_method(:wibble) { puts “I too can wibble.” }  
end  
end  
end

a = A.new  
b = B.new  
a.add\_wibble  
a.wibble  
b.add\_wibble  
b.wibble  
begin  
a1 = A.new  
a1.wibble  
rescue =\> ex  
puts ex  
end

begin  
b1 = B.new  
b1.wibble  
rescue =\> ex  
puts ex  
end

% ruby wibble.rb  
I’m wibbling!  
I too can wibble.  
undefined method `wibble' for #<A:0x1e89f0> undefined method`wibble’ for #\<B:0x1e88ec\>

---

<div class="post-metadata">

**Author:** ![Jake\_McArthur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@Jake\_McArthur](https://www.ruby-forum.com/u/Jake_McArthur)\
**Post date:** [May 24, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/define-method-for-a-single-instance/61529/4 "2006-05-24T20:42:31Z")

</div>

On 5/24/06, Jake McArthur [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> foo = A.new  
> bar = A.new  
> foo.add\_wibble\_method  
> foo.wibble # should work
> 
> # bar.wibble should not work
> 
> Any help would be appreciated.

irb(main):001:0\> class A  
irb(main):002:1\> def add\_wibble\_method  
irb(main):003:2\> class \<\< self  
irb(main):004:3\> def wibble  
irb(main):005:4\> puts “wibble!”  
irb(main):006:4\> end  
irb(main):007:3\> end  
irb(main):008:2\> end  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> foo = A.new  
=\> #\<A:0x2ac53f0\>  
irb(main):011:0\> bar = A.new  
=\> #\<A:0x2ac2ed0\>  
irb(main):012:0\> foo.add\_wibble\_method  
=\> nil  
irb(main):013:0\> foo.wibble  
wibble!  
=\> nil  
irb(main):014:0\> bar.wibble  
NoMethodError: undefined method `wibble’ for #\<A:0x2ac2ed0\>  
from (irb):14

---

<div class="post-metadata">

**Author:** ![Jake\_McArthur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@Jake\_McArthur](https://www.ruby-forum.com/u/Jake_McArthur)\
**Post date:** [May 24, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/define-method-for-a-single-instance/61529/5 "2006-05-24T20:48:50Z")

</div>

On May 24, 2006, at 1:37 PM, Logan C. wrote:

> class B  
> def add\_wibble  
> (class \<\< self; self; end).class\_eval do  
> define\_method(:wibble) { puts “I too can wibble.” }  
> end  
> end  
> end

Perfect! Thanks.

- Jake McArthur

---

<div class="post-metadata">

**Author:** ![Jake\_McArthur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f19dbf/32.png) [@Jake\_McArthur](https://www.ruby-forum.com/u/Jake_McArthur)\
**Post date:** [May 24, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/define-method-for-a-single-instance/61529/6 "2006-05-24T21:39:26Z")

</div>

On May 24, 2006, at 11:18 AM, Jake McArthur wrote:

> foo = A.new  
> bar = A.new  
> foo.add\_wibble\_method  
> foo.wibble # should work
> 
> # bar.wibble should not work
> 
> Any help would be appreciated.

#the self is needed or bar gets wibble too.

class A  
def add\_wibble\_method  
# don’t know what to do here  
def self.wibble  
puts “wibble”  
end  
end  
end

foo = A.new  
bar = A.new  
foo.add\_wibble\_method  
foo.wibble # should work  
bar.wibble rescue puts “error”

– Elliot T.

> **[Curiosity Blog – Elliot Temple](https://www.curi.us/blog/)**
>
> A philosophy blog
