# Beginning question. modules + object creation

**URL:** <https://www.ruby-forum.com/t/beginning-question-modules-object-creation/89232>\
**Category:** Ruby\
**Created:** [March 18, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/beginning-question-modules-object-creation/89232 "2007-03-18T23:38:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 18, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/beginning-question-modules-object-creation/89232/1 "2007-03-18T23:38:48Z")

</div>

I was wondering if i create a modules file called mathematics.rb:

module Mathematics  
class Add  
def add(operand\_one, operand\_two)  
return operand\_one + operand\_two  
end

end#end class  
end

and then i create another file called usemodules.rb

require ‘mathematics’

adder = Add.new  
puts "2 + 3 = " + adder.add(2, 3).to\_s

how do i create an Add object in this new file, i understand i could  
just make the add method above into a class method and then i wouldnt  
need to create an object at all. However how would i create an object to  
use.

thanks

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 18, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/beginning-question-modules-object-creation/89232/2 "2007-03-18T23:41:33Z")

</div>

Corey K. wrote:

> I was wondering if i create a modules file called mathematics.rb:
> 
> module Mathematics  
> class Add  
> def add(operand\_one, operand\_two)  
> return operand\_one + operand\_two  
> end
> 
> end#end class  
> end
> 
> and then i create another file called usemodules.rb
> 
> require ‘mathematics’
> 
> adder = Add.new  
> puts "2 + 3 = " + adder.add(2, 3).to\_s
> 
> how do i create an Add object in this new file, i understand i could  
> just make the add method above into a class method and then i wouldnt  
> need to create an object at all. However how would i create an object to  
> use.
> 
> thanks

never mind i figured it out on my own i would do it like  
Mathematics::Add.new  
zen and the art of programming as soon as i shut my brain off by asking  
a question the answer pops in my head, lol.

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 18, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/beginning-question-modules-object-creation/89232/3 "2007-03-18T23:58:30Z")

</div>

On Mar 18, 2007, at 6:38 PM, Corey K. wrote:

> and then i create another file called usemodules.rb
> 
> require ‘mathematics’
> 
> adder = Add.new  
> puts "2 + 3 = " + adder.add(2, 3).to\_s

Because you nested your class Add within Mathematics you have to  
use the fully qualified name when you are writing code that  
is outside the Mathematics module:

adder = Mathematics::Add.new  
puts “2 + 3 = #{adder.add(2,3)}”

Gary W.
