From: dblack@... Date: 2005-12-28T10:45:11+09:00 Subject: Re: Is there an easy way to extend an object? Hi -- On Wed, 28 Dec 2005, Jeff Cohen wrote: > Given an object, is there a clean way of creating a new object that > wraps the original and then adds a new method? > > It doesn't have to wrap the old object if there's a way to directly add > a new method to the object (I'm allowed to change the incoming object). > > def extend_it(obj) > > # how do I add a method to obj > # or create a wrapper for it? You can directly define a method on a particular object: def obj.new_method ... end You can also create a module, and then use 'extend' to add that module's instance method's to the object's capabilities. module M def x end end obj.extend(M) There are a few more variants on these if they're not what you need.... David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black