# Module, class and methods confusion

**URL:** <https://www.ruby-forum.com/t/module-class-and-methods-confusion/60798>\
**Category:** Ruby\
**Created:** [May 17, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/module-class-and-methods-confusion/60798 "2006-05-17T16:14:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkspence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@pkspence](https://www.ruby-forum.com/u/pkspence)\
**Post date:** [May 17, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/module-class-and-methods-confusion/60798/1 "2006-05-17T16:14:39Z")

</div>

I have a test harness script that requires module “mhiqErrorLogger”.  
Within the test script, I’m attempting to call a method within a class  
in the “mhiqErrorLogger” module and am getting a “undefined method”  
error. Obviously, I’ve got something wrong here, but haven’t a clue as  
to what. Can someone please point me in the right direction?

* * *

## contents of test harness script:

require ‘dbi’  
require ‘mhiqErrorLogger’

begin

dbh = DBI.connect(“dbi:ADO:Provider=sqloledb;Data  
Source=(local);Initial Catalog=portal;User ID=xxxx;Password=xxxx”)

#-- intentionally throw a “divide by zero” error for testing  
intError = 1/0

rows = dbh.select\_all(“SELECT name FROM portal\_contacts WHERE name IS  
NOT NULL ORDER BY name”)

if !rows.empty?  
puts " Results returned by query"  
rows.each do |row|  
puts " Name: " + row[“name”].to\_s  
end  
else  
puts “Query returned no results”  
end

rescue Exception =\> ex  
#-- “undefined method” error gets thrown on this line!  
MHIQErrorLogger::ErrorLogger::PostError(ex, dbh)

ensure  
if (!dbh.nil?)  
dbh.disconnect  
end

end

* * *

## contents of mhiqErrorLogger.rb:

require ‘dbi’

module MHIQErrorLogger

class ErrorLogger

```
def PostError(ex,dbh)

  errorMessage = ex.message
  errorMessage = errorMessage.gsub(13.chr,"")
  errorMessage = errorMessage.gsub(10.chr,"")
  errorMessage = errorMessage.gsub(/'/,"`")

  if (dbh.nil?)

    #--
    #-- TODO: write error information to an external text file
    #--
    puts 'No database connection present'

  #-- database connection present
  else

    #-- retrieve name of the currently running script, translate all
    #-- single quotes to backticks to prevent errors when INSERTed
    scriptName = $0.gsub(/'/,"`")

    begin

      sth = dbh.prepare("INSERT INTO mhiqErrorLog (errormessage, 

```

scriptname) VALUES (?, ?)")  
sth.execute(errorMessage, scriptName)

```
      row = dbh.select_one("SELECT ident_current('mhiqErrorLog') as 

```

[newId]")  
newId = row[“newId”].to\_i

```
      sth = dbh.prepare("INSERT INTO mhiqErrorStackTrace (errorID, 

```

modulelinenumber) VALUES (?, ?)")

```
      $@.each {|ele| sth.execute(newId, ele.to_s)}
      dbh.commit

    rescue Exception => ex

      dbh.rollback
      puts ex.message

    end

  end

end ## def PostError

```

end ## class ErrorLogger

end ## module MHIQErrorLogger

---

<div class="post-metadata">

**Author:** ![pkspence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@pkspence](https://www.ruby-forum.com/u/pkspence)\
**Post date:** [May 17, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/module-class-and-methods-confusion/60798/2 "2006-05-17T16:38:45Z")

</div>

Patrick S. wrote:

> rescue Exception =\> ex  
> #-- “undefined method” error gets thrown on this line!  
> MHIQErrorLogger::ErrorLogger::PostError(ex, dbh)

Just a guess, but should that be

```
MHIQErrorLogger:ErrorLogger::PostError.new(ex, dbh)
```

---

<div class="post-metadata">

**Author:** ![pkspence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@pkspence](https://www.ruby-forum.com/u/pkspence)\
**Post date:** [May 17, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/module-class-and-methods-confusion/60798/3 "2006-05-17T16:45:09Z")

</div>

Tim H. wrote:

> Just a guess, but should that be
> 
> ```
> MHIQErrorLogger:ErrorLogger::PostError.new(ex, dbh)
> 
> ```

I’m getting the following error:  
ErrorLogger is not a class/module

I’m new to Ruby, obviously, and still trying to get my head wrapped  
around some of it’s concepts and syntax.

---

<div class="post-metadata">

**Author:** ![pkspence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@pkspence](https://www.ruby-forum.com/u/pkspence)\
**Post date:** [May 17, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/module-class-and-methods-confusion/60798/4 "2006-05-17T16:50:43Z")

</div>

2006/5/17, Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Patrick S. wrote:
> 
> > rescue Exception =\> ex  
> > #-- “undefined method” error gets thrown on this line!  
> > MHIQErrorLogger::ErrorLogger::PostError(ex, dbh)
> 
> Just a guess, but should that be
> 
> ```
> MHIQErrorLogger:ErrorLogger::PostError.new(ex, dbh)
> 
> ```

No, it’s a method. The error is more likely here:

module MHIQErrorLogger

class ErrorLogger

def PostError(ex,dbh)

Should be

def self.PostError(ex,dbh)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![pkspence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@pkspence](https://www.ruby-forum.com/u/pkspence)\
**Post date:** [May 17, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/module-class-and-methods-confusion/60798/5 "2006-05-17T17:12:51Z")

</div>

Robert K. wrote:

> No, it’s a method. The error is more likely here:
> 
> module MHIQErrorLogger
> 
> class ErrorLogger
> 
> def PostError(ex,dbh)
> 
> Should be
> 
> def self.PostError(ex,dbh)
> 
> Cheers
> 
> robert

Made the change in the method definition, but getting same error:  
“ErrorLogger is not a class/module”

Now I’m confused as to how it should be called…  
MHIQErrorLogger::ErrorLogger.PostError.new(ex, dbh)  
or  
MHIQErrorLogger::ErrorLogger::PostError.new(ex, dbh)

---

<div class="post-metadata">

**Author:** ![pkspence](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@pkspence](https://www.ruby-forum.com/u/pkspence)\
**Post date:** [May 17, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/module-class-and-methods-confusion/60798/6 "2006-05-17T17:38:00Z")

</div>

Patrick S. wrote:

> Made the change in the method definition, but getting same error:  
> “ErrorLogger is not a class/module”
> 
> Now I’m confused as to how it should be called…  
> MHIQErrorLogger::ErrorLogger.PostError.new(ex, dbh)  
> or  
> MHIQErrorLogger::ErrorLogger::PostError.new(ex, dbh)

OK, got it! This works…  
MHIQErrorLogger::ErrorLogger.PostError(ex, dbh)

Tim, Robert… thanks for the help!
