code_junkie

diff a ruby string or array

Nov 10th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. >> foo = [1, 2, 3]
  2. => [1, 2, 3]
  3. >> goo = [2, 3, 4]
  4. => [2, 3, 4]
  5. >> foo - goo
  6. => [1]
  7.  
  8. gem install differ
  9.  
  10. git clone git://github.com/myobie/htmldiff.git
  11. cd htmldiff
  12. gem build htmldiff.gemspec
  13. sudo gem install htmldiff-x.y.z.gem
  14.  
  15. script/plugin install git://github.com/myobie/htmldiff.git
  16.  
  17. # bottom of environment.rb
  18. require 'htmldiff'
  19.  
  20. # in model
  21. class Page < ActiveRecord::Base
  22. extend HTMLDiff
  23. end
  24.  
  25. # in view
  26. <h1>Revisions for <%= @page.name %></h1>
  27. <ul>
  28. <% @page.revisions.each do |revision| %>
  29. <li>
  30. <b>Revised <%= distance_of_time_in_words_to_now revision.created_at %> ago</b><BR>
  31. <%= Page.diff(
  32. revision.changes['description'][0],
  33. revision.changes['description'][1]
  34. ) %>
  35. <BR><BR>
  36. </li>
  37. <% end %>
  38.  
  39. # in style.css
  40. ins.diffmod, ins.diffins { background: #d4fdd5; text-decoration: none; }
  41. del.diffmod, del.diffdel { color: #ff9999; }
  42.  
  43. def diff str1, str2
  44. system "diff #{file_for str1} #{file_for str2}"
  45. end
  46.  
  47. private
  48. def file_for text
  49. exp = Tempfile.new("bk", "/tmp").open
  50. exp.write(text)
  51. exp.close
  52. exp.path
  53. end
Add Comment
Please, Sign In to add comment