# The simpelest of module function scoping

**URL:** <https://www.ruby-forum.com/t/the-simpelest-of-module-function-scoping/171784>\
**Category:** Ruby\
**Created:** [July 9, 2009, 8:26am UTC](https://www.ruby-forum.com/t/the-simpelest-of-module-function-scoping/171784 "2009-07-09T08:26:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [July 9, 2009, 8:26am UTC](https://www.ruby-forum.com/t/the-simpelest-of-module-function-scoping/171784/1 "2009-07-09T08:26:24Z")

</div>

It’s a little sad that I’ve written many lines of ruby code, but I  
can’t something as simple as this to work:

#! /usr/bin/env ruby  
module T

def t(x)  
puts x  
end

T::t(5)  
end

> > ./t.rb

./t.rb:8: undefined method `t’ for T:Module (NoMethodError)

Obviously, I’ve tried different incantations, like t(5), T.t(5) etc.  
No sigar… I must be missing something really trivial…

Tom

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [July 9, 2009, 8:31am UTC](https://www.ruby-forum.com/t/the-simpelest-of-module-function-scoping/171784/2 "2009-07-09T08:31:04Z")

</div>

As happens so many times, a few tries later, I figured out that I need  
to do this:

…  
def T::t(x)  
…

And then all works fine.

Which leads to the follow: why was the language designed this way? It  
all seems a bit redundant.

And if I still keep the original def t(x), how can it be used?

Thanks,  
Tom

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [July 9, 2009, 8:36am UTC](https://www.ruby-forum.com/t/the-simpelest-of-module-function-scoping/171784/3 "2009-07-09T08:36:20Z")

</div>

Tom wrote:

> And if I still keep the original def t(x), how can it be used?

You can “mix” it into a class:

class C  
include T  
def foo; t(…); end  
end

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [July 9, 2009, 8:50am UTC](https://www.ruby-forum.com/t/the-simpelest-of-module-function-scoping/171784/4 "2009-07-09T08:50:56Z")

</div>

> … or see ri module\_function.

Interesting. I had no idea this existed. I think I’ll need to remove  
the big layer of dust from my ‘Ruby for Rails’ book and restudy these  
things again.

Tom

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [July 9, 2009, 8:34am UTC](https://www.ruby-forum.com/t/the-simpelest-of-module-function-scoping/171784/5 "2009-07-09T08:34:34Z")

</div>

Tom Verbeure wrote:

> It’s a little sad that I’ve written many lines of ruby code, but I  
> can’t something as simple as this to work:
> 
> #! /usr/bin/env ruby  
> module T
> 
> def t(x)

```
  def T.t(x)

```

or  
def self.t(x)

or wrap in class \<\< self…end, or see ri module\_function.

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [July 9, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/the-simpelest-of-module-function-scoping/171784/6 "2009-07-09T17:28:19Z")

</div>

Tom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> all seems a bit redundant.  
> Actually it’s extraordinary simple and beautiful. Read my Ruby intro:

[http://www.apeth.com/rubyIntro/justenoughruby.html](http://www.apeth.com/rubyIntro/justenoughruby.html)

Read fairly quickly (since you probably know it all, already) just up to  
section 1.13 (“The truth? You couldn’t handle the truth!”). You will at  
that point suddenly understand this syntax and why it is needed. m.
