Ruby Module Method Access

前端 未结 3 1978
野的像风
野的像风 2021-01-31 17:01

I have a ruby module for constants. It has a list of variables and 1 method which applies formatting. I can\'t seem to access the method in this module. Any idea why?

3条回答
  •  执笔经年
    2021-01-31 17:32

    module Foo
      def self.hello # This is a class method
        puts "self.hello"
      end
    
      def hello # When you include this module, it becomes an instance method 
        puts "hello"
      end
    end
    
    Foo.hello #=> self.hello
    
    class Bar
      include Foo
    end
    
    Bar.new.hello #=> hello
    

提交回复
热议问题