Class: Hash
- Defined in:
- lib/reactive_support/core_ext/hash/keys.rb,
lib/reactive_support/core_ext/object/blank.rb,
lib/reactive_support/core_ext/object/deep_dup.rb,
lib/reactive_support/core_ext/array/extract_options.rb
Overview
Instance Method Summary collapse
-
#assert_valid_keys(*valid_keys) ⇒ Object
The #assert_valid_keys method validates that all keys in a hash match *valid_keys+, raising an ArgumentError if the keys don’t match.
-
#deep_dup ⇒ Object
When called on a hash, the
#deep_dupmethod duplicates all the key-value pairs in the hash recursively, so that actions on the duplicate do not affect the original hash:. -
#extractable_options? ⇒ Boolean
By default, only instances of
Hashitself are extractable. -
#present? ⇒ Boolean
When called on a hash (or any enumerable), the
#present?method returnstrueif the hash is not empty, even if its elements have blank values: { foo: :bar }.present? # => true { ‘bar’ => nil }.present? # => true {}.present? # => false. -
#stringify_keys ⇒ Object
The
#stringify_keysmethod returns a hash identical to the calling hash, but with symbol keys turned into strings. -
#stringify_keys! ⇒ Object
The
#stringify_keys!method converts symbol hash keys into strings. -
#symbolize_keys ⇒ Object
The
#symbolize_keysmethod returns a hash identical to the calling hash but with string keys turned into symbols. -
#symbolize_keys! ⇒ Object
The
#symbolize_keys!method converts string hash keys into symbols. -
#transform_keys(&block) ⇒ Object
The
#transform_keysmethod returns a new hash with all keys transformed in accordance with the givenblock. -
#transform_keys!(&block) ⇒ Object
The
#transform_keys!method transforms all of the calling hash’s keys in accordance with the givenblock.
Instance Method Details
#assert_valid_keys(*valid_keys) ⇒ Object
The #assert_valid_keys method validates that all keys in a hash match *valid_keys+, raising an ArgumentError if the keys don’t match. Note that, as usual, symbols and strings are treated as distinct.
Examples:
{:foo => 'bar'}.assert_valid_keys(:foo) # => true
{:foo => 'bar'}.assert_valid_keys(:foo, :baz) # => true
{:foo => 'bar'}.assert_valid_keys(:baz) # => ArgumentError
{:foo => 'bar'}.assert_valid_keys('foo') # => ArgumentError
25 26 27 28 29 30 31 32 |
# File 'lib/reactive_support/core_ext/hash/keys.rb', line 25 def assert_valid_keys(*valid_keys) valid_keys.flatten! each_key do |key| unless valid_keys.include?(key) raise ArgumentError.new("Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}") end end end |
#deep_dup ⇒ Object
When called on a hash, the #deep_dup method duplicates all the key-value pairs in the hash recursively, so that actions on the duplicate do not affect the original hash:
hash = {b: 2} # => {b: 2} dup, deep = hash.dup, hash.deep_dup # => {b: 2}, {b: 2}
deep[:b] = 14 # => {b: 14} p hash # => {b: 2}
dup[:b] = 14 # => {b: 14} p hash # => {b: 14}
56 57 58 59 60 |
# File 'lib/reactive_support/core_ext/object/deep_dup.rb', line 56 def deep_dup self.each_with_object(dup) do |(key, value), hash| hash[key.deep_dup] = value.deep_dup end 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 extractable. Array#extract_options! then pops the hash if it comes as the last argument in a set of splat args.
9 10 11 |
# File 'lib/reactive_support/core_ext/array/extract_options.rb', line 9 def instance_of? Hash end |
#present? ⇒ Boolean
When called on a hash (or any enumerable), the #present? method returns true if the hash is not empty, even if its elements have blank values:
{ foo: :bar }.present? # => true
{ 'bar' => nil }.present? # => true
{}.present? # => false
110 111 112 |
# File 'lib/reactive_support/core_ext/object/blank.rb', line 110 def present? !blank? end |
#stringify_keys ⇒ Object
The #stringify_keys method returns a hash identical to the calling hash, but with symbol keys turned into strings. It is non-destructive; the original hash is still available after it is called.
Although this method was formerly a part of ActiveSupport, it was already deprected by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.
Examples:
orig = { :foo => 'bar' }
dup = orig.stringify_keys
orig #=> { :foo => 'bar' }
dup #=> { 'foo' => 'bar' }
49 50 51 |
# File 'lib/reactive_support/core_ext/hash/keys.rb', line 49 def stringify_keys transform_keys &:to_s end |
#stringify_keys! ⇒ Object
The #stringify_keys! method converts symbol hash keys into strings. It is a destructive method; the original hash is changed when this method is called.
Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.
Examples:
orig = { :foo => 'bar' }
orig.symbolize_keys!
orig #=> { 'foo' => 'bar' }
67 68 69 |
# File 'lib/reactive_support/core_ext/hash/keys.rb', line 67 def stringify_keys! transform_keys! &:to_s end |
#symbolize_keys ⇒ Object
The #symbolize_keys method returns a hash identical to the calling hash but with string keys turned into symbols. It is non-destructive; the original hash is still available after it is called.
Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.
Examples:
orig = { 'foo' => 'bar' }
dup = orig.symbolize_keys
orig #=> { 'foo' => 'bar' }
dup #=> { :foo => 'bar' }
86 87 88 |
# File 'lib/reactive_support/core_ext/hash/keys.rb', line 86 def symbolize_keys transform_keys {|key| key.to_sym } end |
#symbolize_keys! ⇒ Object
The #symbolize_keys! method converts string hash keys into symbols. It is a destructive method; the original hash is changed when this method is called.
Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.
Examples:
orig = { 'foo' => 'bar' }
orig.symbolize_keys!
orig #=> { :foo => 'bar' }
104 105 106 |
# File 'lib/reactive_support/core_ext/hash/keys.rb', line 104 def symbolize_keys! transform_keys! {|key| key.to_sym } end |
#transform_keys(&block) ⇒ Object
The #transform_keys method returns a new hash with all keys transformed in accordance with the given block. The #transform_keys method is non-destructive; the original hash will still be available after it is called.
If no block is given, the method returns an enumerator.
Example:
hash = { :foo => 'bar' }
hash.transform_keys {|key| key.to_s.upcase } # => { 'FOO' => 'bar' }
hash.transform_keys # => #<Enumerator: {:foo=>"bar"}:transform_keys>
hash # => { :foo => 'bar' }
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/reactive_support/core_ext/hash/keys.rb', line 121 def transform_keys(&block) return enum_for(:transform_keys) unless block_given? dup = self.class.new each_key do |key| dup[yield key] = self[key] end dup end |
#transform_keys!(&block) ⇒ Object
The #transform_keys! method transforms all of the calling hash’s keys in accordance with the given block. The #transform_keys! method is destructive; the original hash will be changed in place after it is called.
If no block is given, the method returns an enumerator.
Example:
hash = { :foo => 'bar' }
hash.transform_keys! {|key| key.to_s.upcase } # => { 'FOO' => 'bar' }
hash.transform_keys! # => #<Enumerator: {:foo=>"bar"}:transform_keys!>
hash # => { 'FOO' => 'bar' }
145 146 147 148 149 150 151 152 153 |
# File 'lib/reactive_support/core_ext/hash/keys.rb', line 145 def transform_keys!(&block) return enum_for(:transform_keys) unless block_given? keys.each do |key| self[yield key] = delete(key) end self end |