# Difference between rb\_define\_module\_function and rb\_define\_s

**URL:** <https://www.ruby-forum.com/t/difference-between-rb-define-module-function-and-rb-define-s/52760>\
**Category:** Ruby\
**Created:** [December 7, 2005, 6:24pm UTC](https://www.ruby-forum.com/t/difference-between-rb-define-module-function-and-rb-define-s/52760 "2005-12-07T18:24:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drjflam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@drjflam](https://www.ruby-forum.com/u/drjflam)\
**Post date:** [December 7, 2005, 6:24pm UTC](https://www.ruby-forum.com/t/difference-between-rb-define-module-function-and-rb-define-s/52760/1 "2005-12-07T18:24:43Z")

</div>

Is there a difference between calling rb\_define\_module\_function and  
rb\_define\_singleton\_method on a module?

Thanks  
-John

> **[John Lam's Blog](https://www.iunknown.com/)**
>
> Since 1997

---

<div class="post-metadata">

**Author:** ![drjflam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@drjflam](https://www.ruby-forum.com/u/drjflam)\
**Post date:** [December 7, 2005, 6:37pm UTC](https://www.ruby-forum.com/t/difference-between-rb-define-module-function-and-rb-define-s/52760/2 "2005-12-07T18:37:01Z")

</div>

> > > > > “J” == John L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

J\> Is there a difference between calling rb\_define\_module\_function and  
J\> rb\_define\_singleton\_method on a module?

rb\_define\_module\_function() make the 2 calls

rb\_define\_private\_method()  
rb\_define\_singleton\_method()

Guy Decoux

---

<div class="post-metadata">

**Author:** ![drjflam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@drjflam](https://www.ruby-forum.com/u/drjflam)\
**Post date:** [December 7, 2005, 6:53pm UTC](https://www.ruby-forum.com/t/difference-between-rb-define-module-function-and-rb-define-s/52760/3 "2005-12-07T18:53:18Z")

</div>

Thanks!

rb\_define\_module\_function() make the 2 calls

---

<div class="post-metadata">

**Author:** ![drjflam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@drjflam](https://www.ruby-forum.com/u/drjflam)\
**Post date:** [December 7, 2005, 7:21pm UTC](https://www.ruby-forum.com/t/difference-between-rb-define-module-function-and-rb-define-s/52760/4 "2005-12-07T19:21:56Z")

</div>

On 12/7/05, ts [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rb\_define\_module\_function() make the 2 calls
> 
> rb\_define\_private\_method()  
> rb\_define\_singleton\_method()

And this allows the given method to be called with both the module as  
a receiver, as well as directly when the module is included in a  
class. For example, the methods in module Math are defined this way:

irb(main):001:0\> class MyMath  
irb(main):002:1\> include Math  
irb(main):003:1\> def some\_complicated\_math  
irb(main):004:2\> sqrt(4)  
irb(main):005:2\> end  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> MyMath.new.some\_complicated\_math  
=\> 2.0

If they aren’t defined this way, you can’t call them from an instance:

irb(main):008:0\> module Test  
irb(main):009:1\> def self.mod\_meth  
irb(main):010:2\> p ‘mod\_meth’  
irb(main):011:2\> end  
irb(main):012:1\> end  
=\> nil  
irb(main):013:0\> class MyTest  
irb(main):014:1\> include Test  
irb(main):015:1\> def meth  
irb(main):016:2\> mod\_meth  
irb(main):017:2\> end  
irb(main):018:1\> end  
=\> nil  
irb(main):019:0\> MyTest.new.meth  
NameError: undefined local variable or method `mod_meth' for #<MyTest:0x2b388d8> from (irb):16:in `meth’  
from (irb):19

In Ruby you can get the equivalent behavior by defining instance  
methods in the module, then extend self at the end:

irb(main):020:0\> module Test2  
irb(main):021:1\> def mod\_meth2  
irb(main):022:2\> p ‘mod\_meth2’  
irb(main):023:2\> end  
irb(main):024:1\> extend self  
irb(main):025:1\> end  
=\> Test2  
irb(main):026:0\> Test2.mod\_meth2  
“mod\_meth2”  
=\> nil  
irb(main):027:0\> class MyTest2  
irb(main):028:1\> include Test2  
irb(main):029:1\> def meth2  
irb(main):030:2\> mod\_meth2  
irb(main):031:2\> end  
irb(main):032:1\> end  
=\> nil  
irb(main):033:0\> MyTest2.new.meth2  
“mod\_meth2”

Regards,  
Ryan
