4

我正在使用 ruby​​ 1.8.7,我需要比较我拥有的两个哈希值,它们本质上是模型的属性。Hash A 比 Hash B 小,Hash B 具有 hash A 的所有属性,加上一些我不关心的额外属性。我的首要目标是查看 A 的元素是否与 B 的相应元素相同。例如

@hash_a = {:cat => 'Bubby', :dog => 'Gizmo'}  
@hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'}  
@hash_a == @hash_b
#=> true

现在它变得比这更复杂一些,因为这些字段并不完全匹配,即使它们引用了相同的信息

@hash_a = {:cats_name => 'Bubby', :dog => 'Gizmo'}  
@hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'}  
@hash_a == @hash_b
#=> true

我正在做的是一个比较两个匹配项的过程,如果字段发生了变化,并且只有当它们发生变化时才更新它。或者如果找不到匹配的项目,则创建一个新项目。更改哈希本身的名称不是一种选择。目前我只是比较私有方法中的每个字段,看看它们是否相等。

return hash_a[:cat] == hash_b[:cats_name] && hash_a[:dog] == hash_b[:dog] 

我觉得必须有更好的方法,我正在寻找比这更快、更优雅的方法。

4

4 回答 4

3

如果将散列转换为数组,则可以像这样比较它们。

@hash_a.to_a == (@hash_a.to_a & @hash_b.to_a)

如果需要,您还可以将此代码隐藏在哈希类中的方法后面:

class Hash
  def diff_equal(other)
    to_a == (to_a & other.to_a)
  end
end

然后像@hash_a.diff_equal(@hash_b). 如果您选择了该路径,您可能需要检查 other 是否为散列或响应该to_a方法。

于 2010-09-21T21:20:29.747 回答
2

这是我的做法:

def eql hash1, hash2, rewire = {}
  map = Hash.new {|h, key| rewire[key] || key}
  !hash1.any? do |key, val| 
    hash2[map[key]] != val
  end
end


hash_a = {:cats_name => 'Bubby', :dog => 'Gizmo'}  
hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'}  
p eql(hash_a, hash_b) #=> false

hash_a = {:cats_name => 'Bubby', :dog => 'Gizmo'}  
hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'}  
p eql(hash_a, hash_b, :cats_name => :cat)  #=> true


hash_a = {:cat => 'Bubby', :dog => 'Gizmo'}  
hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'}  
p eql(hash_a, hash_b) #=> true

hash_a = {:cat => 'Bubby', :dog => 'Gizmo', :fish => "Wanda"}  
hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'}  
p eql(hash_a, hash_b) #=> false

不会太长,而且似乎也能像你想要的那样工作:)

于 2010-09-21T20:30:40.807 回答
1

嘿,如果你真的想要快速和优雅,你去:

(a = @hash_a.values; (a & @hash_b.values) == a)

有一些明显的限制......

于 2010-09-21T19:59:03.110 回答
0

一种可能性是首先重新映射一个哈希的键,然后执行一组子集操作:

require 'set'

def remap_keys(hash, key_map)
  hash.inject({}) do |acc, pair|
    key, value = pair
    remapped_key = key_map[key] || key
    acc[remapped_key] = value
    acc
  end
end

def hash_subset?(a, b)
  set_a = Set.new(a)
  set_b = Set.new(b)
  set_a.subset?(set_b)
end

hash_a = {:cats_name => 'Bubby', :dog => 'Gizmo'}  
hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'}  

puts hash_subset?(remap_keys(hash_a, {:cats_name => :cat}), hash_b)

但是,我确信有更有效的方法可以做到这一点。不止一种皮肤的方法:cat,嗯?!

于 2010-09-21T19:50:40.993 回答