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
vote
2 answers

rspec expect compare two hash that by excluding specified keys

Is there any spec expect which will compare two hashes by excluding specified keys alone. H1 = {'name' => 'XXXXx', 'age' => 29, 'DOB' => 'dd/mm/yyyy'} H2 = {'name' => 'XXXXX', 'age' => 29, 'DOB' => 'dd/mm/yyyy'} Compare the above two hashes by…
1
vote
1 answer

Is there a specified enumeration order for hash keys as with hash values?

The Ruby documentation makes a statement about Hash#values: Hashes enumerate their values in the order that the corresponding keys were inserted. This also appears to be true for Hash#keys, but that is not documented. Is this likely to remain…
peterk
  • 5,136
  • 6
  • 33
  • 47
1
vote
2 answers

Transforming deep nested hash into multidimensional array in ruby

I want to transform nested hashes into multidimensional arrays recursivley. This is my current code: def deep_to_a(hash) return hash.to_a.each {|k, v| if k.is_a?(Hash) then hash[k.to_a] = hash.delete(k) deep_to_a(k) elsif…
1
vote
1 answer

how to access value from hash based on key value

I have following hash hash = { "some value": "abc", "other value": "dcd" } The key value is coming from an object Test and I can access it as Test.key I am trying to access hash value from the key which is coming from Test.key. I tried to…
jose1221
  • 243
  • 1
  • 5
  • 13
1
vote
4 answers

Ruby change the order of hash keys

I have a hash and I would like the change the key order from. {"result"=>{"data"=>[{"Quantity"=>13, "Rate"=>17.1}, {"Quantity"=>29,"Rate"=>3.2}, {"Quantity"=>7,…
Shalafister's
  • 761
  • 1
  • 7
  • 34
1
vote
3 answers

Find product's frequency in this CashRegister class

I have 3 simple classes CashRegister, Bill and Position. A CashRegister is composed of Bill objects and a Bill object is composed of Position objects. They're implemented as followed class CashRegister def initialize @bills = [] end def…
Huy Tran
  • 1,770
  • 3
  • 21
  • 41
1
vote
2 answers

Remove hash from an array of hash that contains specific keys

I have this hash : { "car": [ { "key": 'removeMe1', "name": 'ok' }, { "key": 'dontRemoveMe1', "surname": 'ok' }, { "key": 'dontRemoveMe2', "array": [ { "trucks": [ { "key": 'removeMe2', "name": 'my_profile_name' } ] }, …
Mio
  • 1,412
  • 2
  • 19
  • 41
1
vote
3 answers

Ruby calculate the percentage of elements in hash

I have a hash : hash = {"str1"=>2, "str2"=>3, "str3"=>7} I want to calculate the percentage of each element in the hash so I can get one like this : {"str1"=>16.66% , "str2"=>25.00%, "str3"=>58.33%} Any idea about that? Thanks
media
  • 135
  • 1
  • 7
1
vote
1 answer

Get javascript string out of ruby hash sent in JSON

So, I have a ruby program that takes a hash and turns it into a JSON string (lets say the hash is #FFFFFF) and that JSON string is sent to a javascript program where it needs to get #FFFFFF out of the JSON string, i've tried JSON.parse(); to no…
EPICBRONY
  • 45
  • 6
1
vote
2 answers

How can I extract a Summoner Name from a JSON response?

I'm playing around with with external APIs from League of Legends. So far, I've been able to get a response from the API, which returns a JSON object. @test_summoner_name = ERB::Util.url_encode('Jimbo') @url =…
Xadren
  • 315
  • 4
  • 16
1
vote
1 answer

Passing Ruby Hash into Classes

I ran into a study drill problem, and I couldn't figure it out. Here's the link to the exercise. https://learnrubythehardway.org/book/ex40.html Below are my work. On Study Drill 2, I passed in variables and it worked. However, at study drill 3,…
Joey
  • 13
  • 5
1
vote
4 answers

check hash value within an array

I have an array (array1) of hashes that looks like this : array1 = [ {:ID=>"1", :value=>"abc"}, {:ID=>"2", :value=>"def"} ] I can loop over each hash and check each hash value manually: array1.each do |h| if…
danynl
  • 279
  • 1
  • 4
  • 18
1
vote
3 answers

How to return a string when there is an non-existent element in a hash using an array

I have this hash and this array, and execute the following command... hash={"a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5,"f"=>6} array=["b","a","c","f","z","q"] print hash.values_at(*array).compact So I want it to return something like: #=>…
Andre
  • 11
  • 1
1
vote
1 answer

Ruby - Parse a file into hash

I've a file containing hundreds of object and value combination like below manner. I want to get input from user as object name & numeric value and return that associated value. Object cefcFRUPowerOperStatus Type PowerOperType …
TheMightyNight
  • 161
  • 1
  • 2
  • 13
1
vote
5 answers

Splitting up an array into a hash

I have an array of about 860 items, I would like to turn it into a hash, the array structure is (Key1, value 1, value2, value 3, Key2, value 1, value 2, value 3........... etc etc) except in real life it looks like (Peter, 150, 39, 345, John, 123,…
D133p53
  • 59
  • 7