Common ruby hashing methods

Source: Internet
Author: User

Common ruby hashing methods
I. Add default values to Hash:

H = {1, 2, 4} #=>{ 1 => 2, 3 => 4}
H. default= 7
H [1]               #=> 2
H [3]               #=> 4
H [4]               #=> 7
H [5]               #=> 7
II. Add a key-value pair to the Hash:
H = {}                   #=> {}
H. store ("a", 1)       # => 1
H ["a"]                   # => 1
H. fetch ("")           # => 1
H ["B"] = 2             #=> 2
H ["B"]                   #=> 2
P H                       #=>{ "A" => 1,"B" => 2}
3. Clear the Hash key-value Pair:
H = {: a => 1,: B => 2}

You can delete all k-v pairs in either of the following ways:
Assign a null value to hash
Use the clear method (this method is faster)

The shift Method Randomly deletes the k-v pair.
H = {: a => 1,: B => 2,: c => 3}
H. shift           # => [: A, 1]
H                   #=>{: B => 2,: c => 3}
A = h. shift     #=> [: C, 3]
A                   #=> [: C, 3]
Delete, delete_if, reject, reject! Method to delete the specified k-v pair:
H = {: a => 1,: B => 2}
H. delete (:)                   # => 1
H                                   #=>{: B => 2}
H = {: a => 1,: B => 2}     #=>{: A => 1,: B => 2}
H. delete_if {| k, v | v! = 3} #=> {}
H                                   #=> {}
H = {: a => 1,: B => 2}     #=>{: A => 1,: B => 2}
H. delete_if {| k, v | v! = 1} #=>{: A => 1}
H = {: a => 1,: B => 2}     #=>{: A => 1,: B => 2}
H. reject {| k, v | v! = 2}         #=>{: B => 2}
H                                   #=>{: A => 1,: B => 2}
The reject method is equivalent to dup. delete_if {}

4. Invert the k-v pairs of Hash.
H = {: a => 1,: B => 1}       #=>{: A => 1,: B => 1}
X = h. invert                     #=>{ 1 =>: B}
The invert method can reverse the Hash key-value, but the uniqueness of the Hash key may lead to data loss in the previous example!

5. Hash iteration:
Each, each_key, each_value, and each_pair (alias of the each method)
No.

Sat. Check key, value in Hash:
  Check whether key exists:
    Has_key? (Include? Alias. Only keys can be judged! ), Key ?, Member?
  Check whether value exists:
    Has_value? Value?

7. Convert a hash to an array:
 H = {: a => 1,: B => 2}
  H. to_a       # => [[: A, 1], [: B, 2] Convert to a two-dimensional array.
  H. keys       # => [: A,: B]
  H. values     #=> [1, 2]
  The following method is useful:
  H = {: a => 1,: B => '2',: c => 5}
  H. values_at (: a,: B)   # => [1, "2"]   Returns the corresponding values Array Based on the specified key.

8. Select key-value pairs based on the conditions:
  H. detect {| k, v | v ="2 "}           # => [: B,"2"]
  Detect and find are alias relationships and are the methods in the Enumerable module. The Hash class mixin module can also be used. Select method. The alias is find_all. Multiple matching k-v pairs can be returned:
 H. select {| k, v | v. is_a? (Integer )} #=> [[: A, 1], [: c, 5]

9. Hash Sorting:
   You can use the sort method directly, but a two-dimensional array is returned.
   It is worth noting that when the Hash key is of the Symbol type, the sort method will fail.

10. Merge two hashes:
  Use the merge method, (merge! And update are alias relationships (thx Beck )).
 H1 = {: a => 1,: B => 2}         #=>{: A => 1,: B => 2}
  H2 = {: B => 3,: d => 3}         #=>{: D => 3,: B => 3}
  H1.mergeh2                       #=>{: A => 1,: d => 3,: B => 3}
  H2.mergeh1                       #=>{: A => 1,: d => 3,: B => 2}
 Note: The value of B changes.
 Of course we can use block to change this ending:
  H1.mergeh2Do| K, old, new |
      Old <New? Old:New
  End
 #=>{: A => 1,: d => 3,: B => 2}
H1.merge h2Do| K, old, new |
    Pold
    PNew
End
#=> 2
#=> 3
(Of course, we can use the revert_merge implemented by active_support in rails to achieve the above effect ).

11. Convert an array to Hash:
 When the number of array elements is an even number:
  Arr = % w [a B cd]       # => ["","B ","C ","D"]
    H = Hash [* arr]             #=>{ "A" => "B ","C" => "d "}
  In this case, the array must be an even number of elements.

12th. When the Hash key changes dynamically:

X = [1, 2]                 #=> [1, 2]
H = {x => 2}           #=>{ [1, 2] => 2}
H [x]                       #=> 2
X [0] = 5                 #=> 5
H [x]                       # => Nil
H. rehash               #=>{ [5, 2] => 2}
H [x]                       #=> 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.