Compare hashes and report where the change was

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Compare hashes and report where the change was

Abdel Latif

Hi,


I would like to compare two hashes, for example:

array1 = [{'id' => 124, 'name' => 'Kamal', 'job' => 'manager'},
{'id' => 314, 'name' => 'John', 'job' => 'developer'}]
array2 = [{'id' => 124, 'name' => 'Kamal', 'job' => 'managerZ'},
{'id' => 314, 'name' => 'JohnDD', 'job' => 'developer'}]


the id is not always 'id' , it could be UserID or EmployeeID or else

I want to have :

def compare_rows(array1,array2,id)

...

end



I want to report:

  • row with UserID equal 124, job changed from 'manager' to 'managerZ'
  • row with UserID equal 314, name changed from 'John' to 'JohnDD'
Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c334d25a-c755-4a86-b1e5-149b3997ffa9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compare hashes and report where the change was

kaine wright

If you want to get what is the difference between two hashes, you can do this:


result = {} hash1.each {|key, value| result[key] = hash2[key] if hash2[key] != value } puts result

or use a this hash1.to_a == hash2.to_a turning it to an array and compairing those. or you could use to_s compare strings.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/de8e95af-a6ac-4724-8fe6-9bd7f9017cda%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.