krunal shah
krunal shah

Reputation: 16339

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
      end
    end
  end
end

class abc
  include Test1::Test2
end

Where can I declare constant ? How ?

Upvotes: 1

Views: 4800

Answers (2)

rookieRailer
rookieRailer

Reputation: 2341

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.

Upvotes: 0

emboss
emboss

Reputation: 39620

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

Upvotes: 7

Related Questions