On 9 Oct 2007, at 14:26, Pat Maddox wrote:
> Anyway, I think it would be cool if things were sandboxed.  You could
> register them globally in your app
>
> String.use_extensions :leetspeak
>
> so now every string in the app has #leetspeak on it.  Or you could
> sandbox them in a block
>
> String.use_extensions :leetspeak do
>   "I am cool".to_leetspeak  # => "I 4m k3w1"
> end
>
> "I am not".to_leetspeak  # => NoMethodError
>
> Pat

[alexg / powerbook]/Users/alexg/Desktop(18): cat temp_extend.rb
class Class
   def uninclude(mod)
     mod.instance_methods.each do |meth|
       undef_method(meth)
     end
   end
   def use_module(mod,&blk)
     include mod
     if block_given?
       blk.call
       uninclude(mod)
     end
   end
end

module Foo
   def to_foo
     'foo'
   end
end

module Bar
   def to_bar
     'bar'
   end
end

String.use_module(Foo)
p "bar".to_foo

String.use_module(Bar) do
   p "foo".to_bar
end
p "foo".to_bar
[alexg / powerbook]/Users/alexg/Desktop(19): ruby temp_extend.rb
"foo"
"bar"
temp_extend.rb:34: undefined method `to_bar' for "foo":String  
(NoMethodError)

Alex Gutteridge

Bioinformatics Center
Kyoto University