我如何做一个差异的两个串或阵列中的红宝石?

有帮助吗?

其他提示

对阵列,使用减员。例如:

>> foo = [1, 2, 3]
=> [1, 2, 3]
>> goo = [2, 3, 4]
=> [2, 3, 4]
>> foo - goo
=> [1]

这里的最后一行中删除一切从foo,也是在咕,只留下的元件1。我不知道如何做到这两个字符串,但直到有人谁知道员额有关,你可以只把每个字符串阵列,使用减操作者,然后将结果回来。

我感到沮丧,与缺乏一个良好图书馆为这红宝石,所以我写了 http://github.com/samg/diffy.它使用 diff 下面,并侧重于便利,并提供相当出选择。

For strings,我第一次尝试红宝石宝石,@sam-藏红花。很容易安装:http://github.com/pvande/differ/tree/master

gem install differ

irb
require 'differ'

one = "one two three"
two = "one two 3"

Differ.format = :color
puts Differ.diff_by_word(one, two).to_s

Differ.format = :html
puts Differ.diff_by_word(one, two).to_s

该HTMLDiff,@特da01提到了上面为我工作。

script/plugin install git://github.com/myobie/htmldiff.git

# bottom of environment.rb
require 'htmldiff'

# in model
class Page < ActiveRecord::Base
  extend HTMLDiff
end

# in view
<h1>Revisions for <%= @page.name %></h1>
<ul>
<% @page.revisions.each do |revision| %>
  <li>
    <b>Revised <%= distance_of_time_in_words_to_now revision.created_at %> ago</b><BR>
      <%= Page.diff(
        revision.changes['description'][0],
        revision.changes['description'][1]
      ) %>
      <BR><BR>
  </li>
<% end %>

# in style.css
ins.diffmod, ins.diffins { background: #d4fdd5; text-decoration: none; }
del.diffmod, del.diffdel { color: #ff9999; }

看起来很好。通过我的方式使用这个用的 acts_as_audited 插件。

还有 diff-lcs 这是作为一个宝石。 它一直没有更新,自2004年以来的但是 我们一直在使用它没有任何问题。

编辑: 新版本是在2011年。看起来像是回到积极发展。

http://rubygems.org/gems/diff-lcs

t=s2.chars; s1.chars.map{|c| c == t.shift ? c : '^'}.join

这个简单的线给一个 ^ 在该职位,不匹配。这往往不够,而且它是复制、粘贴的能力。

我刚刚发现了一个新的项目,该项目似乎相当灵活性:

http://github.com/pvande/differ/tree/master

想出来,并将尝试后一种报告。

我有同样的疑问和解决方案,我发现不是100%的红宝石,但是对我最好的。该问题有差异。rb是,它没有一个漂亮的格式化,以显示出差异,在一个人性化的方式。所以我用比较系统,这个代号:

 def diff str1, str2
   system "diff #{file_for str1} #{file_for str2}"
 end

 private
 def file_for text
   exp = Tempfile.new("bk", "/tmp").open
   exp.write(text)
   exp.close
   exp.path
 end

只是有益于Windows的人:diffy看起来的辉煌,但我相信它只会的工作*尼克斯(正确的我,如果我错了).当然它没有工作我的机器。

不同的工作让我(Windows7 64,Ruby1.8.7).

也许阵列。差异通过猴子贴可以帮助...

http://grosser.it/2011/07/07/ruby-array-diffother-difference-between-2-arrays/

获得逐字符号决议,我增加了一个新的功能 damerau-levenshtein宝石

require "damerau-levenshtein"
differ = DamerauLevenshtein::Differ.new
differ.run "Something", "Smothing"
# returns ["S<ins>o</ins>m<subst>e</subst>thing", 
#  "S<del>o</del>m<subst>o</subst>thing"]

或者与分析:

require "damerau-levenshtein"
require "nokogiri"

differ = DamerauLevenshtein::Differ.new
res = differ.run("Something", "Smothing!")
nodes = Nokogiri::XML("<root>#{res.first}</root>")

markup = nodes.root.children.map do |n|
  case n.name
  when "text"
    n.text
  when "del"
    "~~#{n.children.first.text}~~"
  when "ins"
    "*#{n.children.first.text}*"
  when "subst"
    "**#{n.children.first.text}**"
  end
end.join("")

puts markup
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top