Module: Nummy::ConstEnumerable
Overview
If stable ordering is important, you should use OrderedConstEnumerable.
A module that can be extended in order to enumerate over constants in a Hash-like manner. This can be used to build enum-like classes and modules that work nicely with static analysis.
The enumeration order for methods defined by this module is not guaranteed to be stable across calls and methods. This is because those constants are looked up using Module#constants, and that method does not have any ordering guarantees.
Instance Method Summary collapse
-
#each_const_name ⇒ Object
Iterates through the names of the constants in
self. -
#each_const_pair ⇒ Object
(also: #each)
Iterates through the name-value pairs of the constants in
self. -
#each_const_value ⇒ Object
Iterates through the values of the constants in
self.
Instance Method Details
#each_const_name {|name| ... } ⇒ self #each_const_name ⇒ Enumerator<Symbol>
Iterates through the names of the constants in self.
33 34 35 36 37 38 |
# File 'lib/nummy/const_enumerable.rb', line 33 def each_const_name(&) return enum_for(__method__) unless block_given? nummy_constants.each(&) self end |
#each_const_pair {|name, value| ... } ⇒ self #each_const_pair ⇒ Enumerator<Array<Symbol, Object>> Also known as: each
This method is used as the basis for the enumeration methods provided by the Enumerable module.
Iterates through the name-value pairs of the constants in self.
84 85 86 87 88 89 |
# File 'lib/nummy/const_enumerable.rb', line 84 def each_const_pair return enum_for(__method__) unless block_given? each_const_name { |name| yield name, nummy_const_get(name) } self end |
#each_const_value {|value| ... } ⇒ self #each_const_value ⇒ Enumerator<Object>
Iterates through the values of the constants in self.
56 57 58 59 60 61 |
# File 'lib/nummy/const_enumerable.rb', line 56 def each_const_value return enum_for(__method__) unless block_given? each_const_name { |name| yield nummy_const_get(name) } self end |