开发者

How to write constant inside module?

module Test1
  module Test2
    def self.included(base)
      # Where can I declare constant ? How ?
      base.extend ClassMethods
      base.class_eval do
           # named scopes
      开发者_StackOverflow中文版end
    end
  end
end

class abc
  include Test1::Test2
end

Where can I declare constant ? How ?


I'm not sure I understand - did you mean this:

module Test1
  module Test2
    CONSTANT = 5
    def self.included(base)
      # Where can I declare constant ? How ?
      base.extend ClassMethods
      base.class_eval do
       # named scopes
      end
    end

    module ClassMethods
    end
  end
end

class A
  include Test1::Test2
end

puts A::CONSTANT # => 5


In Ruby, a variable that starts with a capital letter, is considered a constant. So, you might use a variable as Pi = 3.14 to declare a constant value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜