steve ross <cwdinfo / gmail.com> wrote:

> All well and good until some other library with some other implementation
> of these String extensions gets pulled in.
> 
> So using what I thought refinements would be, I could have done:
> 
> module CoreExtensions
>   class String
>     def singularize
>       # something here
>     end
>   end
> end
> 
> class MyConsumerClass
>   using CoreExtensions
>   
>   def do_something
>     puts "dogs".singularize
>   end
> end
> 
> This would magically have scoped my String extensions to the
>CoreExtensions module, preventing any namespace pollution, while at the
>same time allowing include-like access to these extensions within my
>class.
>

But you can still do that:

module CoreExtensions
  refine String do
    def singularize
      "howdy"
    end
  end
end

using CoreExtensions
class MyConsumerClass
  def do_something
    puts "dogs".singularize
  end
end

MyConsumerClass.new.do_something

That works because MyConsumerClass was defined *after* the "using"
statement, so it gets the refinement of String. Nothing else does. "Some
other library with some other implementation" does not.

I missed the earlier discussion on this thread, though, so perhaps this
isn't helpful...

m.

-- 
matt neuburg, phd = matt / tidbits.com, http://www.apeth.net/matt/
Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html