I am using ruby ββ1.8.7, and I need to compare the two hashes that I have, which are essentially attributes of the model. Hash A is smaller than Hash B, and Hash B has all the attributes of hash A, as well as some additional attributes that I don't need. My main goal is to check if the elements of A coincide with the corresponding elements of B. So for example
@hash_a = {:cat => 'Bubby', :dog => 'Gizmo'} @hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'} @hash_a == @hash_b
Now itβs a little more complicated, because the fields do not completely coincide, although they refer to the same piece of information
@hash_a = {:cats_name => 'Bubby', :dog => 'Gizmo'} @hash_b = {:cat => 'Bubby', :dog => 'Gizmo', :lion => 'Simba'} @hash_a == @hash_b
What I'm working on is a process that compares two matching elements, updates it if the fields have been changed, and only if they have changed. Or creates a new element if it cannot find the corresponding element. Changing the names of the hash itself is not an option. Currently, I'm just comparing each field in a private method to make sure they are equal.
return hash_a[:cat] == hash_b[:cats_name] && hash_a[:dog] == hash_b[:dog]
I feel that there must be a better way, I am looking for something faster and more elegant than this.
Mysrt source share