class Object

Constants

SymbolizedHash

Implements a hash where keys :foo and "foo" are considered to be the same.

rgb = SymbolizedHash.new

rgb[:black] = '#000000'
rgb[:black]  # => '#000000'
rgb['black'] # => '#000000'

rgb['white'] = '#FFFFFF'
rgb[:white]  # => '#FFFFFF'
rgb['white'] # => '#FFFFFF'

Internally strings are mapped to symbols when used as keys in the entire writing interface (calling []=, merge, etc). This mapping belongs to the public interface. For example, given:

hash = SymbolizedHash.new('a' => 1)

You are guaranteed that the key is returned as a symbol:

hash.keys # => [:a]

Technically other types of keys are accepted:

hash = SymbolizedHash.new('a' => 1)
hash[0] = 0
hash # => { :a => 1, 0 => 0 }

but this class is intended for use cases where strings or symbols are the expected keys and it is convenient to understand both as the same. For example, processing data throught a multi-step pipeline where steps can be written by other people.

Note that core extensions define Hash#to_symbolized_hash:

rgb = { black: '#000000', 'white' => '#FFFFFF' }.to_symbolized_hash

which may be handy.

Public Instance Methods

deep_dup() click to toggle source

Returns a deep copy of object if it's duplicable. If it's not duplicable, returns self.

object = Object.new
dup    = object.deep_dup
dup.instance_variable_set(:@a, 1)

object.instance_variable_defined?(:@a) # => false
dup.instance_variable_defined?(:@a)    # => true
# File lib/symbolized/core_ext/object/deep_dup.rb, line 14
def deep_dup
  duplicable? ? dup : self
end
duplicable?() click to toggle source

Can you safely dup this object?

False for nil, false, true, symbol, number and BigDecimal(in 1.9.x) objects; true otherwise.

# File lib/symbolized/core_ext/object/duplicable.rb, line 25
def duplicable?
  true
end