# Adding a method to a single object

**URL:** <https://www.ruby-forum.com/t/adding-a-method-to-a-single-object/51903>\
**Category:** Ruby\
**Created:** [January 9, 2006, 3:04am UTC](https://www.ruby-forum.com/t/adding-a-method-to-a-single-object/51903 "2006-01-09T03:04:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Lange](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Marco\_Lange](https://www.ruby-forum.com/u/Marco_Lange)\
**Post date:** [January 9, 2006, 3:04am UTC](https://www.ruby-forum.com/t/adding-a-method-to-a-single-object/51903/1 "2006-01-09T03:04:30Z")

</div>

Hi,

how is it possible to add a method to a single object programatically?  
E.g. I want to write a method

class Object

def rename\_method(old\_id, new\_id)  
…  
end

end

The method shall rename a method for a specific instance of any class,  
e.g. the following should be possible:

a = 5  
5.rename\_method :times, :veces

5.times { … }  
-\> Error, Method "times"does not exist

5.veces { … }  
-\> Expected behaviour

I already succeeded in renaming methods for whole classes, but for a  
single instance?

I do not really have a use case for such a method, but it is fun  
learning about the object model in Ruby.

Best regards,  
Marco

---

<div class="post-metadata">

**Author:** ![Marco\_Lange](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Marco\_Lange](https://www.ruby-forum.com/u/Marco_Lange)\
**Post date:** [January 9, 2006, 3:22am UTC](https://www.ruby-forum.com/t/adding-a-method-to-a-single-object/51903/2 "2006-01-09T03:22:36Z")

</div>

Hi –

On Mon, 9 Jan 2006, Marco Lange wrote:

> Hi,
> 
> how is it possible to add a method to a single object programatically?

def obj.meth  
…  
end

(and a few other ways, but that’s the simplest)

> The method shall rename a method for a specific instance of any class, e.g.  
> → Expected behaviour
> 
> I already succeeded in renaming methods for whole classes, but for a single  
> instance?

Keep in mind that a single instance has a class of its own – its  
singleton class – as well as its class of origin. If you do the  
renaming in the singleton class, it will affect only that object.

Have a look at this example, and see if it helps:

class C  
def m  
puts “In the method”  
end  
end

c = C.new  
c.m # In the method

class \<\< c # “\<\< c” invokes the singleton class of c  
alias n m  
undef\_method(“m”)  
end

c.n # In the method  
c.m # error – no such method

Hmmm… one problem is that Fixnums don’t have singleton classes, so  
this wouldn’t work for 5. Anyway, maybe it will give you ideas 🙂

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...
