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
0
votes
2 answers

Create a hash from an array using group_by

I have the following array ages = [["a", 15],["b", 16], ["c", 15], ["d", 16], ["e", 17], ["f", 20]] I have to create a hash with ages as values such that it looks like this {15 => ["a","c"], 16=> ["b","d]....} when I run the group_by method: puts…
user13350731
  • 37
  • 1
  • 2
0
votes
1 answer

Ruby set hash inside the Hash for the Array of Hashes

I am working on Rails 6 API. This is what I get "data": [ { "invoice_details": { "customer_name": "Dylan Sollfrank", "invoice_number": "1060", "invoice_status": "paid" } }, { …
urjit on rails
  • 1,763
  • 4
  • 19
  • 36
0
votes
3 answers

Calculate rank of hash value and insert it into a CSV file in Ruby

I have one hash h1 = {"Cust1"=>500, "Cust4"=>400, "Cust2"=>100, "Cust3"=>100} I want to insert this hash into a CSV file with the ranking of keys according to their value. The sample output should look like this ID,Sales,Rank Cust1,500,1 Cust4,400,2…
Sayandip Ghatak
  • 191
  • 2
  • 14
0
votes
1 answer

Why key of hash is not parsed

I'm working with hash like this, the first key is a hash hash = { { a: 'a', b: 'b' } => { c: { d: 'd', e: 'e' } } } when I convert it to json, I get this: data_json = hash.to_json # => "{\"{:a=\\u003e\\\"a\\\",…
Taoufik
  • 196
  • 1
  • 1
  • 12
0
votes
2 answers

Group array of hashes by value and retain structure (hash) in Ruby

I have a hash like this: hash = { 'en-us': { where: 'USA' }, 'en-us-zone2': { where: 'USA' }, 'en-nl': { where: 'Netherlands' }, 'en-pt': { where: 'Portugal' }, } And I tried to group them…
Dennis
  • 1,805
  • 3
  • 22
  • 41
0
votes
3 answers

How to change hash return format for Ruby

I have the following hash: {"description":"Hello","id":"H"}` If I type the hash in the console, I get: {:description=>"Hello", :id=>"H"} I would like it to return the same form mentioned first, using "description" and "id". Is there a way to…
Za_Voucha
  • 13
  • 3
0
votes
1 answer

How to set the appended data as the key in a hash

I'm trying to get the date to be my hash's key and then have the total of the balance be the value in my hash array to use the hash later when returning an account statement which will print date, amount and balance. Here is the code: class Bank …
JBDR
  • 55
  • 4
0
votes
4 answers

Ruby exclude specific data from array of hashes

I've got response which hash and array of hashes: "id"=>67547, "description"=>"project", "actors"=> [ {"id"=>123, "displayName"=>"John Doe", "type"=>"atlassian-user-role-actor", "name"=>"john.doe", …
mr_muscle
  • 2,536
  • 18
  • 61
0
votes
3 answers

Ruby group hashes based on matching keys and store the value of non matching keys in an array

Need to achieve the given output based on the following hash value foos = [ { :key => 'Foo', :value => 1, :revenue => 2 }, { :key => 'Foo', :value => 1, :revenue => 4 }, { :key => 'Bar', :value => 2, :revenue => 7 }, {…
0
votes
1 answer

Creating an RPG point buy system. Why is my if condition not being met?

I'm creating a point buy method for an RPG in which players modify their character's ability scores. My if condition is not being met when the input should be correct, and I can't tell why. $player = { abils: {str: 10, con: 10, dex: 10, wis: 10,…
Richard
  • 164
  • 7
0
votes
2 answers

How to get the key name inside the value for printing inside a Ruby hash

I am creating a hash containing lambdas as the value in Ruby. I want to access the key name in the lambda. The hash is a collection of anonymous functions (lambdas) which take inputs and do a specific task. So, I am making a hash which draws a shape…
0
votes
2 answers

Sort Array of Hashes Keys

I am looking for a solution to sort the Hash keys in an Array. arr = [{"name"=>"Product Management", "id"=>647628}, {"name"=>"Sales", "id"=>647630}] arr.each {|inner_hash| inner_hash.sort} Expected Result: [{"id"=>647628, "name"=>"Product…
0
votes
3 answers

Delete duplicated elements in an array that's a value in a hash and its corresponding ids

I have a hash with values that's an array. How do I delete repeated elements in the array and the corresponding ids in the most performant way? Here's an example of my hash hash = { "id" => "sjfdkjfd", "name" => "Field Name", "type" =>…
C. Yee
  • 127
  • 2
  • 15
0
votes
2 answers

rails: attempting to match two values in an array, getting Unsupported argument type: Hash error

I have two fields in two different models which store location values in an array and what I am trying to achieve is a controller instance variable that can match any identical values in both arrays and then show that in the index view. However,…
0
votes
2 answers

Ruby - Set key-value pairs inside array of hashes

The problem is: I have a method def comparison_reporter(list_of_scenarios_results1, list_of_scenarios_results2) actual_failed_tests = list_of_scenarios_results2.select {|k,v| v == 'Failed'} actual_passed_tests = list_of_scenarios_results2.select…
Mikhah
  • 139
  • 12