2010-05-12 54 views

回答

5

http://github.com/pvande/differ 你可以使用它,它执行字符串的diff。您必须构建一些逻辑来将其格式化为输出就绪状态。可能在助手中使用Builder :: XmlMarkup。

还有: http://github.com/myobie/htmldiff

这似乎输出标记 - 但它没有很好的记录。

就内建的帮手而言,我不认为Rails内置任何东西。 有http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Diff.html - 但与第一个插件不同,它用于散列而不是字符串。

2

对于今天有人在寻找答案:https://github.com/samg/diffy是最安全的选择。正如这里提到的其他宝石和图书馆自一段时间以来已被抛弃。

+0

是,diffy听起来前途 – Saim 2015-12-03 17:04:44

0

有两种方式:

1.works也非英文字符串

class String 
    def -(other) 
    s1 = self.mb_chars.downcase.chars 
    s2 = other.mb_chars.downcase.chars 
    s1.size >= s2.size ? s1 - s2 : s2 - s1 
    end 
end 

> 'abcde' - 'abc' 
=> ["d", "e"] 
> 'abc' - 'ac' 
=> ["b"] 

2. 从http://tobyho.com/2011/03/26/string-difference-in-ruby/

class String 
    def -(other) 
    self.index(other) == 0 ? self[other.size..self.size] : nil 
    end 
end 

> 'abcde' - 'abc' 
=> "de" 
but 
> 'abc' - 'ac' 
=> nil