ruby-hash

Next key/value pair overwrites the existing pair in a hash while trying to add pair with new key

孤人 提交于 2020-01-06 05:44:27
问题 I have: fruits = { "orange" => {:season => "winter"}, "apple" => {:season => "winter"}, "banana" => {:season => "summer"}, "grape" => {:season => "spring"}, "peach" => {:season => "winter"}, "pineapple" => {:season => "summer"} } I want to get: { "winter"=>["orange", "apple", "peach"], "summer"=>["banana", "pineapple"], "spring"=>["grape"] } I did: def sort_fruits(fruits_hash) fruits=[] sorted = {} seasons = fruits_hash.map {|k, v|v[:season]} seasons.uniq.each do |season| fruits.clear fruits

Is there a solution to bypass 'can't add a new key into hash during iteration (RuntimeError)'?

空扰寡人 提交于 2019-12-18 18:53:31
问题 I have a big problem with the expected RuntimeError: "can't add a new key into hash during iteration" In my case a I have a YAML file: test.yaml - in which I have some keys already added. test.yaml key1: key2: key3: I am getting the contents of the file in a variable: file_hash = YAML.load_file("testm.yaml") Then I need to loop through this hash and add other keys to them: file_hash.each do |key| file_hash[key] = 'key_1' file_hash[key] = 'key_2' end File.open('test.yaml', 'w') { |f| YAML.dump

How to remove a key from Hash and get the remaining hash in Ruby/Rails?

梦想的初衷 提交于 2019-12-17 14:59:40
问题 To add a new pair to Hash I do: {:a => 1, :b => 2}.merge!({:c => 3}) #=> {:a => 1, :b => 2, :c => 3} Is there a similar way to delete a key from Hash ? This works: {:a => 1, :b => 2}.reject! { |k| k == :a } #=> {:b => 2} but I would expect to have something like: {:a => 1, :b => 2}.delete!(:a) #=> {:b => 2} It is important that the returning value will be the remaining hash, so I could do things like: foo(my_hash.reject! { |k| k == my_key }) in one line. 回答1: Rails has an except/except!

How this one attribute here hold multiple attribute in ruby class?

僤鯓⒐⒋嵵緔 提交于 2019-12-13 18:24:42
问题 Here as you see we have one attribute called "attributes" and we initialize it in our class, so the question is where the name and shirt attributes come from, as we dont initialize and define them in our class? class Shirt attr_accessor :attribute def initialize(attributes) @attributes = attributes end end store = Shirt.new(name: "go", size: "42") Also when I inspect this instance of the shirt class I get a hash: @attributes={:name=>"go", :size=>"42"} Anyone can help explain it? 回答1: In Ruby

Ruby return top level hash key if value recursively contains string

僤鯓⒐⒋嵵緔 提交于 2019-12-13 07:08:49
问题 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 found. Find key/value pairs deep inside a hash containing an arbitrary number of nested hashes and arrays This is sort of similar to what I'm trying to do but I haven't been able to map it to my own problem h.find{ |k,v| break k if v.include? "number"

Is there a solution to bypass 'can't add a new key into hash during iteration (RuntimeError)'?

北城余情 提交于 2019-11-30 17:36:51
I have a big problem with the expected RuntimeError: "can't add a new key into hash during iteration" In my case a I have a YAML file: test.yaml - in which I have some keys already added. test.yaml key1: key2: key3: I am getting the contents of the file in a variable: file_hash = YAML.load_file("testm.yaml") Then I need to loop through this hash and add other keys to them: file_hash.each do |key| file_hash[key] = 'key_1' file_hash[key] = 'key_2' end File.open('test.yaml', 'w') { |f| YAML.dump(file_hash, f) } The main issue is that I am unable to write into a hash while in a loop. I don't

How to remove a key from Hash and get the remaining hash in Ruby/Rails?

こ雲淡風輕ζ 提交于 2019-11-27 16:35:18
To add a new pair to Hash I do: {:a => 1, :b => 2}.merge!({:c => 3}) #=> {:a => 1, :b => 2, :c => 3} Is there a similar way to delete a key from Hash ? This works: {:a => 1, :b => 2}.reject! { |k| k == :a } #=> {:b => 2} but I would expect to have something like: {:a => 1, :b => 2}.delete!(:a) #=> {:b => 2} It is important that the returning value will be the remaining hash, so I could do things like: foo(my_hash.reject! { |k| k == my_key }) in one line. Rails has an except/except! method that returns the hash with those keys removed. If you're already using Rails, there's no sense in creating