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

How to remove a character or characters from the keys of a json structure in Ruby recursively

So basically have a structure like this: { "i$tems": { "it$em": [ { "batt$ers": { "ba$tter": [ { "i$d": "1001", …
Rico
  • 58,485
  • 12
  • 111
  • 141
0
votes
1 answer

Rails model results to hash - variable as a key issue

I've tried to put results from my vote model in a hash for further usage, but I don't know how to create a hash key from a variable in Ruby. See example below: def create_hash_array(campaign_votes) target_hash = Hash.new …
KIT
  • 155
  • 1
  • 6
0
votes
1 answer

Iterate over an array of objects

I have got an array of objects in object and trying to reduce to specific form of array. For example: [ #, #>, #
rekha
  • 27
  • 3
0
votes
1 answer

Relating a key of a hash to an array element in a different array in Ruby

I have a hash(city: [coordinates]) like this: cities = { l1: [41.91372380139719,-87.72308349609375], l2: [42.092312110873536,-87.79449462890625], l3: [42.08008203350686,-87.73406982421875], l4: [41.86976539904969,-87.68325805664062], …
Dinukaperera
  • 97
  • 1
  • 10
0
votes
3 answers

How to access an array element inside a hash in Ruby

I have a hash of arrays of coordinates of locations like this: cities = { "l10"=> [41.84828634806966,-87.61184692382812], "l11"=> [41.86772008597142,-87.63931274414062], "l12"=> [41.88510316124205,-87.60498046875], …
Dinukaperera
  • 97
  • 1
  • 10
0
votes
1 answer

How do I build a doubly-nested hash in Ruby?

I'm working on a language translation system and need to build a "map" of how the fields to translate should be translated. To start, I need to build the data structure with a set of defaults. Let's say I have these variables: fields = [:title,…
Dan Sharp
  • 1,209
  • 2
  • 12
  • 31
0
votes
2 answers

Ruby 2.2.5 error on hash mockup

I'm trying to mock up a hash that is dynamically generated with only 1 or 2 known key names at the top-level, so I can test with it in anticipation for converting it to a nested JSON. This is what I'm using: ruby.exe -v ruby 2.2.5p319 (2016-04-26…
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
0
votes
5 answers

How to group Date and time data from API

I am trying to group data I am getting from an API to serve to our front application. I mean group "time" by "date". dates: {date1: [time1, time2, timeN], date2: [time1...]} My input is like this: {"date"=>"2017-04-04T00:00:00",…
Nikos4Life
  • 101
  • 1
  • 9
0
votes
1 answer

no implicit conversion of Symbol into Integer Error with hash

I get the error "no implicit conversion of Symbol into Integer" Here is my code: # == Schema Information # # Table name: my_payments # # id :integer not null, primary key # email :string # ip …
Alien Java
  • 75
  • 2
  • 12
0
votes
4 answers

Sum of particular values in a Ruby hash

I have two hashes: first = { 1 => [15, 15, 15, 8], 4 => [11, 12, 7, 7], 5 => [14, 17, 13, 13], 6 => [19, 19, 15, 15], 7 => [5, 12, 12, 12], 8 => [10, 14, 14, 14], 9 => [8, 7, 8, 8] } second = { 1 => [0, 1, 2], 4 => [2, 3], 5…
Julius Dzidzevičius
  • 10,775
  • 11
  • 36
  • 81
-1
votes
3 answers

Ruby Hash Not Populating

I am working in Chef, trying to create/populate a ruby hash with networking device information, as populated by nmcli. I think the code is correct, as VS Code isn't complaining, and it seems to run just fine in chef-shell -z but I'm not able to…
misteralexander
  • 448
  • 2
  • 7
  • 19
-1
votes
1 answer

Percentage in array of hashes?

I have hash and an array of hashes array: hash = {"BEGIN"=>5, "END"=>10} array = [ {:name=>"aaa", :phase=>"END", :quantity=>6}, {:name=>"bbb", :phase=>"END", :quantity=>4}, {:name=>"ccc", :phase=>"BEGIN", :quantity=>5} ] How can I…
-1
votes
1 answer

TypeError Parsing Ruby Array of Hashes

I have an Array of Ruby Hashes that I'm trying to iterate so that I can parse values by key. forms =…
littleK
  • 19,521
  • 30
  • 128
  • 188
-1
votes
1 answer

Access a specific value in ruby hash with multiple values

Hi how can I access a specific value in ruby hash, for example how can i get "colder" inside jupiter planets= { "jupiter" => ["brown", "big" , "colder"] "mars" => ["red", "small", "cold"] };
JhonDoe
  • 27
  • 6
-1
votes
1 answer

How to create a hash by reading a text file in Ruby?

I want my program to read each word from the text file and then, match them with numbers by row. For example; text file is my name is donald knuth0 and the program should run like : "my" => "1" , "name" => "2" , "donald" => "3" , "knuth" => "4"