module Hints
module Designer
def message
"Hello, World!"
end
end
end
p Hints::Designer.message
Why this give me the following error...?
undefined method `message' for Hints::Designer:Module (NoMethodError)
module Hints
module Designer
def message
"Hello, World!"
end
end
end
p Hints::Designer.message
Why this give me the following error...?
undefined method `message' for Hints::Designer:Module (NoMethodError)
What you need to do is define message as a class method (not as an instance method). Try:
module Hints
module Designer
def self.message
"Hello, World!"
end
end
end
puts Hints::Designer.message
If you want to use instance methods with a module, you must extend an object using the module and any given constraints (see http://ruby-doc.org/core/classes/Module.html).
use p Hints::Designer.new.message
message is an instance method not a class method
You want to use this:
module Hints
module Designer
def self.message
"Hello, World!"
end
end
end
That is, make message
a "class" method (before it was an instance method). This seems a bit weird, but keep in mind that modules are often used to create mixin classes, so an "instance of a module" makes sense in that context.