Posted over 8 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.

Copy
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):

Copy
module DoesValues as_trait do |*args| self::VALUES = args end end class Foo include DoesValues[:foo, :bar, :baz] end Foo::VALUES => [ :foo, :bar, :baz ]

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
15 days 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