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

Adding a hash that has an array in Ruby and adding to that array

Hello all I'm new and I've been searching and can't find a clear answer to this question, I'm hoping this summarizes it: a = {:a1 => "a1", :b1 => [] } how would I add :c1 to the a hash and make it another hash, leading to an array, and add values…
-1
votes
1 answer

Duplicate Hash Key unique Pair

I have a data pair: [{:mobile=>21, :web=>43},{:mobile=>23, :web=>543},{:mobile=>23, :web=>430},{:mobile=>34, :web=>13},{:mobile=>26, :web=>893}] How can I make this into: [{mobile: [21, 23, 34, 26]}, {web: [43, 543, 430, 13, 893]}
Sudhir Vishwakarma
  • 785
  • 10
  • 15
-1
votes
2 answers

How to create a Hash from a nested CSV in Ruby?

I have a CSV in the following format: name,contacts.0.phone_no,contacts.1.phone_no,codes.0,codes.1 YK,1234,4567,AB001,AK002 As you can see, this is a nested structure. The CSV may contain multiple rows. I would like to convert this into an array of…
Yedhu Krishnan
  • 1,225
  • 15
  • 31
-1
votes
3 answers

How to merge hashes inside array?

How do I merge the hashes in these arrays: description = [ { description: "Lightweight, interpreted, object-oriented language ..." }, { description: "Powerful collaboration, review, and code management ..." } ] title = [ { title: "JavaScript"…
Ganesh Raju
  • 95
  • 1
  • 14
-1
votes
2 answers

Translate Ruby hash (key,value) to separate keys

I have a map function in ruby which returns an array of arrays with two values in each, which I want to have in a different format. What I want to have: "countries": [ { "country": "Canada", "count": 12 }, {and so on... } ] But…
amaridev
  • 31
  • 4
-1
votes
2 answers

Array of paths from Nested Hash

I have a hash my_hash = {"key1"=> {"key2"=> {"key3"=> "value"}}, "key4"=> "value"} I want to return only the full path of the keys as an array. All concatenated like this [key1, key1key2, key1key2key3, key4]. Any suggestions on how to do this.…
iyokeose
  • 1
  • 3
-1
votes
1 answer

How to push a value to an array value in a hash

I'm trying to set different arrays as the values of a hash using a range, then push a value into one of the arrays. I want this outcome: hash[0] << 3 hash #=> {0=>[3], 1=>[], 2=>[], 3=>[]} I did this: hash =…
Wolf_Tru
  • 523
  • 7
  • 19
-1
votes
3 answers

Map key and Value in a hash into 2 new value with static keys

so I have an array of hashes (json format) that look like this [ { "2017-04-01 00:00:00 UTC": 100.992226272734 }, { "2017-03-01 00:00:00 UTC": 3.0594465818934964 }, ] now i want to map each key into new value with a static key…
-1
votes
4 answers

Fill Hash with nil values if no values given

I have these arrays: positions = [[0, 1, 2], [2, 3]] values = [[15, 15, 15], [7, 7]] keys = [1, 4] I need to create a hash whose keys are from keys and the values are from values. Values must be at indices defined in positions. If no index is…
Julius Dzidzevičius
  • 10,775
  • 11
  • 36
  • 81
-2
votes
1 answer

Rails Create hash from two other hash

I have two hash max and min contain same key max {22479=>2018-05-24 20:15:11 UTC, 15392=>2018-04-24 09:38:32 UTC, 16319=>2018-04-27 02:21:53 UTC, 24644=>2018-06-07 20:01:04 UTC, 21551=>2018-05-19 20:11:11 UTC, 17876=>2018-05-03 15:47:54 UTC,…
compsy
  • 233
  • 4
  • 12
-2
votes
1 answer

Access Hash inside helper of another page

I currently have a helper titled directors_helper.rb with a hash predefined inside of it. module DirectorsHelper def dirs = { gm: { pos: "General Manager", email: "123@abc.com" }, prod: { pos: "Production Director", email:…
jasonsogan
  • 11
  • 1
  • 4
-2
votes
1 answer

Convert array with integers which indicate hash depth to hash

I have the following array: [{text: "a", depth: 0}, {text: "b", depth: 1}, {text: "c", depth: 2}, {text: "d", depth: 1}] The problem I'm trying to solve is to take a flattened array (above) and create a nested structure based on each element's…
-2
votes
1 answer

How to access two elements within nested hashes within an array?

I have the following array with nested hashes: pizza = [ { flavor: "cheese", extras: { topping1: 1, topping2: 2, topping3: 3} }, { flavor: "buffalo chicken", extras: { topping1: 1, topping2: 2, topping3: 3} } } ] If want to verify…
-2
votes
4 answers

Ruby RoR split a hash

I have a hash like this : hash = {"user:**xy@xy.com** password:**azerty**"=>1, "user:**yy@yy.com** password:**qwerty**"=>1} How can I extract only the username (email) and the password of each element to use it separately. With split method…
media
  • 135
  • 1
  • 7
-2
votes
1 answer

I want to convert array of hash to single hash with specific format in ruby

I have a hash called hash1 hash1 = [{key: 'key1', value: 'value1'}, {key: 'key2', value: 'value2'}, {key: 'key3', value: 'value3'}] And want to convert to below format {'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}
Vinay
  • 324
  • 1
  • 3
  • 15
1 2 3
12
13