How to add to an existing hash in Ruby

前端 未结 7 1612
悲哀的现实
悲哀的现实 2021-01-30 00:02

In regards to adding an key => value pair to an existing populated hash in Ruby, I\'m in the process of working through Apress\' Beginning Ruby and have just fi

相关标签:
7条回答
  • 2021-01-30 00:19

    If you have a hash, you can add items to it by referencing them by key:

    hash = { }
    hash[:a] = 'a'
    hash[:a]
    # => 'a'
    

    Here, like [ ] creates an empty array, { } will create a empty hash.

    Arrays have zero or more elements in a specific order, where elements may be duplicated. Hashes have zero or more elements organized by key, where keys may not be duplicated but the values stored in those positions can be.

    Hashes in Ruby are very flexible and can have keys of nearly any type you can throw at it. This makes it different from the dictionary structures you find in other languages.

    It's important to keep in mind that the specific nature of a key of a hash often matters:

    hash = { :a => 'a' }
    
    # Fetch with Symbol :a finds the right value
    hash[:a]
    # => 'a'
    
    # Fetch with the String 'a' finds nothing
    hash['a']
    # => nil
    
    # Assignment with the key :b adds a new entry
    hash[:b] = 'Bee'
    
    # This is then available immediately
    hash[:b]
    # => "Bee"
    
    # The hash now contains both keys
    hash
    # => { :a => 'a', :b => 'Bee' }
    

    Ruby on Rails confuses this somewhat by providing HashWithIndifferentAccess where it will convert freely between Symbol and String methods of addressing.

    You can also index on nearly anything, including classes, numbers, or other Hashes.

    hash = { Object => true, Hash => false }
    
    hash[Object]
    # => true
    
    hash[Hash]
    # => false
    
    hash[Array]
    # => nil
    

    Hashes can be converted to Arrays and vice-versa:

    # Like many things, Hash supports .to_a
    { :a => 'a' }.to_a
    # => [[:a, "a"]]
    
    # Hash also has a handy Hash[] method to create new hashes from arrays
    Hash[[[:a, "a"]]]
    # => {:a=>"a"} 
    

    When it comes to "inserting" things into a Hash you may do it one at a time, or use the merge method to combine hashes:

    { :a => 'a' }.merge(:b => 'b')
    # {:a=>'a',:b=>'b'}
    

    Note that this does not alter the original hash, but instead returns a new one. If you want to combine one hash into another, you can use the merge! method:

    hash = { :a => 'a' }
    
    # Returns the result of hash combined with a new hash, but does not alter
    # the original hash.
    hash.merge(:b => 'b')
    # => {:a=>'a',:b=>'b'}
    
    # Nothing has been altered in the original
    hash
    # => {:a=>'a'}
    
    # Combine the two hashes and store the result in the original
    hash.merge!(:b => 'b')
    # => {:a=>'a',:b=>'b'}
    
    # Hash has now been altered
    hash
    # => {:a=>'a',:b=>'b'}
    

    Like many methods on String and Array, the ! indicates that it is an in-place operation.

    0 讨论(0)
  • 2021-01-30 00:20
    my_hash = {:a => 5}
    my_hash[:key] = "value"
    
    0 讨论(0)
  • 2021-01-30 00:22
    x = {:ca => "Canada", :us => "United States"}
    x[:de] = "Germany"
    p x
    
    0 讨论(0)
  • 2021-01-30 00:24
    hash = { a: 'a', b: 'b' }
     => {:a=>"a", :b=>"b"}
    hash.merge({ c: 'c', d: 'd' })
     => {:a=>"a", :b=>"b", :c=>"c", :d=>"d"} 
    

    Returns the merged value.

    hash
     => {:a=>"a", :b=>"b"} 
    

    But doesn't modify the caller object

    hash = hash.merge({ c: 'c', d: 'd' })
     => {:a=>"a", :b=>"b", :c=>"c", :d=>"d"} 
    hash
     => {:a=>"a", :b=>"b", :c=>"c", :d=>"d"} 
    

    Reassignment does the trick.

    0 讨论(0)
  • 2021-01-30 00:30

    If you want to add more than one:

    hash = {:a => 1, :b => 2}
    hash.merge! :c => 3, :d => 4
    p hash
    
    0 讨论(0)
  • 2021-01-30 00:42
    hash {}
    hash[:a] = 'a'
    hash[:b] = 'b'
    hash = {:a => 'a' , :b = > b}
    

    You might get your key and value from user input, so you can use Ruby .to_sym can convert a string to a symbol, and .to_i will convert a string to an integer.
    For example:

    movies ={}
    movie = gets.chomp
    rating = gets.chomp
    movies[movie.to_sym] = rating.to_int
    # movie will convert to a symbol as a key in our hash, and 
    # rating will be an integer as a value.
    
    0 讨论(0)
提交回复
热议问题