# Monkey-patching with modules

**URL:** <https://www.ruby-forum.com/t/monkey-patching-with-modules/124113>\
**Category:** Ruby\
**Created:** [December 24, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/monkey-patching-with-modules/124113 "2007-12-24T22:08:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [December 24, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/monkey-patching-with-modules/124113/1 "2007-12-24T22:08:56Z")

</div>

module Foo  
class String  
def hi  
puts ‘hi’  
end  
end  
end

include Foo

x = “hello”  
x.hi

Why does this not work?

Thanks.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [December 24, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/monkey-patching-with-modules/124113/2 "2007-12-24T22:18:03Z")

</div>

Mike H. wrote:

> module Foo  
> class String

You are defining a new class, Foo::String.

Try this here instead:  
class ::String

The :: means ‘look up String in the global scope’.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [December 24, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/monkey-patching-with-modules/124113/3 "2007-12-24T22:19:48Z")

</div>

On Dec 24, 2007 4:08 PM, Mike H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> x = “hello”  
> x.hi
> 
> Why does this not work?

A module is a namespace, so constant names defined inside a module are  
nested within the module.

You’ve defined a new class named Foo::String which is not the same  
class as String (a.k.a. ::String)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [December 24, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/monkey-patching-with-modules/124113/4 "2007-12-24T22:49:17Z")

</div>

Thanks very much.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [December 25, 2007, 4:45am UTC](https://www.ruby-forum.com/t/monkey-patching-with-modules/124113/5 "2007-12-25T04:45:37Z")

</div>

On Dec 24, 2007, at 4:18 PM, Rick DeNatale wrote:

> A module is a namespace, so constant names defined inside a module are  
> nested within the module.

Is there some equivalent of a variable constant? I’m looking for a  
variable that is shared only within the module but is variable.

Basically, a more restrictive global.

Thanks,  
aRi

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [December 25, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/monkey-patching-with-modules/124113/6 "2007-12-25T16:36:04Z")

</div>

On Dec 24, 9:44 pm, thefed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,  
> aRi

Class variable?

Regards,  
Jordan
