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

Multiply the values of a hash by a number, and return 0 for the negative values

How do I multiply the non-negative number values of a hash by a number (for example: 2) and for the negative values just return 0? For example, with this hash (with variable years keys): hash = {"year2020" => "-2.0", "year2021" => "3.0", "year2022"…
Chris
  • 55
  • 4
2
votes
1 answer

Why duplicate key for hash in ruby language not return Error?

A. Duplicate key with String as a key irb(main):001:0> myHash = {'a' => 'a', 'a' => 'a'} (irb):1: warning: key "a" is duplicated and overwritten on line 1 B. Duplicate key with Array as a key irb(main):001:0> myHash = {[1,2,3] => [1,2,3], [1,2,3]…
Ullman
  • 19
  • 2
2
votes
3 answers

Rails/Ruby: turning query result into multi-level hash

I have a query* that results in the following: #, #, …
gibihmruby
  • 41
  • 7
2
votes
2 answers

Clone constant of hash into new variable without mutating constant on update with .each block?

I'm struggling with something. I've abstracted my code out to be as simple as possible, yet I still don't understand why it's having this behaviour. I'm creating a constant consisting of a set of key-value pairs and freezing it. I'm then using the…
Joe
  • 273
  • 5
  • 17
2
votes
4 answers

Turn nested hash to two dimensional array in Ruby

I want to write a method that can receive a nested hash and return a nested array of two dimensional arrays. hash_to_a({1=>2, 2=>3, {3=>4, 5=>6}=>7}) # [[1, 2], [2, 3], [[[3, 4], [5, 6]], 7]] hash_to_a({{5=>{1=>3, 2=>4}}=>{7=>8}}) # [[[[5, [[1,…
Huy Tran
  • 1,770
  • 3
  • 21
  • 41
2
votes
1 answer

Using params in rails gives ArgumentError: When assigning attributes, you must pass a hash as an argument

I am using params which is a hash and still, I get this error stating ArgumentError (When assigning attributes, you must pass a hash as an argument.) Here are parts of my code: Project/app/controllers/index_controller.rb: class IndexController <…
vinay_Kumar
  • 117
  • 2
  • 10
2
votes
2 answers

Create nested hashes in ruby and save it into JSON

Hi I'm new in ruby and I'm trying to save a nested hash into a JSON file, the final hash looks like this: {"**School**":{"*Students*":{ "Info":{},"Values":{} },"*Teachers*":{ "Info":{},"Values":{} } } } But initially the hash must start empty…
Jay
  • 23
  • 4
2
votes
4 answers

Sorting a complex ruby hash works fine, but it's impossible to reverse, what can I do?

I have a hash in ruby with a complex structure, like this: something = {} something[1488343493] = { :type => 'tag', :name => 'v1.2', :sha => 'a66fd116e454378794d24c41c193d385be37436f'} something[1488288253] = { :type => 'pull', :number => '469',…
q9f
  • 11,293
  • 8
  • 57
  • 96
1
vote
2 answers

Matching of randomly ordered array when test nested hash with RSpec

In my RSpec tests I often have the challenge to compare deep nested hashes like this { foo: ["test", { bar: [1,2,3] }] } The values [1,2,3] are read from a DB where the order is not guaranteed, nor do I care about the order. So when I compare the…
23tux
  • 14,104
  • 15
  • 88
  • 187
1
vote
3 answers

How to convert array to hash in Ruby using indices?

I want to convert the array of this: ["Cyan", "Magenta", "Yellow", "Black"] to hash like this: {1 => "Cyan", 2 => "Magenta", 3 => "Yellow", 4 => "Black"} How could I make it in Ruby language? I've tried using this code color = ["Cyan", "Magenta",…
1
vote
4 answers

How to produce a concatenated string from a hash recursively?

I have the following complicated hash structure (among many) that looks like the following: hash = {"en-us"=> {:learn_more=>"Learn more", :non_apple_desktop=>"To redeem, open this link.", :value_prop_safety=>"", :storage_size=> …
Dan Rubio
  • 4,709
  • 10
  • 49
  • 106
1
vote
1 answer

Ruby - Strings & Symbols Questions

Context: just started learning Ruby, the code below is a part of a slightly longer program, but I only need some help with the first part, and I may not articulate my problem clearly enough, so apologies in advance Question: I've marked the problem…
Stepdeff
  • 21
  • 1
1
vote
2 answers

Split text in hash value into new hash using separator in Ruby

I found another question similar to the one I have, but I was not able to replicate it (probably because it doesn't deal with hashes neither have symbols). So, given the following array of hashes: [{:id=>1, :name=>"AA;AB;AC", :title=>"A row"}, …
rdornas
  • 630
  • 7
  • 15
1
vote
2 answers

How can I group hash contents based on key starts_with?

I have a hash that currently looks like this: { "prefix1_key1": [a, b], "prefix1_key2": [c, d], "prefix2_key1": [e, f], "prefix3_key1": [g, h] } And I would want to transfer this into: { "prefix1": [a, b, c, d], "prefix2": [e, f], …
Victor Motogna
  • 731
  • 1
  • 12
  • 22
1
vote
1 answer

How to send form-data with RestClient, if data received from a hash?

I have an API endpoint that requires data to be sent to it as form-data. I am using RestClient for sending the data. The problem is that I do not manage to give the data for the request in a way, that the endpoint would be able to read it. This is…
Leero11
  • 365
  • 2
  • 4
  • 17
1 2
3
12 13