Question

Given the following example:

module A
  module B
    def whoa
      puts 'Whoa!'
    end
  end
end

How can I access the whoa method?

1.9.3p392 :047 > A.B.whoa
NoMethodError: undefined method `B' for A:Module

1.9.3p392 :048 > A::B.whoa
NoMethodError: undefined method `whoa' for A::B:Module

1.9.3p392 :049 > A::B::whoa
NoMethodError: undefined method `whoa' for A::B:Module

None of these approaches seems to work.

Was it helpful?

Solution

Assuming you don't want class level methods, you can also include the module into a class, instantiate an object of that class and call whoa:

class C
   include A::B
end

c = C.new
c.whoa
# Whoa!

OTHER TIPS

You've define the method as an instance method. If you want to use the method without an instance, it should be a class method on the module:

module A
  module B
    def self.whoa
      puts 'Whoa!'
    end
  end
end

1.9.3p327 :009 > A::B.whoa
Whoa!
 => nil
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top