Posted almost 9 years ago. Visible to the public. Repeats.

How to define constants with traits

This is pretty much like defining subclasses through traits with the Modularity gem.

module MyConstantTrait as_trait do self::MY_CONSTANT = 'hello universe' end end

This would set MY_CONSTANT for any class using the trait.

Of course you can also use parameters (for logic or for setting values, like here):

module DoesValues as_trait do |*args| self::VALUES = args end end class Foo include DoesValues[:foo, :bar, :baz] end Foo::VALUES => [ :foo, :bar, :baz ]
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Arne Hartherz
Last edit:
5 months ago
by Jakob Scholz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more