Forum: Ruby Undefined method error

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.
24c6e4ed08f7a4bf9cfb7edc2dbd14b8?d=identicon&s=25 Patrick Spence (pkspence)
on 2006-05-19 16:26
Still trying to get my head wrapped around the concepts regarding
Modules, Classes and their methods.

Once an instance of the MyErrorLogger::ErrorLogger class has been
instanciated, shouldn't Komodo be displaying the intellisense for the
object? Using the example below, when I type "errorLogger.", why isn't a
list of the properties and methods of the object displayed?

Back on topic, consider the following...

#-- myErrorLogger.rb
module MyErrorLogger

  class ErrorLogger

    def initialize()
      puts "Instance of MyErrorLogger::ErrorLogger has been created"
    end

    def ErrorLogger.PostError(ex)
      puts "Exception error message: " + ex.to_s
    end

  end # class ErrorLogger

end # module MyErrorLogger


#-- testErrorLogger.rb
require 'myErrorLogger'
include MyErrorLogger

def testBed

  #-- message is displayed in console window as expected
  errorLogger = MyErrorLogger::ErrorLogger.new

  #-- "undefined method 'PostError'" error gets thrown on this line
  errorLogger.PostError(Exception.new("test error message"))

end

#-- fire it up
testBed
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-19 16:59
(Received via mailing list)
2006/5/19, Patrick Spence <patrick@pkspence.com>:
> #-- myErrorLogger.rb
>     end
> def testBed
>
>   #-- message is displayed in console window as expected
>   errorLogger = MyErrorLogger::ErrorLogger.new
>
>   #-- "undefined method 'PostError'" error gets thrown on this line
>   errorLogger.PostError(Exception.new("test error message"))

PostError is a method of MyErrorLogger::ErrorLogger, i.e. of the class
instance.  errorLogger is an instance of MyErrorLogger::ErrorLogger so
it cannot know this method.

Kind regards

robert
24c6e4ed08f7a4bf9cfb7edc2dbd14b8?d=identicon&s=25 Patrick Spence (pkspence)
on 2006-05-19 18:22
Robert Klemme wrote:
> PostError is a method of MyErrorLogger::ErrorLogger, i.e. of the class
> instance.  errorLogger is an instance of MyErrorLogger::ErrorLogger so
> it cannot know this method.
>
> Kind regards
>
> robert

I understood that when an instance of a Method::Class has been created,
the resulting object would inherit all the methods and properties it was
based on. Now I'm confused!
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-19 18:38
(Received via mailing list)
2006/5/19, Patrick Spence <patrick@pkspence.com>:
> the resulting object would inherit all the methods and properties it was
> based on. Now I'm confused!

It seems so.  What is a "Method::Class" in your terminology?

Ok, I try again.  All classes are objects but not all objects are
classes.  A class defines instance methods. These methods can be used
in instances of the class and instances of sub classes.  Every object
can also have singleton methods (there are other names for them as
well but I'll stick with the convention Matz has founded); singleton
methods are present in that single instance only, there is no
inheritance associated with them whatsoever but you can find them in
the singleton class (every object has one and the singleton class has
only one instance). Since a class is an object it can have singleton
methods. These do roughly the same as static methods in Java, i.e. the
operate on the class level.  Some code:

irb(main):001:0> class Foo
irb(main):002:1> def bar() # instance method
irb(main):003:2> "bar"
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> Foo.new.bar
=> "bar"
irb(main):007:0> Foo.bar
NoMethodError: undefined method `bar' for Foo:Class
        from (irb):7
        from :0
irb(main):008:0> f=Foo.new
=> #<Foo:0x3d0238>
irb(main):009:0> f.bar
=> "bar"
irb(main):010:0> def f.special() # singleton method
irb(main):011:1> "buh!"
irb(main):012:1> end
=> nil
irb(main):013:0> f.special
=> "buh!"
irb(main):014:0> Foo.new.special
NoMethodError: undefined method `special' for #<Foo:0x3b5b38>
        from (irb):14
        from :0
irb(main):015:0> def Foo.nana() # singleton method
irb(main):016:1> "nana"
irb(main):017:1> end
=> nil
irb(main):018:0> Foo.nana
=> "nana"
irb(main):019:0> Foo.new.nana
NoMethodError: undefined method `nana' for #<Foo:0x3a8ff8>
        from (irb):19
        from :0

Hope it helps...

robert
24c6e4ed08f7a4bf9cfb7edc2dbd14b8?d=identicon&s=25 Patrick Spence (pkspence)
on 2006-05-19 19:08
Robert Klemme wrote:
> 2006/5/19, Patrick Spence <patrick@pkspence.com>:
>> the resulting object would inherit all the methods and properties it was
>> based on. Now I'm confused!
>
> It seems so.  What is a "Method::Class" in your terminology?

Oops, I meant "Module::Class". As for your reply, thanks! I need some
time to digest it.
This topic is locked and can not be replied to.