[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

class String in a module

Gabriel Dragffy

8/6/2007 7:57:00 PM

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



3 Answers

Stefano Crocco

8/6/2007 8:05:00 PM

0

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

Joel VanderWerf

8/6/2007 8:14:00 PM

0

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

dohzya

8/7/2007 9:36:00 PM

0

maybe :
---
module MyMod
String = String
end
---
?


--
Etienne Vallette d'Osia