Posted over 10 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 ]

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Arne Hartherz
Last edit:
14 days ago
by Henning Koch
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 short-lived cookies to improve usability.
Accept or learn more