Hi all
I have a module file which contains 'class String' and a couple of custom methods for me to add to Object::String. So I do something like:
require 'mystring' or load or whatever. It doesn't extend String's functionallity... no doubt because the modules contents are isolated in their own namespace... How to get around that?
Many thanks
Gabriel Dragffy
gabe@dragffy.com
Alle lunedì 6 agosto 2007, Gabriel Dragffy ha scritto:
Hi all
I have a module file which contains 'class String' and a couple of
custom methods for me to add to Object::String. So I do something like:
require 'mystring' or load or whatever. It doesn't extend String's
functionallity... no doubt because the modules contents are isolated
in their own namespace... How to get around that?
Many thanks
Gabriel Dragffy
gabe@dragffy.com
Do you mean the following situation?
# file mystring.rb
module MyMod
class String
...
end
end
In this case, you're right. This piece of (pseudo) code creates the class
MyMod::String, which is a completely different thing from the String class.
If you want to extend String, you simply have to move the class
String;...;end part outside the definition of module MyMod:
# file mystring.rb
class String
...
end
module MyMod
...
end
I hope this help
Stefano
Gabriel Dragffy wrote:
Hi all
I have a module file which contains 'class String' and a couple of custom methods for me to add to Object::String. So I do something like:
require 'mystring' or load or whatever. It doesn't extend String's functionallity... no doubt because the modules contents are isolated in their own namespace... How to get around that?
Use the global scope operator (at least I think that's what it's called):
module M
class ::String
def backwards; reverse; end
end
end
puts "24".backwards # => 42
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407