Class: SymbolHash
Overview
A subclass of Hash where all keys are converted into Symbols, and optionally, all String values are converted into Symbols.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Accessed a symbolized key.
-
#[]=(key, value) ⇒ Object
Assigns a value to a symbolized key.
-
#delete(key) ⇒ void
Deleted a key and value associated with it.
-
#has_key?(key) ⇒ Boolean
Tests if a symbolized key exists.
-
#initialize(symbolize_value = true) ⇒ SymbolHash
constructor
Creates a new SymbolHash object.
-
#merge(hash) ⇒ SymbolHash
Merges the contents of another hash into a new SymbolHash object.
-
#update(hash) ⇒ SymbolHash
(also: #merge!)
Updates the object with the contents of another Hash object This method modifies the original SymbolHash object.
Methods inherited from Hash
Constructor Details
#initialize(symbolize_value = true) ⇒ SymbolHash
Creates a new SymbolHash object
8 9 10 |
# File 'lib/yard/core_ext/symbol_hash.rb', line 8 def initialize(symbolize_value = true) @symbolize_value = symbolize_value end |
Class Method Details
.[](hash) ⇒ SymbolHash .[](*list) ⇒ SymbolHash
27 28 29 30 31 32 33 34 35 |
# File 'lib/yard/core_ext/symbol_hash.rb', line 27 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
48 |
# File 'lib/yard/core_ext/symbol_hash.rb', line 48 def [](key) super(key.to_sym) end |
#[]=(key, value) ⇒ Object
Assigns a value to a symbolized key
41 42 43 |
# File 'lib/yard/core_ext/symbol_hash.rb', line 41 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
53 |
# File 'lib/yard/core_ext/symbol_hash.rb', line 53 def delete(key) super(key.to_sym) end |
#has_key?(key) ⇒ Boolean
Tests if a symbolized key exists
58 |
# File 'lib/yard/core_ext/symbol_hash.rb', line 58 def has_key?(key) super(key.to_sym) end |
#merge(hash) ⇒ SymbolHash
Merges the contents of another hash into a new SymbolHash object
72 |
# File 'lib/yard/core_ext/symbol_hash.rb', line 72 def merge(hash) dup.merge!(hash) end |
#update(hash) ⇒ SymbolHash Also known as: merge!
Updates the object with the contents of another Hash object This method modifies the original SymbolHash object
65 |
# File 'lib/yard/core_ext/symbol_hash.rb', line 65 def update(hash) hash.each {|k,v| self[k] = v }; self end |