Class: Hash
- Defined in:
- lib/bioinform/support/delete_many.rb,
lib/bioinform/support/partial_sums.rb,
lib/bioinform/support/third_part/active_support/core_ext/hash/keys.rb,
lib/bioinform/support/third_part/active_support/core_ext/array/extract_options.rb,
lib/bioinform/support/third_part/active_support/core_ext/hash/indifferent_access.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#assert_valid_keys(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
- #delete_many(*keys) ⇒ Object
-
#extractable_options? ⇒ Boolean
By default, only instances of Hash itself are extractable.
-
#partial_sums(initial = 0.0) ⇒ Object
=> 5, 4 => 3, 3 => 2.partial_sums == 3=>7, 4=>10.
-
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
-
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
-
#symbolize_keys ⇒ Object
(also: #to_options)
Return a new hash with all keys converted to symbols, as long as they respond to
to_sym. -
#symbolize_keys! ⇒ Object
(also: #to_options!)
Destructively convert all keys to symbols, as long as they respond to
to_sym. -
#with_indifferent_access ⇒ Object
(also: #nested_under_indifferent_access)
Returns an
ActiveSupport::HashWithIndifferentAccessout of its receiver:.
Instance Method Details
#assert_valid_keys(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch. Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols as keys, this will fail.
Examples
{ :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
{ :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key: name"
{ :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
48 49 50 51 52 53 |
# File 'lib/bioinform/support/third_part/active_support/core_ext/hash/keys.rb', line 48 def assert_valid_keys(*valid_keys) valid_keys.flatten! each_key do |k| raise(ArgumentError, "Unknown key: #{k}") unless valid_keys.include?(k) end end |
#delete_many(*keys) ⇒ Object
11 12 13 |
# File 'lib/bioinform/support/delete_many.rb', line 11 def delete_many(*keys) keys.each{|el| delete el} end |
#extractable_options? ⇒ Boolean
By default, only instances of Hash itself are extractable. Subclasses of Hash may implement this method and return true to declare themselves as extractable. If a Hash is extractable, Array#extract_options! pops it from the Array when it is the last element of the Array.
7 8 9 |
# File 'lib/bioinform/support/third_part/active_support/core_ext/array/extract_options.rb', line 7 def instance_of?(Hash) end |
#partial_sums(initial = 0.0) ⇒ Object
=> 5, 4 => 3, 3 => 2.partial_sums == 3=>7, 4=>10
12 13 14 15 |
# File 'lib/bioinform/support/partial_sums.rb', line 12 def partial_sums(initial = 0.0) sums = initial sort.collect_hash{|k,v| [k, sums += v]} end |
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
{ :name => 'Rob', :years => '28' }.stringify_keys
#=> { "name" => "Rob", "years" => "28" }
6 7 8 |
# File 'lib/bioinform/support/third_part/active_support/core_ext/hash/keys.rb', line 6 def stringify_keys dup.stringify_keys! end |
#stringify_keys! ⇒ Object
Destructively convert all keys to strings. Same as stringify_keys, but modifies self.
12 13 14 15 16 17 |
# File 'lib/bioinform/support/third_part/active_support/core_ext/hash/keys.rb', line 12 def stringify_keys! keys.each do |key| self[key.to_s] = delete(key) end self end |
#symbolize_keys ⇒ Object Also known as: to_options
Return a new hash with all keys converted to symbols, as long as they respond to to_sym.
{ 'name' => 'Rob', 'years' => '28' }.symbolize_keys
#=> { :name => "Rob", :years => "28" }
24 25 26 |
# File 'lib/bioinform/support/third_part/active_support/core_ext/hash/keys.rb', line 24 def symbolize_keys dup.symbolize_keys! end |
#symbolize_keys! ⇒ Object Also known as: to_options!
Destructively convert all keys to symbols, as long as they respond to to_sym. Same as symbolize_keys, but modifies self.
30 31 32 33 34 35 |
# File 'lib/bioinform/support/third_part/active_support/core_ext/hash/keys.rb', line 30 def symbolize_keys! keys.each do |key| self[(key.to_sym rescue key) || key] = delete(key) end self end |
#with_indifferent_access ⇒ Object Also known as: nested_under_indifferent_access
Returns an ActiveSupport::HashWithIndifferentAccess out of its receiver:
{:a => 1}.with_indifferent_access["a"] # => 1
8 9 10 |
# File 'lib/bioinform/support/third_part/active_support/core_ext/hash/indifferent_access.rb', line 8 def with_indifferent_access ActiveSupport::HashWithIndifferentAccess.(self) end |