docs/HASH.md in lite-ruby-1.0.1 vs docs/HASH.md in lite-ruby-1.0.2

- old
+ new

@@ -1,7 +1,26 @@ # Hash +`zip` +------ +Creates a new hash from two separate arrays. + +```ruby +Hash.zip(%i[a b c], [1, 2, 3]) #=> { a: 1, b: 2, c: 3 } +``` + +`alias` +------ +Adds a key/value pair from an existing key/value pair. + +```ruby +h1 = { foo: 'bar', baz: 'boo' } +h1.alias('boo', :foo) + +h1['boo'] #=> 'bar' +``` + `assert_valid_keys!` ------ Raises an error if key is not included in a list of keys. ```ruby @@ -44,10 +63,18 @@ { foo: { baz: 'boo' } }.bury(:foo, :moo) #=> { foo: :moo } { foo: { baz: 'boo' } }.bury(:foo, :baz, :moo) #=> { foo: { baz: :moo } } { foo: { baz: 'boo' } }.bury(:foo) #=> raises ArgumentError: '2 or more arguments required' ``` +`collate(!)` +------ +Merge the values of this hash with those from another, setting all values to be arrays representing the values from both hashes. + +```ruby +{ a: 1, b: 2 }.collate(a: 3, b: 4, c: 5) #=> { a: [1, 3], b: [2, 4], c: [5] } +``` + `collect_keys` ------ Returns an array with all keys converted using the block operation. ```ruby @@ -62,10 +89,31 @@ ```ruby { foo: 'bar', baz: :boo }.collect_values #=> ['bar', :boo] { foo: 'bar', baz: :boo }.collect_values { |k| k.to_s.upcase } #=> ['BAR', BOO'] ``` +`dearray_values(!)` +------ +Any array values will be replaced with the first element of the array and arrays with no elements will be set to `nil`. + +```ruby +h1 = { a: [1], b: [1, 2], c: 3, d: [] } + +h1.dearray_values #=> { a: 1, b: 1, c: 3, d: nil } +h1.dearray_values!(1) #=> { a: 1, b: 2, c: 3, d: nil } +``` + +`dearray_singular_values(!)` +------ +Any array values with one or no elements will be set to the element or `nil`. + +```ruby +h1 = { a: [1], b: [1, 2], c: 3, d: [] } + +h1.dearray_singular_values #=> { a: 1, b: [1, 2], c: 3, d: nil } +``` + `deep_merge(!)` ------ Returns a new hash with self and other_hash merged recursively. ```ruby @@ -73,10 +121,32 @@ h2 = { a: false, b: { x: [3, 4, 5] } } h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } } ``` +`delete_unless` +------ +Inverse of `delete_if`. + +```ruby +h1 = { a: 1, b: 2, c: 3 } +h1.delete_unless { |_, v| v == 1 } + +h1 #=> { a: 1 } +``` + +`delete_values` +------ +Delete key/value pairs by value. + +```ruby +h1 = { a: 1, b: 2 } +h1.delete_values(1) + +h1 #=> { b: 2 } +``` + `demote(!)` ------ Moves a key value pair to the tail of the hash. ```ruby @@ -91,10 +161,22 @@ ```ruby { abc: nil, xyz: 1 }.denillify #=> { abc: 0, xyz: 1 } { abc: nil, xyz: 1 }.denillify!(9) #=> { abc: 9, xyz: 1 } ``` +`diff` +------ +Difference comparison of two hashes. + +```ruby +h1 = { a: 1, b: 2 } +h2 = { a: 1, b: 3 } + +h1.diff(h2) #=> { b: 2 } +h2.diff(h1) #=> { b: 3 } +``` + `except(!)` ------ Returns a hash that includes everything but the given keys. ```ruby @@ -119,10 +201,42 @@ ```ruby { a: 1, b: 2, c: 3 }.hmap { |k, v| { k => v + 3 } } #=> { a: 4, b: 5, c: 6 } ``` +`insert` +------ +As with `store` but only if the key isn't already in the hash. + +```ruby +h1 = { a: 1, b: 2 } + +h1.insert(:c, 3) #=> true +h1.insert(:b, 3) #=> false +``` + +`invert` +------ +Create an inverse hash by storing multiple values in arrays. + +```ruby +h1 = { a: 3, b: 3, c: 3, d: 2, e: 9, f: 3, g: 9 } + +h1.invert #=> { 2 => :d, 3 => %i[f c b a], 9 => %i[g e] } +``` + +`keys?` aka `has_keys?` +------ +Returns if hash contains the given keys. + +```ruby +h1 = { a: 0, b: 1 } + +h1.keys?(:a, :b) #=> true +h1.keys?(:z) #=> false +``` + `nillify(!)` ------ Transforms all blank values to `nil`. ```ruby @@ -136,10 +250,21 @@ ```ruby {}.only_fill(:foo) #=> { foo: nil } { :foo => 1, baz: 2 }.only_fill(:foo, :bar, placeholder: 0) #=> { foo: 1, bar: 0 } ``` +`only_keys?` aka `has_only_keys?` +------ +Returns if hash contains only the given keys. + +```ruby +h1 = { a: 0, b: 1 } + +h1.only_keys?(:a, :b) #=> true +h1.only_keys?(:a) #=> false +``` + `pair?` ------ Returns if the hash has a key with a matching value. ```ruby @@ -267,9 +392,33 @@ ------ Converts an object to have an object like API. ```ruby { foo: { bar: true } }.to_object.foo.bar #=> true +``` + +`update_each` +------ +Iterate over hash updating the key/value pair. + +```ruby +{ a: 1, b: 2 }.update_each { |k, v| { "#{k}!" => v + 1 } } #=> { 'a!' => 2, 'b!' => 3 } +``` + +`update_keys` +------ +Iterate over hash updating just the keys. + +```ruby +{ a: 1, b: 2 }.update_keys { |k| "#{k}!" } #=> { 'a!' => 1, 'b!' => 2 } +``` + +`update_values` +------ +Iterate over hash updating just the values. + +```ruby +{ a: 1, b: 2 }.update_values { |v| v + 1 } #=> { a: 2, b: 3 } ``` `vacant?` ------ Returns if the value of a key is blank?.