ks_
ks_

Reputation: 208

Calling parent module methods from a nested class

I'm having trouble figuring out how to call a method from a parent module in a class.

I want to call module functions from parent module in my nested classes, but can't seem to find a way how to do this.

example:

module Awesome
  class Checker
    def awesome?
      awesome_detection
    end
  end

  module_function
  def awesome_detection
    true
  end

end

If I call Awesome::Checker.new.awesome?, it's unaware of awesome_detection

Any ideas on what I'm missing?

Upvotes: 16

Views: 7406

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176402

#!/usr/bin/env ruby -wKU

module Awesome

  class Checker
    def awesome?
      Awesome.awesome_detection
    end
  end

  def self.awesome_detection
    puts "yes"
  end

end

Awesome::Checker.new.awesome?
# => yes

Upvotes: 24

Related Questions