Module: Nummy::ConstEnumerable

Includes:
Enumerable
Included in:
OrderedConstEnumerable
Defined in:
lib/nummy/const_enumerable.rb

Overview

Note:

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

Instance Method Details

#each_const_name {|name| ... } ⇒ self #each_const_nameEnumerator<Symbol>

Iterates through the names of the constants in self.

Overloads:

  • #each_const_name {|name| ... } ⇒ self

    Calls the given block with the name of each constant in self.

    Yield Parameters:

    • name (Symbol)

    Returns:

    • (self)
  • #each_const_nameEnumerator<Symbol>

    Returns an Enumerator that calls the given block with the name of each constant in self.

    Returns:

    • (Enumerator<Symbol>)


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_pairEnumerator<Array<Symbol, Object>> Also known as: each

Note:

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.

Overloads:

  • #each_const_pair {|name, value| ... } ⇒ self

    Calls the given block with a two-item array containing the name and value of each constant in self.

    The order of the pairs is guaranteed to be the same as the insertion order of the constants.

    Yield Parameters:

    • name (Symbol)
    • value (Object)

    Returns:

    • (self)
  • #each_const_pairEnumerator<Array<Symbol, Object>>

    Returns an Enumerator that iterates over with a two-item array containing the name and value of each constant in self.

    Returns:

    • (Enumerator<Array<Symbol, Object>>)


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_valueEnumerator<Object>

Iterates through the values of the constants in self.

Overloads:

  • #each_const_value {|value| ... } ⇒ self

    Calls the given block with the value of each constant in self.

    The order of the values is guaranteed to be the same as the insertion order of the constants.

    Yield Parameters:

    • value (Object)

    Returns:

    • (self)
  • #each_const_valueEnumerator<Object>

    Returns an Enumerator that calls the given block with the value of each constant in self.

    Returns:

    • (Enumerator<Object>)


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