0

Possible Duplicate:
Comparing ruby hashes

How can I compare two hashes and show only if name was matching correctly.

element1 = {:name => "Original", :description => "The original one!"}
element2 = {:name => "Original", :description => ""}
Community
  • 1
  • 1
Sri
  • 47
  • 4
  • 12

2 Answers2

2

If a reverse of a diff is what you want then you could try this.

class Hash
  def in_both(other)
    self.keys.inject({}) do |memo, key|
      memo[key] = self[key] if self[key] == other[key]
      memo
    end
  end
end

> element1.in_both(element2)
=> {:name=>"Original"}

or the much shorter

element1.select{|k,v| element2[k]==v}
Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
0

I don't know if this is what you're looking for

element1[:name] == element2[:name]

Or be more specific, please.

erickzetta
  • 681
  • 5
  • 3