### Install Diffy Gem on Unix
Source: https://github.com/samg/diffy/blob/main/README.md
Instructions for installing the Diffy gem on Unix-based systems using the RubyGems package manager. This is a prerequisite for using Diffy's diffing capabilities.
```bash
gem install diffy
```
--------------------------------
### Configuring Default Options for Diffy in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This Ruby example shows how to inspect and modify the global default options for the Diffy gem. You can view the current defaults using `Diffy::Diff.default_options` and update them using `merge!`. These defaults can be overridden on a per-instance basis. It also demonstrates setting the default output format.
```ruby
require 'diffy'
# View current defaults
puts Diffy::Diff.default_options.inspect
# Modify defaults
Diffy::Diff.default_options.merge!(:source => 'files', :context => 3)
# All new diffs use these defaults (can still be overridden per instance)
diff = Diffy::Diff.new('/tmp/file1.txt', '/tmp/file2.txt') # Uses :source => 'files' by default
# Set default output format
Diffy::Diff.default_format = :html
diff = Diffy::Diff.new("foo\n", "bar\n")
puts diff.to_s # Outputs HTML by default
```
--------------------------------
### Chunk-Based Iteration in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This Ruby example shows how to iterate over diffs in chunks, where each chunk represents a contiguous group of changes (additions, deletions, or unchanged sections). It uses the `each_chunk` method and demonstrates converting the chunks to an array or counting them.
```ruby
require 'diffy'
string1 = "foo\nbar\nbang\nbaz\n"
string2 = "foo\nbar\nbing\nbong\n"
diff = Diffy::Diff.new(string1, string2)
# Iterate over chunks (grouped changes)
diff.each_chunk do |chunk|
puts "Chunk: #{chunk.inspect}"
end
# Convert to array for processing
chunks = diff.each_chunk.to_a
# Use with enumerable methods
chunk_count = diff.each_chunk.count
```
--------------------------------
### Get Diffy CSS Styles
Source: https://github.com/samg/diffy/blob/main/README.md
Retrieves the default CSS styles provided by Diffy for rendering HTML diffs. These styles can be included directly in your HTML or adapted.
```ruby
puts Diffy::CSS
```
--------------------------------
### Ignoring Whitespace Differences in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This Ruby example demonstrates how to ignore whitespace differences between two strings when generating a diff. It uses the `:diff => "-w"` option to pass the `-w` flag to the underlying diff command, effectively ignoring whitespace changes. It also shows how to pass multiple diff options.
```ruby
require 'diffy'
string1 = " foo\nbar\n"
string2 = "foo\nbar\n"
# Ignore whitespace with diff -w option
diff = Diffy::Diff.new(string1, string2, :diff => "-w", :allow_empty_diff => false)
puts diff.to_s
# Pass multiple diff options
diff = Diffy::Diff.new(string1, string2, :diff => ["-w", "-U 3"])
```
--------------------------------
### Handle Invalid Format Option with ArgumentError in Diffy
Source: https://context7.com/samg/diffy/llms.txt
Illustrates that Diffy raises an ArgumentError when an unsupported format option is passed to the `to_s` method. The example shows how to catch this error and displays the expected error message, listing available formats.
```ruby
begin
diff = Diffy::Diff.new("foo", "bar")
diff.to_s(:invalid_format)
rescue ArgumentError => e
puts e.message # => Format :invalid_format not found in ["text", "color", "html_simple", "html"]
end
```
--------------------------------
### Safely Handle Non-UTF8 Bytes with Diffy
Source: https://context7.com/samg/diffy/llms.txt
Shows how Diffy can correctly process strings containing non-UTF8 bytes without raising errors. The example generates a diff between two strings, one of which includes invalid UTF-8 sequences, and prints the result.
```ruby
string1 = "Foo ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000\n"
string2 = "Bar ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000\n"
diff = Diffy::Diff.new(string1, string2)
puts diff.to_s # Works correctly despite invalid UTF-8 bytes
```
--------------------------------
### Enable Full Diff Output with Diffy
Source: https://github.com/samg/diffy/blob/main/README.md
Explains how to include the full diff information, typically removed by default, by using the :include_diff_info => true option. This preserves the header and context lines from the diff command.
```Ruby
Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n", :include_diff_info => true).to_s(:text)
```
--------------------------------
### Set Context Lines for Diffy Changes
Source: https://github.com/samg/diffy/blob/main/README.md
Illustrates how to specify the number of context lines around each change using the :context option. This overrides the default of 10000 lines, which aims to show the full file.
```Ruby
puts Diffy::Diff.new("foo\nfoo\nBAR\nbang\nbaz", "foo\nfoo\nbar\nbang\nbaz", :context => 1)
```
--------------------------------
### Diff Files Instead of Strings with Diffy
Source: https://github.com/samg/diffy/blob/main/README.md
Shows how to use the :source option to compare the content of two files instead of direct strings. This is useful for version control and file analysis.
```Ruby
puts Diffy::Diff.new('/tmp/foo', '/tmp/bar', :source => 'files')
```
--------------------------------
### Include Plus/Minus Symbols in Diffy HTML Output
Source: https://github.com/samg/diffy/blob/main/README.md
Demonstrates how to include the '+', '-', and ' ' symbols at the beginning of each line in HTML output using the :include_plus_and_minus_in_html option. This enhances clarity for line additions, deletions, and unchanged lines.
```Ruby
puts Diffy::Diff.new(string1, string2, :include_plus_and_minus_in_html => true).to_s(:html_simple)
```
--------------------------------
### Environment Variable Configuration for Diff Binary in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This Ruby snippet demonstrates how Diffy respects the `DIFFY_DIFF` environment variable. If set, Diffy will use the specified diff binary for comparisons. This allows you to easily switch or specify the diff implementation used by the gem.
```ruby
require 'diffy'
# Diffy will use the diff binary specified in DIFFY_DIFF environment variable
diff = Diffy::Diff.new("foo\n", "bar\n")
puts diff.to_s
```
--------------------------------
### Diff Two Strings (HTML GitHub-like Output)
Source: https://github.com/samg/diffy/blob/main/README.md
Produces an HTML diff with inline highlighting, similar to GitHub's diff view. This format is suitable for web interfaces where clear visual diffs are needed.
```ruby
puts Diffy::Diff.new("foo\n", "Foo\n").to_s(:html)
```
--------------------------------
### File Diffing in Ruby
Source: https://context7.com/samg/diffy/llms.txt
Compares two files using Diffy by specifying the :source option as 'files'. This method handles file paths, including those with spaces.
```ruby
require 'diffy'
# Compare two files
diff = Diffy::Diff.new('/tmp/file1.txt', '/tmp/file2.txt', :source => 'files')
puts diff.to_s
# Works with file paths containing spaces
diff = Diffy::Diff.new('/tmp/my documents/file1.txt', '/tmp/my documents/file2.txt', :source => 'files')
puts diff.to_s
```
--------------------------------
### Custom Formatting with Enumerable Interface in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This Ruby snippet demonstrates how to iterate over diff lines using the Enumerable interface provided by Diffy. It shows how to process each line individually to create custom output formats, such as logging added, removed, or unchanged lines. It also illustrates using `map` for transformations and `compact` to filter out nil values.
```ruby
require 'diffy'
string1 = "foo\nbar\n"
string2 = "foo\nbar\nbaz\n"
diff = Diffy::Diff.new(string1, string2)
# Iterate over each line
diff.each do |line|
case line
when /^\+/ then puts "Added: #{line.chomp}"
when /^-/ then puts "Removed: #{line.chomp}"
when /^ / then puts "Unchanged: #{line.chomp}"
end
end
# Use map for transformations
custom_output = diff.map do |line|
case line
when /^\+/ then "line #{line.chomp} added"
when /^-/ then "line #{line.chomp} removed"
end
end.compact.join("\n")
puts custom_output
```
--------------------------------
### Setting Default Diff Options in Diffy
Source: https://github.com/samg/diffy/blob/main/README.md
This section shows how to configure default options for `Diffy::Diff` instances. You can retrieve the current default options using `Diffy::Diff.default_options` and modify them using `Diffy::Diff.default_options=`. Options passed during instantiation of `Diffy::Diff.new` will override or merge with these defaults.
```ruby
puts Diffy::Diff.default_options
Diffy::Diff.default_options.merge!(:source => 'files')
```
--------------------------------
### Handle Invalid Source Option with ArgumentError in Diffy
Source: https://context7.com/samg/diffy/llms.txt
Demonstrates how Diffy raises an ArgumentError when an invalid source option is provided during Diffy::Diff initialization. It shows the expected error message and how to catch it.
```ruby
begin
diff = Diffy::Diff.new("foo", "bar", :source => 'invalid')
rescue ArgumentError => e
puts e.message # => Invalid :source option "invalid". Supported options are 'strings' and 'files'.
end
```
--------------------------------
### HTML Diff with Inline Highlighting in Ruby
Source: https://context7.com/samg/diffy/llms.txt
Produces HTML output with character-level highlighting for changes between lines. It also provides Diffy::CSS and Diffy::CSS_COLORBLIND_1 for styling.
```ruby
require 'diffy'
string1 = "foo\n"
string2 = "Foo\n"
# HTML with inline character highlighting
html = Diffy::Diff.new(string1, string2).to_s(:html)
puts html
# Output:
#
# Include CSS for styling
puts Diffy::CSS
# Output includes styles:
# .diff li.ins{background:#dfd; color:#080}
# .diff li.del{background:#fee; color:#b00}
# .diff del strong{font-weight:normal;background:#fcc;}
# .diff ins strong{font-weight:normal;background:#9f9;}
# Use colorblind-safe palette
puts Diffy::CSS_COLORBLIND_1
```
--------------------------------
### Change Split View Output Format to HTML
Source: https://github.com/samg/diffy/blob/main/README.md
Demonstrates how to change the output format of a split diff to HTML by passing the :format option. The output includes HTML divs for deleted, inserted, and unchanged lines.
```Ruby
Diffy::SplitDiff.new(string1, string2, :format => :html)
```
```Ruby
puts Diffy::SplitDiff.new(string1, string2, :format => :html).left
```
```Ruby
puts Diffy::SplitDiff.new(string1, string2, :format => :html).right
```
--------------------------------
### Include Diff Metadata in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This snippet demonstrates how to include full diff output with file paths and line number information when generating a diff using the Diffy gem in Ruby. It utilizes the `:include_diff_info` option to add header details to the diff output.
```ruby
require 'diffy'
string1 = "foo\nbar\n"
string2 = "foo\nbar\nbaz\n"
# Include diff header information
diff = Diffy::Diff.new(string1, string2, :include_diff_info => true)
puts diff.to_s(:text)
```
--------------------------------
### Set Default Diff Format in Ruby
Source: https://github.com/samg/diffy/blob/main/README.md
This snippet demonstrates how to set the default output format for Diffy in a Ruby application. It impacts all subsequent diff operations unless overridden.
```ruby
Diffy::Diff.default_format = :html
```
--------------------------------
### Iterating Diff Chunks with Diffy
Source: https://github.com/samg/diffy/blob/main/README.md
This snippet illustrates using the `.each_chunk` method provided by Diffy for iterating over groups of additions, deletions, and unchanged lines within a diff. The `.to_a` method is used to collect these chunks into an array for further processing or inspection.
```ruby
Diffy::Diff.new("foo\nbar\nbang\nbaz\n", "foo\nbar\nbing\nbong\n").each_chunk.to_a
```
--------------------------------
### Diff Two Strings (Plain Text)
Source: https://github.com/samg/diffy/blob/main/README.md
Compares two strings and outputs the differences in a plain text format. This is useful for simple console output or basic logging.
```ruby
string1 = <<-TXT
Hello how are you
I'm fine
That's great
TXT
string2 = <<-TXT
Hello how are you?
I'm fine
That's swell
TXT
puts Diffy::Diff.new(string1, string2)
```
--------------------------------
### Diff Two Strings (HTML Simple Output)
Source: https://github.com/samg/diffy/blob/main/README.md
Generates an HTML representation of the differences between two strings using a simple list format. This output is easily styleable with CSS.
```ruby
string1 = <<-TXT
Hello how are you
I'm fine
That's great
TXT
string2 = <<-TXT
Hello how are you?
I'm fine
That's swell
TXT
puts Diffy::Diff.new(string1, string2).to_s(:html_simple)
```
--------------------------------
### Context Control for Diffs in Ruby
Source: https://context7.com/samg/diffy/llms.txt
Allows control over the number of context lines displayed around changes in a diff. The context can be set via the :context option or the :diff option for more advanced Unix diff arguments.
```ruby
require 'diffy'
string1 = "foo\nfoo\nBAR\nbang\nbaz\n"
string2 = "foo\nfoo\nbar\nbang\nbaz\n"
# Show only 1 line of context around changes
diff = Diffy::Diff.new(string1, string2, :context => 1)
puts diff.to_s
# Output:
# foo
# -BAR
# +bar
# bang
# Default context is 10000 (shows entire file)
# Override with custom diff options
diff = Diffy::Diff.new(string1, string2, :diff => '-U3')
```
--------------------------------
### Override Command Line Diff Options in Diffy
Source: https://github.com/samg/diffy/blob/main/README.md
Explains how to override the default command-line options passed to the Unix diff command using the :diff option. This allows for customization of diffing behavior, such as ignoring whitespace with '-w'. Note that this option is ignored if :context is also used.
```Ruby
puts Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n", :diff => "-w")
```
--------------------------------
### Color Terminal Output for Diffs in Ruby
Source: https://context7.com/samg/diffy/llms.txt
Generates ANSI color-coded diff output suitable for terminal display. The :color format can be applied directly or set as the default format globally.
```ruby
require 'diffy'
string1 = "foo\nbar\nbaz\n"
string2 = "foo\nbang\nbaz\n"
# Output with ANSI color codes
puts Diffy::Diff.new(string1, string2).to_s(:color)
# Output:
# foo (normal)
# -bar (red)
# +bang (green)
# baz (normal)
# Set default format globally
Diffy::Diff.default_format = :color
puts Diffy::Diff.new(string1, string2).to_s
```
--------------------------------
### HTML Output with Plus/Minus Symbols in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This Ruby snippet shows how to generate HTML diff output that includes plus (+) and minus (-) symbols to indicate added and removed lines, respectively. It uses the `:include_plus_and_minus_in_html` option with the `:html_simple` format.
```ruby
require 'diffy'
string1 = "foo\nbar\n"
string2 = "foo\nbaz\n"
# Include symbols in HTML output
html = Diffy::Diff.new(string1, string2, :include_plus_and_minus_in_html => true).to_s(:html_simple)
puts html
```
--------------------------------
### Custom Diff Formatting with Enumerable in Diffy
Source: https://github.com/samg/diffy/blob/main/README.md
Diffy allows custom output formatting by providing an enumerable interface for `Diffy::Diff` objects. You can iterate over individual lines of the diff using `.each` to apply custom logic based on additions (`+`) or deletions (`-`). This enables flexible processing of diff results.
```ruby
Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n").each do |line|
case line
when /^\+/ then puts "line #{line.chomp} added"
when /^-/ then puts "line #{line.chomp} removed"
end
end
```
--------------------------------
### Simple HTML Diff Output in Ruby
Source: https://context7.com/samg/diffy/llms.txt
Generates a simpler HTML diff output without inline character highlighting, prioritizing performance. The output uses standard HTML tags for additions and deletions.
```ruby
require 'diffy'
string1 = "Hello how are you\nI'm fine\nThat's great\n"
string2 = "Hello how are you?\nI'm fine\nThat's swell\n"
# Simple HTML format
html = Diffy::Diff.new(string1, string2).to_s(:html_simple)
puts html
# Output:
#
#
# Hello how are you
# - Hello how are you?
# - I'm fine
# That's great
# - That's swell
#
#
```
--------------------------------
### Control Empty Diff Behavior in Diffy
Source: https://github.com/samg/diffy/blob/main/README.md
Details how to manage the behavior of Diffy when there are no differences between inputs. By default, it returns an empty string, but setting :allow_empty_diff => false restores the previous behavior of returning the full text of the first input.
```Ruby
Diffy::Diff.new(string1, string2, :allow_empty_diff => false)
```
--------------------------------
### Environment Variable Configuration for Diff Binary in Bash
Source: https://context7.com/samg/diffy/llms.txt
This bash snippet shows how to configure the diff binary used by the Diffy gem via environment variables. You can set the `DIFFY_DIFF` variable to specify a custom diff implementation, such as `/usr/local/bin/diff` or `ldiff`.
```bash
# Use a specific diff implementation
export DIFFY_DIFF=/usr/local/bin/diff
# Or use ldiff (from Diff::LCS)
export DIFFY_DIFF=ldiff
```
--------------------------------
### Basic String Diffing in Ruby
Source: https://context7.com/samg/diffy/llms.txt
Generates a text-based diff between two strings. It requires the 'diffy' gem and outputs lines removed with '-' and lines added with '+'.
```ruby
require 'diffy'
string1 = <<-TXT
Hello how are you
I'm fine
That's great
TXT
string2 = <<-TXT
Hello how are you?
I'm fine
That's swell
TXT
# Create and display diff in default text format
diff = Diffy::Diff.new(string1, string2)
puts diff.to_s
# Output:
# -Hello how are you
# +Hello how are you?
# I'm fine
# -That's great
# +That's swell
```
--------------------------------
### Side-by-Side Split Diffs in Ruby
Source: https://context7.com/samg/diffy/llms.txt
Creates side-by-side diff views, separating deletions (left) and insertions (right). Supports both text and HTML formats for the split view.
```ruby
require 'diffy'
string1 = "Hello how are you\nI'm fine\nThat's great\n"
string2 = "Hello how are you?\nI'm fine\nThat's swell\n"
# Create split diff
split = Diffy::SplitDiff.new(string1, string2)
# Left side shows deletions and unchanged lines
puts split.left
# Output:
# -Hello how are you
# I'm fine
# -That's great
# Right side shows insertions and unchanged lines
puts split.right
# Output:
# +Hello how are you?
# I'm fine
# +That's swell
# HTML format split view
split_html = Diffy::SplitDiff.new(string1, string2, :format => :html)
puts split_html.left # HTML with only deletions
puts split_html.right # HTML with only insertions
```
--------------------------------
### Controlling Empty Diff Behavior in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This Ruby code explains how to manage the output when two strings being compared are identical. By default, Diffy returns an empty string. Setting `:allow_empty_diff => false` forces Diffy to output the content of the strings even when they are identical.
```ruby
require 'diffy'
string1 = "foo\nbar\n"
string2 = "foo\nbar\n"
# Default: return empty string for identical inputs
diff = Diffy::Diff.new(string1, string2)
puts diff.to_s # => ""
# Show full text even when identical
diff = Diffy::Diff.new(string1, string2, :allow_empty_diff => false)
puts diff.to_s
```
--------------------------------
### Side-by-Side Diff (Right Side - Insertions)
Source: https://github.com/samg/diffy/blob/main/README.md
Generates the right side of a side-by-side diff, showing only the lines that were added to the new string. This complements the left side output from `Diffy::SplitDiff`.
```ruby
string1 = "Hello how are you\nI'm fine\nThat's great\n"
string2 = "Hello how are you?\nI'm fine\nThat's swell\n"
puts Diffy::SplitDiff.new(string1, string2).right
```
--------------------------------
### Side-by-Side Diff (Left Side - Deletions)
Source: https://github.com/samg/diffy/blob/main/README.md
Generates the left side of a side-by-side diff, showing only the lines that were deleted from the original string. This is part of the `Diffy::SplitDiff` functionality.
```ruby
string1 = "Hello how are you\nI'm fine\nThat's great\n"
string2 = "Hello how are you?\nI'm fine\nThat's swell\n"
puts Diffy::SplitDiff.new(string1, string2).left
```
--------------------------------
### Ignoring CRLF Differences in HTML Output in Ruby
Source: https://context7.com/samg/diffy/llms.txt
This Ruby snippet illustrates how to ignore differences caused by Windows (CRLF) versus Unix (LF) line endings when generating an HTML diff. By setting `:ignore_crlf => true`, Diffy treats strings with different line endings as identical if their content is otherwise the same.
```ruby
require 'diffy'
unix_string = "foo\nbar\n"
windows_string = "foo\r\nbar\r\n"
# Ignore CRLF differences
diff = Diffy::Diff.new(unix_string, windows_string, :ignore_crlf => true)
puts diff.to_s(:html)
```
--------------------------------
### Ignore CRLF in HTML Diffs with Diffy
Source: https://github.com/samg/diffy/blob/main/README.md
This snippet demonstrates how to ignore carriage return and line feed characters (CRLF) when generating HTML output for diffs. By passing a truthy value to the `:ignore_crlf` option, Diffy will normalize line endings for comparison, simplifying HTML output.
```ruby
puts Diffy::Diff.new(" foo\nbar\n", "foo\r\nbar\r\n", ignore_crlf: true).to_s(:html)
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.