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

Next key/value pair overwrites the existing pair in a hash while trying to add pair with new key

I have: fruits = { "orange" => {:season => "winter"}, "apple" => {:season => "winter"}, "banana" => {:season => "summer"}, "grape" => {:season => "spring"}, "peach" => {:season => "winter"}, "pineapple" => {:season => "summer"} } I want…
Jaysan
  • 43
  • 2
  • 4
0
votes
0 answers

how to merge hash of hash with same keys in ruby

I have a two 2nd level hash , and i have to merge both hashes hash1 = {"brands"=>{"likes"=>["gucci", "ck", "armani", "heyo"], "dislikes"=>["lv", "tods", "pols"]}} hash2 = {"brands"=>{"likes"=>["bampam","plot"], "dislikes"=>["pom", "ui"]}} output…
summu
  • 388
  • 2
  • 7
  • 25
0
votes
2 answers

Print out elements in a hash table, Ruby

I have a hash table that looks like this: hash…
user2909180
  • 203
  • 3
  • 11
0
votes
1 answer

Using multiple classes together as a hash key

I found a script that calculates 3D models and combines identical vertices. It has the following logic, where according to my understanding, vertices are hash maps of vertex class: unless vertices.key?(vertex) new_vertices << vertex vertices[…
user923
  • 559
  • 4
  • 12
0
votes
2 answers

How to count number of appearance of a specific value in Ruby hash?

I have a Ruby hash (originally was a param in rails) How can I count the number of correctness in each answers_attributes ? (Why I was doing this is I am trying to create an multiple-choice quiz by rails. One question may have many answers. I was…
manh.vu
  • 335
  • 1
  • 4
  • 14
0
votes
2 answers

Ruby if statement optimization refactor best practice

I am having a very common refactor situation here with me, and after going through a few blogs I still didn't get any satisfactory comment on the same; so asking a question here. h = { a: 'a', b: 'b' } new_hash = {} new_hash[:a] = h[:a].upcase…
Swaps
  • 1,450
  • 24
  • 31
0
votes
1 answer

Hash created from CSV row not behaving like a normal hash

I'm getting some weird issues. I'm trying to allow importing of a CSV into my model. I'm getting an unknown attribute 'hashtag' for Job. error, but that's not the issue. My model definitely has a hashtag column. When I get the error, if I try doing…
Joe
  • 273
  • 5
  • 17
0
votes
3 answers

Hash unique by column and merge other column

I have below hash. How to get unique and merged hash. [ { "email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb", "roles"=>[:admin] }, { "email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb", …
mbt
  • 29
  • 9
0
votes
1 answer

Data Array transferred to hash values and exporting the keys to an output file

I have this following test data that needs to export to the format shown in my desired output with ruby programming. The real data array has 1000000 records. data_array1=aaaa data_array2=bbbb ---------------- ---------------- data_array8=hhhh ,…
Rubz
  • 95
  • 8
0
votes
1 answer

Call the same method but with different arguments, better way

I have something like this in my function with calls the print_hash function three times but with different args. How to do it nicer? The print_hash function is just only about print key and…
Daniel
  • 153
  • 2
  • 12
0
votes
2 answers

Loop through hash and find records to delete in Ruby

I am self-learning ruby by watching few videos and reading through blogs. I am currently in a situation where I have to parse the below parameters that I receive from an external CRM system into RUBY and find the relevant records from the database…
nurav
  • 13
  • 1
  • 4
0
votes
2 answers

Add a value to an existing Key value pair ruby hash

My ruby script filters a log and generates a hash like this scores = {"Rahul" => "273", "John"=> "202", "coventry" => "194"} by skipping multiple values for a key which is obvious log file will be like this Rahul has 273 Rahul has 217 John has…
Hameed Basha
  • 13
  • 1
  • 7
0
votes
2 answers

Delete a hash from array of hashes in rails

I have array of hashes in json format, and I have to remove one of the hash from that array, I am iterating that array and if that particular key/ value matches i am deleting that hash, I found clear() method but, clear leaves the {}, which I don't…
summu
  • 388
  • 2
  • 7
  • 25
0
votes
3 answers

Hash message Rails

I have issues linking my Rails app to Paybox (through a French bank). The technical support says that the problem could come from the hash message i'm sending to them (through a form). But they have no knowledge about Ruby so they can't guarantee…
justinedps26
  • 159
  • 1
  • 3
  • 15
0
votes
3 answers

Ruby hash and user input

Hello i am just getting into ruby and i need help with this task. The task is to enter the 7 days of the week in a hash like this {"Monday" => 1 , "Tuesday" =>2 ..etc} And when a user inputs a number between 1-7 it shows the corresponding day. So if…
daxtera
  • 33
  • 6