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

Efficient way to update values to array of hashes in ruby?

I have an array of hashes like below: items = [ {"id" => 1, "cost" => '2.00'}, {"id" => 2, "cost" => '6.00'}, {"id" => 1, "cost" => '2.00'}, {"id" => 1, "cost" => '2.00'}, {"id" => 1, "cost" => '2.00'} ] I…
Sri
  • 2,233
  • 4
  • 31
  • 55
0
votes
2 answers

How to get key instead of key's value in a hash using Ruby and also omit the ":"

So I have an example below : movies = { dobby: "dobster is a lad", pirates_of_the_carribean: "Its all about jack sparrow kicking ass!" } puts "what do you want to know about?\n #{movies[:dobby]}. =…
Ahurasim
  • 27
  • 1
  • 8
0
votes
1 answer

Rails Disposable Object in controller

Trying to store objects in an array to iterate through in the view. In my controller: def expire @search = Search.new(search_params) if @search.valid? @clients = [] @allClients = #value from REST API @allClients.each do |client| …
Corey
  • 835
  • 1
  • 9
  • 32
0
votes
1 answer

how to parse the given hash into desired output in ruby on rails?

I had to send daily course update notification to the user. Update notification contain four types of [:due,:missed,:over_due,:new]. To make it easy I omitted unnecessary field from data like due and missed coz it the same as overdue and new format.…
Mukesh Kumar Gupta
  • 1,567
  • 20
  • 15
0
votes
2 answers

Setting hash value (fractions) in ruby and sorting

I receive the following hash: my_hash = {a:(1/20), b:(1/26)} But when I see the hash I get the following: irb(main):019:0> my_hash = {a:(1/20), b:(1/26)} => {:a=>0, :b=>0} irb(main):020:0> my_hash => {:a=>0, :b=>0} As you can see it convert to…
Felipe Maion
  • 336
  • 1
  • 12
0
votes
2 answers

Select specific key from hash in Ruby

I have the following JSON which I convert to hash: <% json = '{ "speed": 50, "braking": 50, "time_on_task": 50 }' json = JSON.parse(json) %> And currently I just loop through them and display them:
    <% json.each do |t| %> …
Cameron
  • 27,963
  • 100
  • 281
  • 483
0
votes
1 answer

how to apply a function to each values of `params`

I am trying to pre-process the params with to_ar2en_i function in ApplicationController before any action processes the params, I have the following in my application_controller.rb: # translates every params' entity from arabic to english…
dariush
  • 3,191
  • 3
  • 24
  • 43
0
votes
2 answers

How to add duplicate keys from XML to a hash

I have a project which takes large amounts of XML data and passes that to Nokogiri, eventually adding each element to a hash outputting to a YAML file. This is works until the XML data set contains duplicate keys. Example Data:
chinds
  • 1,761
  • 4
  • 28
  • 54
0
votes
1 answer

Ruby RoR get keys and values from hash separately

I have a hash like this: hash = {"str1"=>5, "str2"=>9, "str3"=> 3} hash.keys gives as a result ["str1", "str2", "str3"] hash.values gives as a result [5, 9, 3] I'm trying to export this to excel with Axlsx using sheet.add_row sheet.add_row…
media
  • 135
  • 1
  • 7
0
votes
0 answers

Tab Spacing Ruby

I'm trying to replicate a file with a Ruby Script, I have an Hash with the content of the file and then I use JSON.pretty_generate to put the file I generate on the same format as the original file, however the spacing is different. This is the…
mf370
  • 321
  • 3
  • 14
0
votes
1 answer

creating active record using no of hashes in rails

I am getting a no of hashes in my controller which are submitted as part of form data.The form basically contains a list of tasks ie task name ,task type etc each of which is getting submitted a hash.The hashes look like this.Task name hash…
yogeshmanjhi
  • 335
  • 1
  • 4
  • 17
0
votes
2 answers

Ruby return top level hash key if value recursively contains string

I have the data structure below and I am trying to return the top level key (lo, eth0 or eth1) if anywhere recursively and arbitrarily deep within it's value is a given string. Then terminate the search after the first instance of the string is…
Brando__
  • 365
  • 6
  • 24
0
votes
2 answers

Save YAML with inheritance in ruby

I have YAML file with inheritance and I want to add or edit a key programatically. I load the YAML into hash using YAML.load method but when I save the hash back using YAML.dump I lose all the inheritance info. Is there a way to edit the YAML in…
Johnathan Kanarek
  • 834
  • 1
  • 10
  • 16
0
votes
1 answer

RoR,Ruby How to split data from database and use count with it

I'm trying to extract data from database and use it with a pie-chart for example. I was able to extract data from the column "browser" from the database which is a string and has this format : "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101…
media
  • 135
  • 1
  • 7
0
votes
1 answer

RSpec routing spec: sequence of fields in hash

I'm having a problem with the actual vs expected failing due to sequence on the hash. I don't recall seeing this before ... and in any case I thought a hash was unordered? How can I have this test pass? RSpec.describe ArticleSectionsController,…
port5432
  • 5,889
  • 10
  • 60
  • 97