13 - remapping the keys and values of a hash.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 19 行

RB
19
字号
class Hash  def tied_with(hash)    remap do |h,key,value|      h[hash[key]] = value  end.delete_if { |key,value| key.nil? || value.nil? }end#---  def remap(hash={})    each { |k,v| yield hash, k, v }    hash  endend#---a = {1 => 2, 3 => 4}b = {1 => 'foo', 3 => 'bar'}a.tied_with(b)                      # => {"foo"=>2, "bar" =>4}b.tied_with(a)                      # => {2=>"foo", 4=>"bar"}#---

⌨️ 快捷键说明

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