# Class String in a module

**URL:** <https://www.ruby-forum.com/t/class-string-in-a-module/111395>\
**Category:** Ruby\
**Created:** [August 6, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/class-string-in-a-module/111395 "2007-08-06T21:58:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 6, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/class-string-in-a-module/111395/1 "2007-08-06T21:58:18Z")

</div>

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 D.

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 6, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/class-string-in-a-module/111395/2 "2007-08-06T22:06:18Z")

</div>

Alle lunedì 6 agosto 2007, Gabriel D. ha scritto:

> Gabriel D.
> 
> removed\_email\_address@domain.invalid

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

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 6, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/class-string-in-a-module/111395/3 "2007-08-06T22:14:19Z")

</div>

Gabriel D. 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

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 7, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/class-string-in-a-module/111395/4 "2007-08-07T23:37:12Z")

</div>

## maybe :

## module MyMod String = String end

?
