⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 05 - keeping multiple values for the same hash key.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
hash = Hash.new { |hash, key| hash[key] = [] }raw_data = [ [1, 'a'], [1, 'b'], [1, 'c'],              [2, 'a'], [2, ['b', 'c']],             [3, 'c'] ]raw_data.each { |x,y| hash[x] << y }hash # => {1=>["a", "b", "c"], 2=>["a", ["b", "c"]], 3=>["c"]}#---class MultiValuedHash < Hash  def []=(key, value)    if has_key?(key)            super(key, [value, self[key]].flatten)    else      super    end    endendhash = MultiValuedHash.newraw_data.each { |x,y|  hash[x] = y }hash# => {1=>["c", "b", "a"], 2=>["b", "c", "a"], 3=>"c"}#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -