Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling parent module methods from a nested class

Tags:

ruby

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?

like image 237
ks_ Avatar asked Aug 07 '09 06:08

ks_


1 Answers

#!/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
like image 108
Simone Carletti Avatar answered Oct 14 '22 22:10

Simone Carletti