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
3
votes
6 answers

Convert hash to array of hashes

I have hash, all its values are arrays, like this: list = { letter: ['a', 'b', 'c'], number: ['one', 'two', 'three'], fruit: ['apple', 'pear', 'kiwi'], car: ['vw', 'mb', 'bmw'], state: ['la', 'ny',…
mechnicov
  • 12,025
  • 4
  • 33
  • 56
3
votes
1 answer

Ruby double splat parameter is too greedy with Hash argument

In Ruby 2.4.1, I have a method like this: def example(*args, **kwargs) p args p kwargs end I can pass in positional arguments that are not Hash just fine: irb(main):001:0> example("Greetings") ["Greetings"] {} And if I want to use named…
Deltik
  • 1,129
  • 7
  • 32
3
votes
3 answers

Recursively setting hash keys from an array of keys

I want a function that can take an array like [:a, :b, :c] and recursively set hash keys, creating what it needs as it goes. hash = {} hash_setter(hash, [:a, :b, :c], 'value') hash #=> {:a => {:b => {:c => 'value' } } } hash_setter(hash, [:a, :b,…
Ben G
  • 26,091
  • 34
  • 103
  • 170
2
votes
1 answer

Ruby Flattening JSON Objects or Hash in

// Flattening JSON Objects // (want to merge all keys as a string from nested hash and want out put in array list) input1 = {a: {b: {c: {} }, d:[] }, e: "e", f: nil, g: -2} input2 = {a: {b: {c: {h: {j: ''}, m: {n: ''}}}, d: {k: {l: '' } }},e: "e",f:…
Gautam
  • 84
  • 6
2
votes
5 answers

How to sum repetitions of a value and add it in two values of a key in Ruby?

Im trying to to create a hash with one key per each type of extension on a directory. To every key I would like to add two values: number of times that extension is repeated and total size of all the files with that extension. Something similar to…
SEVENELEVEN
  • 187
  • 1
  • 11
2
votes
2 answers

Hash deep transform all values if specific key

My respect to community! I have deep nested hash and I want to transform all values of specific key provided. Something like deep transform values if key == :something Example of hash: {:id=>"11ed35b8e53c442ea210c39d6f24bddf", …
Transfer
  • 41
  • 3
2
votes
3 answers

How to use `eq` matcher with `hash_including` matcher for an array of hashes in rspec

I have an array of hashes and I'm trying to assert that the array has exactly a certain number of hashes in a certain order that have a certain key. So let's say I have an array of fruits. fruits = [ { name: 'apple', count: 3 }, { name:…
Amiratak88
  • 1,204
  • 12
  • 18
2
votes
2 answers

Ruby: How to set a default value for nil keys on a hash created via hash literal notation?

In a free Ruby course I'm working through, I was shown the way to create a default value for a hash via constructor. And it looks like so: no_nil_hash = Hash.new("default value for nil key") puts no_nil_hash["non_existing_key"] Which prints:…
2
votes
2 answers

Ruby how to find each value in hash greater than x?

I am bit new to Ruby and had a problem with learning hashes. I have hash which contains months and days inside this month. And I need to print each one that has 31 days. months = { "MAR" => 31, 'APR' => 30, 'MAY' => 31, 'JUN' => 30 }
Silka
  • 194
  • 2
  • 11
2
votes
3 answers

search hash values into another hash and create a new hash with matching values

In a Ruby project where I have the two hashes below (from two models): "users" and "conflicts". I would like to find the way to iterate them in order to search conflicts'values within users'values and get a hash that would contain, for each…
garatex
  • 21
  • 3
2
votes
2 answers

Ruby Hash is including extra values for a single key

I've tried to build a hash multiple ways, but when I use it in a grouped_options_for_select tag, there are extra values that aren't in my original hash. Here are two ways I've tried to build my hash: UNITS_OF_MEASURE_GROUPED = { "Dry" => …
Kyle Carlson
  • 7,967
  • 5
  • 35
  • 43
2
votes
2 answers

How to sort a hierarchical array of hashes

I'm working with an array like the below: arr = [{ item: "Subject", id: "16", parent_id: "" }, { item: "Math", id: "17", parent_id: "16" }, …
strello
  • 23
  • 4
2
votes
2 answers

How group ruby literal hash by year/moth and sum values

I have the following hash and I would like to group by year/month and sum the values: range = { "2020-11-06 00:00:00 +0000" => 234100.14176395803, "2020-11-07 00:00:00 +0000" => 57731.63072153537, "2020-11-08 00:00:00 +0000" =>…
Jose Perez
  • 35
  • 5
2
votes
3 answers

Plucking out all hash keys that has a specific word

How do you pluck out a hash key that has for example Hash 1 {sample => {apple => 1, guest_email => my_email@example.com }} Hash 2 {guest => {email => my_email@example.com}} Lets say I want 1 method that will pluck out the email from either of those…
Berimbolo
  • 293
  • 2
  • 12
2
votes
3 answers

Add hash in array if hash with this value doesn't exist, otherwise expand existing one

I want to create an array with roles for projects. I have an array of hashes, like that: projects_with_roles = [ { id: 1, name: 'First', roles: ['user', 'compliance_lead'] }, { id: 5, name: 'Five', roles: ['financial_lead'] } ] I want to add…
nrave
  • 41
  • 5
1
2
3
12 13