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

Generate nested hash from string

I want to generate nested hash from string. Input: key: a.b.c value: true Output: {:a=>{:b=>{:c=>true}}} I have the following method from the stackoverflow: Generate nested hashes from strings and deep merging in ruby hash =…
1
vote
1 answer

Can't sort hash tried few different ways

I want to sort hash by position, I am using sort_by but it is not sorting out, as it should hash = { "a": {"name": "a", "type": "text", "position": 1, "required": "false"}, "b": {"name": "b", "type": "text", "position": 4, "required": "false"}, "c":…
Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
1
vote
4 answers

Combine 2 hashes with the same key

I am wanting to combine 2 hashes that have the same keys. @clean_by_hour = Sale.where(item_name: clean).group_by_hour_of_day(:complete_time, format: "%-l %P").count => {"12 am"=>0, "1 am"=>0, "2 am"=>0, "3 am"=>0, "4 am"=>0, "5 am"=>0, "6 am"=>0, "7…
1
vote
2 answers

Filtering out hashes from arrays of hashes that have matching keys

Say I have an array of hashes like so default_search_order = [ { field: 'subscribers.nickname', direction: 'ASC' }, { field: 'subscribers.email', direction: 'ASC' }, { field: 'roles.name', direction: 'ASC' }, {…
Josh Wren
  • 338
  • 2
  • 12
1
vote
3 answers

How to iterate over deep nested hash without known depth in Ruby

I have multiple YAML (localization) files. I parse them and convert to hash in Ruby. For example this is one of them: hello: Hallo messages: alerts: yay: Da! no: Nein deep: nested: another: level: hi:…
Slasher
  • 13
  • 4
1
vote
1 answer

How to optimize mapping hash that contains similar keys and values?

I've got some constants defined like this CONSUMER_TYPE = 'consumer' CONSUMER_1_TYPE = "#{CONSUMER_TYPE}1" CONSUMER_2_TYPE = "#{CONSUMER_TYPE}2" CONSUMER_3_TYPE = "#{CONSUMER_TYPE}3" INDUSTRIAL_TYPE = 'industrial' INDUSTRIAL_1_TYPE =…
Alex Nikolsky
  • 2,087
  • 6
  • 21
  • 36
1
vote
1 answer

Sorting Ruby Array of hashes with 2 date keys

I have an array of hashes with 2 keys that have timestampvalues (YYYY/MM/DD/HH/MM/SS) : start_date and end_date. Array_initial = [ { :started_at => 20201105143200, :ended_at => 20201105143900 }, { :started_at => 20201105142900, :ended_at =>…
sandino
  • 11
  • 4
1
vote
2 answers

Is there an elegant way in Ruby to filter a hash of arrays of hashses?

Let's say I have a this type of data structure: { "foo": [{state: on}, {state: off}, {state: on}], "bar": [{state: off}, {state: off}, {state: on}], "baz": [{state: on}, {state: on}, {state: on}] } How can I filter the nested hash arrays in…
Jarad DeLorenzo
  • 329
  • 2
  • 10
1
vote
2 answers

Does ruby has a hash mode which only support updating exist key but not adding new keys?

For example, I have a hash, in which updating is valid but adding new key is invalid. opts = { url: 'www.google.com', local: 'disk', limit: 10 } opts[:url] = 'www.facebook.com' # valid opts[:other] = 'www.apple.com' # should raise an error
Run
  • 876
  • 6
  • 21
1
vote
2 answers

Reflecting value of hash in Array Rails

I have one Array. arr = [] I have one Hash hash = {a: 1, b: 2, c: 3} Addind hash in Array arr << hash value of arr is: [{:a=>1, :b=>2, :c=>3}] Now Adding value in Hash hash[:d] = 4 Now See Value of Array is: [{:a=>1, :b=>2, :c=>3, :d=>4}] Can…
Ketan Mangukiya
  • 360
  • 1
  • 7
1
vote
4 answers

Why is only one value in my hash being changed?

I'm making a simple RPG as a learning project, and am having an issue with part of the character creator. This code should determine what skill string is assigned to player[:caste][:skill] and player[:sub][:skill], then increase each respective…
Richard
  • 164
  • 7
1
vote
3 answers

Why am I unable to edit the values in this hash?

I'm trying to create a new hash called $player[:abil_mods] which is based on my $player[:abils] hash. It should take each value, subtract 10, divide by 2, and assign it to an identical key in the new hash. However, it doesn't seem to be editing the…
Richard
  • 164
  • 7
1
vote
1 answer

How to check if a string is equal to any key within a hash and editing another hash based on the result

I'm trying to write a character creator for an RPG, but there are two things I have questions about. How can I check if a string is equal to any key within a hash, in this case, if race_choice is equal to any key within races? How can I change…
Richard
  • 164
  • 7
1
vote
2 answers

Ruby hash only return TRUE

If string any of the Value matches, I want to output the value Code: list = { "red" => ["apple", "cherry"], "blue" => ["sky", "cloud"], "white" => ["paper"] } str = "testString" list.each do |k, v| puts "string: #{str}" puts…
useeffect
  • 59
  • 1
  • 5
1
vote
2 answers

Finding Longest Substring No Duplicates - Help Optimizing Code [Ruby]

So I've been trying to solve a Leetcode Question, "Given a string, find the length of the longest substring without repeating characters." For example Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Currently…
PastaCountry
  • 117
  • 2
  • 8