Project

diffing

0.0
No commit activity in last 3 years
No release in over 3 years
Diffing of two strings
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
>= 0
 Project Readme

Diffing

Build Status

Installation

sudo gem install diffing

Usage

require 'diffing'

from = "Hello! I am string for diffing test"
to   = "Hi! I am two string for diffing"

By chars

diff = Diffing.by_chars( from, to )

diff.as_ascii
# => H{`ello`>>`i`}! I am{+` two`} string for diffing{-` test`}
diff.as_html
# => H<del>ello</del><ins>i</ins>! I am<ins> two</ins> string for diffing<del> test</del>

By words

diff = Diffing.by_words( from, to )

diff.as_ascii
# => {`Hello!`>>`Hi!`} I am{+` two`} string for diffing{-` test`}
diff.as_html
# => <del>Hello!</del><ins>Hi!</ins> I am<ins> two</ins> string for diffing<del> test</del>

By lines

diff = Diffing.by_lines( from, to )

diff.as_ascii
# => {`Hello! I am string for diffing test`>>`Hi! I am two string for diffing`}
diff.as_html
# => <del>Hello! I am string for diffing test</del><ins>Hi! I am two string for diffing</ins>

Custom format

module CustomFormat
  class << self

    def source( value )
      value
    end

    def insert( value )
      "(++#{ value })"
    end

    def remove( value )
      "(--#{ value })"
    end

    def change( from, to )
      "(#{ from } => #{ to })"
    end

  end
end


Diffing.by_chars( from, to ).format( CustomFormat )
# => H(ello => i)! I am(++ two) string for diffing(-- test)
Diffing.by_words( from, to ).format( CustomFormat )
# => (Hello! => Hi!) I am(++ two) string for diffing(-- test)
Diffing.by_lines( from, to ).format( CustomFormat )
# => (Hello! I am string for diffing test => Hi! I am two string for diffing)

Custom use separated parts

Diffing.by_words( from, to ).parts.map { |part|
  
  result = ''
  result << "<source:#{ part.source }>" if part.source?
  result << "<insert:#{ part.insert }>" if part.insert?
  result << "<remove:#{ part.remove }>" if part.remove?
  result
  
}.join 
# => <insert:Hi!><remove:Hello!><source: I am><insert: two><source: string for diffing><remove: test>

Custom pattern of parts

Diffing::Diff.new( from, to, /.{,3}\s?/ ).as_ascii
# => {`Hello! `>>`Hi! `}I a{`m string `>>`m two string `}for diffin{`g test`>>`g`}

Copyright

Copyright © 2015 Denis Churbanov