Module: Dry::Core::Constants

Included in:
ClassAttributes
Defined in:
lib/dry/core/constants.rb

Overview

A list of constants you can use to avoid memory allocations or identity checks.

Examples:

Just include this module to your class or module

class Foo
  def call(value = EMPTY_ARRAY)
     value.map(&:to_s)
  end
end

Constant Summary collapse

EMPTY_ARRAY =

An empty array

[].freeze
EMPTY_HASH =

An empty hash

{}.freeze
EMPTY_OPTS =

An empty list of options

{}.freeze
EMPTY_SET =

An empty set

Set.new.freeze
EMPTY_STRING =

An empty string

''.freeze
Undefined =

A special value you can use as a default to know if no arguments were passed to you method

Examples:

def method(value = Undefined)
  if value == Undefined
    puts 'no args'
  else
    puts value
  end
end
Object.new.tap do |undefined|
  def undefined.to_s
    'Undefined'
  end

  def undefined.inspect
    'Undefined'
  end
end.freeze

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/dry/core/constants.rb', line 48

def self.included(base)
  super

  constants.each do |const_name|
    base.const_set(const_name, const_get(const_name))
  end
end