2011-08-04 8 views
1
module Test1 
    module Test2 
    def self.included(base) 
     # Where can I declare constant ? How ? 
     base.extend ClassMethods 
     base.class_eval do 
      # named scopes 
     end 
    end 
    end 
end 

class abc 
    include Test1::Test2 
end 

どこで定数を宣言できますか?どうやって ?定数内部モジュールの書き方は?

答えて

6

私は理解していない - あなたはこのことを意味した:Rubyで

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 
0

を、大文字で始まる変数は、定数と考えられています。したがって、定数を宣言するために変数をPi = 3.14として使用することがあります。

+0

'はじまり'ではありませんが、すべてがCAPS – Tim

+0

です。@Tim、rookieRailerは正しいです。 Ruby定数* start_with *大文字。 クラスは実際にはRubyの定数です。 – olivervbk

関連する問題