# Uninitialized constant- when trying to calculate

**URL:** <https://www.ruby-forum.com/t/uninitialized-constant-when-trying-to-calculate/264552>\
**Category:** Development\
**Created:** [January 22, 2024, 10:39am UTC](https://www.ruby-forum.com/t/uninitialized-constant-when-trying-to-calculate/264552 "2024-01-22T10:39:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xlorate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/46a35a/32.png) [@xlorate](https://www.ruby-forum.com/u/xlorate)\
**Post date:** [January 22, 2024, 10:39am UTC](https://www.ruby-forum.com/t/uninitialized-constant-when-trying-to-calculate/264552/1 "2024-01-22T10:39:59Z")

</div>

I am trying to use three modules,

First\_Module.rb

```auto
module Raw_Data
  A = 10.0
  B = 20.0
end
```

Second\_Module.rb

```auto
$LOAD_PATH << '.'
require "First_Module.rb"
module Do_Calc
  x= Raw_Data::A
  y= Raw_Data::B
  x = x * 12.0
  y = y * 1.5
  puts x
  puts y
  def Do_It.one(aa)
    aaa = aa * x
    puts (aaa)
  end

  def Do_It.two(bb)
    bbb = bb* y
    puts (bbb)
  end

end

```

Final\_Result.rb

```auto
$LOAD_PATH << '.'

require "Second_Module.rb"

Do_It.one(2)

Do_It.two(10)
```

On the second module the code does show puts x & puts y

I get an error on the next line

> def Do\_It.one(aa)

“uninitialized constant Do\_Calc::Do\_It (NameError)”

How can this be fixed?

---

<div class="post-metadata">

**Author:** ![robert-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@robert-b](https://www.ruby-forum.com/u/robert-b)\
**Post date:** [January 22, 2024, 10:40am UTC](https://www.ruby-forum.com/t/uninitialized-constant-when-trying-to-calculate/264552/2 "2024-01-22T10:40:11Z")

</div>

Hi Dave,

The error occurs because you’re calling “Do\_It.one(aa)” but “Do\_It” is not defined anywhere in your code.  
To fix your error, you can define “Do\_It” inside the “Do\_Calc” module as a class and put “one” and “two” methods in the “Do\_It” class:

```ruby
module Do_Calc
  class Do_It
  
    x = Raw_Data::A * 12.0
    y = Raw_Data::B * 1.5
  
    def self.one(aa)
      aaa = aa * x
      puts aaa
    end
  
    def self.two(bb)
      bbb = bb * y
      puts bbb
    end
  
  end
end

```

In your Final\_Result.rb, you can use these methods as below:

```ruby
Do_Calc::Do_It.one(2)
Do_Calc::Do_It.two(10)

```

Just to note that, if you want to use variable `x` or `y` inside your methods, you may need to move these two lines `x = Raw_Data::A * 12.0` and `y = Raw_Data::B * 1.5` inside your methods or define `x` and `y` as class variables using `@@`.

I hope this helps!

Best,  
Bobby the Bot

---

<div class="post-metadata">

**Author:** ![xlorate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/46a35a/32.png) [@xlorate](https://www.ruby-forum.com/u/xlorate)\
**Post date:** [January 22, 2024, 11:03am UTC](https://www.ruby-forum.com/t/uninitialized-constant-when-trying-to-calculate/264552/3 "2024-01-22T11:03:05Z")

</div>

That did it thanks:.xlorate
