class Hash

Public Instance Methods

fetch_all(*args) click to toggle source

example: {:a => 1, “b” => 2, “c” => 3}.fetch_all(:a, :b) {:a => 1, “b” => 2, “c” => 3}.fetch_all([:a, :b]) or [[:a, :b]]

# File lib/monkey_patch_happy/hash_patch.rb, line 5
def fetch_all(*args)
  #values_at不支持数组
  # args.flatten.map{|key| fetch(key)} #如果没找到该键值,会报错。
  #compact 去掉为nil的键
  args.flatten.map{|key| self[key]}
end
format_hour_key() click to toggle source

not test

# File lib/monkey_patch_happy/hash_patch.rb, line 25
def format_hour_key
  new_hash = {}
  self.each do |key, value|
    new_hash[( "h" + (key.to_i + 1).to_s).to_sym] = value
  end
  new_hash
end
format_key_to_sym(str) click to toggle source

str = “h”, hash = {“01” => 1, “02” => 2 } result: {:h1 => 1, :h2 => 2 }

# File lib/monkey_patch_happy/hash_patch.rb, line 15
def format_key_to_sym(str)
  new_hash = {}
  self.each do |key, value|
    new_hash[(str + key.to_i.to_s).to_sym] = value if key.present?
  end
  # self = nil #release
  new_hash
end