Forum: Ruby Module, class and methods confusion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Patrick S. (Guest)
on 2006-05-17 18:14
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
Tim H. (Guest)
on 2006-05-17 18:38
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)
Patrick S. (Guest)
on 2006-05-17 18:45
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.
Robert K. (Guest)
on 2006-05-17 18:50
(Received via mailing list)
2006/5/17, Tim H. <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
Patrick S. (Guest)
on 2006-05-17 19:12
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)
Patrick S. (Guest)
on 2006-05-17 19:38
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!
This topic is locked and can not be replied to.