# Module methods

**URL:** <https://www.ruby-forum.com/t/module-methods/189252>\
**Category:** Ruby\
**Created:** [May 27, 2010, 2:00pm UTC](https://www.ruby-forum.com/t/module-methods/189252 "2010-05-27T14:00:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [May 27, 2010, 2:00pm UTC](https://www.ruby-forum.com/t/module-methods/189252/1 "2010-05-27T14:00:50Z")

</div>

i wanted to have a Module method having the same name as another one  
(from Kernel ?) :

module PlaySound  
…  
def PlaySound.rand  
play a random sound  
end  
…  
end

then i had a stack overflow and found renaming the method to  
“PlaySound.random” was OK.

I don’ understand because i thougth Modules are designed to avoid names  
collision ?

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [May 27, 2010, 4:30pm UTC](https://www.ruby-forum.com/t/module-methods/189252/2 "2010-05-27T16:30:07Z")

</div>

2010/5/27 Une Bévue [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> then i had a stack overflow and found renaming the method to  
> “PlaySound.random” was OK.
> 
> I don’ understand because i thougth Modules are designed to avoid names  
> collision ?

I guess you are doing something like this:

irb(main):001:0\> module PlaySound  
irb(main):002:1\> def PlaySound.rand  
irb(main):003:2\> rand  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> PlaySound.rand  
SystemStackError: stack level too deep  
from (irb):3:in `rand' from (irb):3:in `rand’  
from (irb):6  
from :0

The rand that you call inside the PlaySound.rand is itself, not  
Kernel’s rand. Try this instead:

irb(main):007:0\> module PlaySound  
irb(main):008:1\> def PlaySound.rand  
irb(main):009:2\> Kernel.rand  
irb(main):010:2\> end  
irb(main):011:1\> end  
=\> nil  
irb(main):012:0\> PlaySound.rand  
=\> 0.821838053097397

Jesus.

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [May 27, 2010, 8:51pm UTC](https://www.ruby-forum.com/t/module-methods/189252/3 "2010-05-27T20:51:50Z")

</div>

Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> from (irb):3:in `rand'
> from (irb):3:in `rand'
> from (irb):6
> from :0
> 
> ```

RIGHT ! )))

> =\> 0.821838053097397  
> i thought i had tried that too but may be with another error…

right know, after your advice, it works !

thanks !
