Questions tagged [ruby-hash]

The Hash class is a Ruby's variant of a dictionary or associative array. Contrary to arrays, the key type of a Hash can be of any type. For questions regarding (cryptographic) hashing, use the [hash] tag.

For more information specific to ruby hash, see the documentation here.

188 questions
673
votes
18 answers

How to remove a key from Hash and get the remaining hash in Ruby/Rails?

To add a new pair to Hash I do: {:a => 1, :b => 2}.merge!({:c => 3}) #=> {:a => 1, :b => 2, :c => 3} Is there a similar way to delete a key from Hash ? This works: {:a => 1, :b => 2}.reject! { |k| k == :a } #=> {:b => 2} but I would expect to…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
20
votes
4 answers

Is there a solution to bypass 'can't add a new key into hash during iteration (RuntimeError)'?

I have a big problem with the expected RuntimeError: "can't add a new key into hash during iteration" In my case a I have a YAML file: test.yaml - in which I have some keys already added. test.yaml key1: key2: key3: I am getting the contents of the…
Cristian M
  • 357
  • 1
  • 3
  • 18
14
votes
5 answers

How to merge two arrays of hashes

I have two arrays of hashes: a = [ { key: 1, value: "foo" }, { key: 2, value: "baz" } ] b = [ { key: 1, value: "bar" }, { key: 1000, value: "something" } ] I want to merge them into one array of…
Rhs
  • 3,188
  • 12
  • 46
  • 84
11
votes
4 answers

Hash destructuring in Ruby 3

I Ruby 2 you could do the following: my_hash = {a: {aa: 1, ab: 2, ac: 3}} my_hash.each do |key, aa:, ab: 4, **| puts key puts aa puts ab end In Ruby 3 this now results in missing keywords :aa, :ab. What would be the best way to refactor code…
Tolsto
  • 1,418
  • 2
  • 17
  • 45
7
votes
5 answers

Generate a hash of all letters and digits

Using ruby, how do I make a hash of each letter in the alphabet (keys) and 1-26 (values) ? I need to create a hash with "a" to "z" in keys and 1 to 26 in values but I do not want to write myself alphabet = {'a'=>1,'b'=>2,....'y'=>25,'z'=>26} I need…
Aliénor
  • 71
  • 4
7
votes
3 answers

Using a lambda as a default in Hash#fetch ruby

I was reading through confident ruby and I was trying out how to define a reusable proc. From the examples given, I wrote this: DEFAULT_BLOCK = -> { 'block executed' } answers = {} answers.fetch(:x, &DEFAULT_BLOCK) I was expecting it to return…
L.T
  • 370
  • 3
  • 8
5
votes
2 answers

Could someone explain me what is the difference between Hash#dig vs Hash#fetch

I'm trying to obtain a nested value in a hash. I've tried using Hash#fetch and Hash#dig but I don't understand how they should be combined. My hash is as follows. response = { "results":[ { "type":"product_group", …
Florin Lei
  • 521
  • 8
  • 16
5
votes
3 answers

Better way to initialize and update deeply nested Hash

I have a Hash and I want to insert some data into it at a deep level, but a key might be missing at any level. So, I am conditionally initializing it before updating its value at every level. What would be a better way to write this or an approach…
Jagdeep Singh
  • 4,880
  • 2
  • 17
  • 22
5
votes
4 answers

Alternatives to convert a array to a hash using same keys and values

I want to convert: [:one, :two, :three] to: {one: :one, two: :two, three: :three} So far I'm using this: Hash[[:basic, :silver, :gold, :platinum].map { |e| [e, e] }] But I would like to know if it's possible by some other way? This is to use in a…
Arnold Roa
  • 7,335
  • 5
  • 50
  • 69
4
votes
4 answers

(Not #dig!) How to determine a key *exists* in a deeply nested Ruby Hash?

Is there an "easy" way, short of hand-writing the kind of nested Hash/Array traversal performed by Hash#dig, that I can determine if a key is present in a deeply nested Hash? Another way to ask this is to say "determine if any value is…
Andrew Hodgkinson
  • 4,379
  • 3
  • 33
  • 43
4
votes
1 answer

slice vs extract! rails 5

I was making some reviews today to an old code and this arises, might be a silly question if so apologize in advance, but is there any real difference between slice and extract! functions (for hashes), I looked in the documentation and there is no…
r4cc00n
  • 1,927
  • 1
  • 8
  • 24
4
votes
3 answers

An "append-only" / "write-only" hash in Ruby

I'm looking for a kind of "append-only" hash where keys may only be set once. For example: capitals = AppendOnlyHash.new capitals['france'] = 'paris' capitals['japan'] = 'tokyo' capitals['france'] = 'nice' # raises immutable exception Any library…
mahemoff
  • 44,526
  • 36
  • 160
  • 222
4
votes
4 answers

Adding Hashes from an array

I am trying to create an array/hash from an array of multiple hashes with same keys and an average of values. My array: [{:amount=>897500, :gross_amount=>897500, :tax=>147500, :hotel_fees=>0, :base_fare=>750000, :currency=>"INR"}, {:amount=>1006500,…
Ravi
  • 304
  • 2
  • 15
3
votes
4 answers

Working with Hashes that have a default value

Am learning to code with ruby. I am learning about hashes and i dont understand this code: count = Hash.new(0). It says that the 0 is a default value, but when i run it on irb it gives me an empty hash {}. If 0 is a default value why can't i see…
3
votes
3 answers

Efficient way of matching 2 hashes having maximum similarity

[ {"id"=>11486, "opt_ids"=> [3545, 3546, 3548, 3550] }, {"id"=>12624, "opt_ids"=> [3545, 3396, 3548, 3550] }, {"id"=>14588, "opt_ids"=> [3394, 3396, 3397, 3399] }, {"id"=>14589, "opt_ids"=> [3394, 3545, 3398, 3548] }, {"id"=>14590,…
Gautam
  • 1,754
  • 1
  • 14
  • 22
1
2 3
12 13