123456789_123456789_123456789_123456789_123456789_

Class: SymbolHash

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Hash
Instance Chain:
self, ::Hash
Inherits: Hash
  • Object
Defined in: lib/yard/core_ext/symbol_hash.rb

Overview

A subclass of ::Hash where all keys are converted into Symbols, and optionally, all ::String values are converted into Symbols.

Class Method Summary

::Hash - Inherited

.[]

Alias for Hash.create.

.create,
.create_186

Alias for Hash.[].

Instance Method Summary

Constructor Details

.new(symbolize_value = true) ⇒ SymbolHash

Creates a new SymbolHash object

Parameters:

  • symbolize_value (Boolean) (defaults to: true)

    converts any ::String values into Symbols if this is set to true.

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 9

def initialize(symbolize_value = true)
  @symbolize_value = symbolize_value
end

Class Method Details

.[](hash) ⇒ SymbolHash .[](*list) ⇒ SymbolHash

Overloads:

  • .[](hash) ⇒ SymbolHash

    Creates a SymbolHash object from an existing ::Hash

    Examples:

    SymbolHash['x' => 1, :y => 2] # => #<SymbolHash:0x...>

    Parameters:

    • hash (Hash)

      the hash object

    Returns:

    • (SymbolHash)

      a new SymbolHash from a hash object

  • .[](*list) ⇒ SymbolHash

    Creates a SymbolHash from an even list of keys and values

    Examples:

    SymbolHash[key1, value1, key2, value2, ...]

    Parameters:

    • list (Array)

      an even list of key followed by value

    Returns:

    • (SymbolHash)

      a new SymbolHash object

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 28

def self.[](*hsh)
  obj = new
  if hsh.size == 1 && hsh.first.is_a?(Hash)
    hsh.first.each {|k, v| obj[k] = v }
  else
    0.step(hsh.size, 2) {|n| obj[hsh[n]] = hsh[n + 1] }
  end
  obj
end

Instance Method Details

#[](key) ⇒ Object

Accessed a symbolized key

Parameters:

  • key (#to_sym)

    the key to access

Returns:

  • (Object)

    the value associated with the key

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 49

def [](key) super(key.to_sym) end

#[]=(key, value)

Assigns a value to a symbolized key

Parameters:

  • key (#to_sym)

    the key

  • value (Object)

    the value to be assigned. If this is a ::String and values are set to be symbolized, it will be converted into a Symbol.

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 42

def []=(key, value)
  super(key.to_sym, value.instance_of?(String) && @symbolize_value ? value.to_sym : value)
end

#delete(key) ⇒ void

This method returns an undefined value.

Deleted a key and value associated with it

Parameters:

  • key (#to_sym)

    the key to delete

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 54

def delete(key) super(key.to_sym) end

#has_key?(key)

Alias for #key?.

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 60

alias has_key? key?

#key?(key) ⇒ Boolean Also known as: #has_key?

Tests if a symbolized key exists

Parameters:

  • key (#to_sym)

    the key to test

Returns:

  • (Boolean)

    whether the key exists

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 59

def key?(key) super(key.to_sym) end

#merge(hash) ⇒ SymbolHash

Merges the contents of another hash into a new SymbolHash object

Parameters:

  • hash (Hash)

    the hash of objects to copy

Returns:

  • (SymbolHash)

    a new SymbolHash containing the merged data

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 74

def merge(hash) dup.merge!(hash) end

#merge!(hash)

Alias for #update.

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 68

alias merge! update

#update(hash) ⇒ SymbolHash Also known as: #merge!

Updates the object with the contents of another ::Hash object. This method modifies the original SymbolHash object

Parameters:

  • hash (Hash)

    the hash object to copy the values from

Returns:

  • (SymbolHash)

    self

[ GitHub ]

  
# File 'lib/yard/core_ext/symbol_hash.rb', line 67

def update(hash) hash.each {|k, v| self[k] = v }; self end