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 ]

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Arne Hartherz
Last edit:
about 1 month 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