Namespace hygenie

Is there a way to restrict the visibility of changes to standard classes
and modules? I commonly use some extensions of standard classes. Now,
when I release my code as a library I’d have to include these
extensions and anyone using my lib would get them nilly-willy.
I very much like the ability to extend existing classes, but I don’t
want to inflict my changes on others.

I know David Black’s “behaviors”, but they are dynamically scoped.
What’s needed is a statically scoped approach to namespace cleanliness.

Michael

···


Michael Schuerig If at first you don’t succeed…
mailto:schuerig@acm.org try, try again.
http://www.schuerig.de/michael/ --Jerome Morrow, “Gattaca”

I very much like the ability to extend existing classes, but I don't
want to inflict my changes on others.

Look if import-module do what you want

   http://www.ruby-lang.org/en/raa-list.rhtml?id=108

Guy Decoux

ts wrote:

I very much like the ability to extend existing classes, but I
don’t M> want to inflict my changes on others.

Look if import-module do what you want

http://www.ruby-lang.org/en/raa-list.rhtml?id=108

I’m not sure if I understand it correctly, but I don’t think it can do
what I want. Here’s an exemplary case

File myexts.rb

module MyExts
class String
def starts_with(s)
index(s) == 0
end
def ends_with(s)
index(s, -(s.length)) != nil
end
def without_prefix(p)
return self unless self.starts_with(p)
self[p.length…-1]
end
end
end

File somelib.rb

require ‘myexts’
module SomeLib
def some_method(s)
if s.starts_with(“…”)

end
end
end

File libuser.rb

require ‘somelib’

class C
include SomeLib

end

Now, within libuser.rb I don’t want the definitions from myexts.rb to be
visible.

Michael

···


Michael Schuerig If at first you don’t succeed…
mailto:schuerig@acm.org try, try again.
Michael Schürig | Sentenced to making sense --Jerome Morrow, “Gattaca”

I'm not sure if I understand it correctly, but I don't think it can do
what I want. Here's an exemplary case

Well, I don't know if it will be fast

pigeon% cat myexts.rb
module MyExts
   def starts_with(s)
      index(s) == 0
   end
   def ends_with(s)
      index(s, -(s.length)) != nil
   end
   def without_prefix(p)
      return self unless self.starts_with(p)
      self[p.length..-1]
   end
end
pigeon%

pigeon% cat somelib.rb
require 'import-module'
require 'myexts'

module SomeLib
   def some_method(s)
      s.import(MyExts) {|x| p x.without_prefix("abc")}
   end
end
pigeon%

pigeon% cat b.rb
#!/usr/bin/ruby
require 'somelib'

class C
  include SomeLib
   def tt
      some_method("def")
      p "abc".without_prefix("def")
   end
end

C.new.tt

pigeon%

pigeon% b.rb
"def"
./b.rb:8:in `tt': undefined method `without_prefix' for "abc":String (NameError)
        from ./b.rb:12
pigeon%

Guy Decoux

ts wrote:

pigeon% cat somelib.rb
require ‘import-module’
require ‘myexts’

Here’s the catch. ‘Require’ is transitive. When I ‘require’ something,
I’ll get everything that was recursively required, even stuff that is
only needed internally within the module/class I really want.

Michael

···


Michael Schuerig If at first you don’t succeed…
mailto:schuerig@acm.org try, try again.
Michael Schürig | Sentenced to making sense --Jerome Morrow, “Gattaca”