# Class \<\< Foo; include Foo; end

**URL:** <https://www.ruby-forum.com/t/class-foo-include-foo-end/91429>\
**Category:** Ruby\
**Created:** [April 9, 2007, 1:10am UTC](https://www.ruby-forum.com/t/class-foo-include-foo-end/91429 "2007-04-09T01:10:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [April 9, 2007, 1:10am UTC](https://www.ruby-forum.com/t/class-foo-include-foo-end/91429/1 "2007-04-09T01:10:36Z")

</div>

Hello,

how should I group some stateless functions into a module and use them  
in a very simple way without littering the namespace? E.g. if the  
module is ``Foo’’ with at least one function foo, then I’d like to use  
that module in a way as simple like that:

require ‘Foo’  
puts Foo.foo(“world”)

The first solution that I’ve found is to write Foo.rb like follows:

module Foo  
def foo(s)  
“Hello, #{s}\n”  
end  
end

class \<\< Foo  
include Foo  
end

Is that Ok? Or is there even a simpler way?

Regards  
Thomas

---

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [April 9, 2007, 1:30am UTC](https://www.ruby-forum.com/t/class-foo-include-foo-end/91429/2 "2007-04-09T01:30:37Z")

</div>

On Apr 8, 2007, at 7:10 PM, Thomas H. wrote:

> end  
> That is fine. There are several variations:

module Foo  
def foo(s)  
“Hello, #{s}\n”  
end  
extend self  
end

Your example and my variation enable the module to  
respond to _all_ of its instance methods.

If you only want the module to respond to some of its instance  
methods you’ll want to look at the following variations:

module Foo  
def foo(s)  
“Hello, #{s}\n”  
end  
module\_function :foo  
end

or

module Foo;end  
def Foo.foo(s)  
“Hello, #{s}\n”  
end

Gary W.

---

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [April 9, 2007, 6:25am UTC](https://www.ruby-forum.com/t/class-foo-include-foo-end/91429/3 "2007-04-09T06:25:57Z")

</div>

On Apr 8, 5:05 pm, Thomas H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how should I group some stateless functions into a module and use them  
> in a very simple way without littering the namespace? E.g. if the  
> module is ``Foo’’ with at least one function foo, then I’d like to use  
> that module in a way as simple like that:
> 
> require ‘Foo’  
> puts Foo.foo(“world”)

module Foo  
def self.foo( msg )  
puts “Hello #{msg}”  
end  
end
