Module: Differ

Defined in:
lib/differ.rb,
lib/differ/diff.rb,
lib/differ/change.rb,
lib/differ/string.rb,
lib/differ/format/html.rb,
lib/differ/format/ascii.rb,
lib/differ/format/color.rb

Defined Under Namespace

Modules: Format, StringDiffer Classes: Change, Diff

Class Method Summary collapse

Class Method Details

.diff(target, source, separator = "\n") ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/differ.rb', line 10

def diff(target, source, separator = "\n")
  old_sep, $; = $;, separator

  target = target.split(separator)
  source = source.split(separator)

  $; = '' if separator.is_a? Regexp

  @diff = Diff.new
  advance(target, source) until source.empty? || target.empty?
  @diff.insert(*target) || @diff.delete(*source)
  return @diff
ensure
  $; = old_sep
end

.diff_by_char(to, from) ⇒ Object



26
27
28
# File 'lib/differ.rb', line 26

def diff_by_char(to, from)
  diff(to, from, '')
end

.diff_by_line(to, from) ⇒ Object



34
35
36
# File 'lib/differ.rb', line 34

def (to, from)
  diff(to, from, "\n")
end

.diff_by_word(to, from) ⇒ Object



30
31
32
# File 'lib/differ.rb', line 30

def diff_by_word(to, from)
  diff(to, from, /\b/)
end

.formatObject



42
43
44
# File 'lib/differ.rb', line 42

def format
  return @format || Format::Ascii
end

.format=(f) ⇒ Object



38
39
40
# File 'lib/differ.rb', line 38

def format=(f)
  @format = format_for(f)
end

.format_for(f) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/differ.rb', line 46

def format_for(f)
  case f
  when Module then f
  when :ascii then Format::Ascii
  when :color then Format::Color
  when :html  then Format::HTML
  when nil    then nil
  else raise "Unknown format type #{f.inspect}"
  end
end