Sha256: f32372be156c9039eb65e9d8c9e1281307e5a6c138f09f3f98e0d944958ea9c9

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

require 'set'

module Dry
  module Core
    # A list of constants you can use to avoid memory allocations or identity checks.
    #
    # @example Just include this module to your class or module
    #   class Foo
    #     def call(value = EMPTY_ARRAY)
    #        value.map(&:to_s)
    #     end
    #   end
    #
    # @api public
    module Constants
      # An empty array
      EMPTY_ARRAY = [].freeze
      # An empty hash
      EMPTY_HASH = {}.freeze
      # An empty list of options
      EMPTY_OPTS = {}.freeze
      # An empty set
      EMPTY_SET = Set.new.freeze
      # An empty string
      EMPTY_STRING = ''.freeze

      # A special value you can use as a default to know if no arguments
      # were passed to you method
      #
      # @example
      #   def method(value = Undefined)
      #     if value == Undefined
      #       puts 'no args'
      #     else
      #       puts value
      #     end
      #   end
      Undefined = Object.new.tap do |undefined|
        def undefined.to_s
          'Undefined'
        end

        def undefined.inspect
          'Undefined'
        end
      end.freeze
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-core-0.2.0 lib/dry/core/constants.rb
dry-core-0.1.0 lib/dry/core/constants.rb