From: Iain Barnett Date: 2012-06-20T06:57:16+09:00 Subject: Re: Looking for a better way to add a method to a core class than monkey patching On 19 Jun 2012, at 21:57, Henry Maddocks wrote: > 2. Visibility. If you ask for the ancestors of the base class you will see your module in the list which will give users a clue why the class isn't behaving in a standard way and a pointer to where they should start looking. > > String.ancestors > => [String, MyStringExtensions, Comparable, Object, Kernel, BasicObject] That's a really good idea, thanks. > > 3. Targeted extension. Using extend you can apply your monkey to specific objects rather than every object of that type in the system. Though, as @Bartosz pointed out, this may cause performance degradation's. > > a = "asdfghj" # => "asdfghj" > b = "2345678" # => "2345678" > b.extend MyStringExtensions > a.foo # NoMethodError: undefined method `foo' for "asdfghj":String > b.foo # => "bar" > I just learnt about this yesterday, and though it's not applicable for the situation I've got at the moment, it's good to know, and this is the first code example of it I've seen too. Much appreciated. Regards, Iain