Skip to content
Inspect the changes of your `String#sub` and `String#gsub` replacements
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.travis.yml
Gemfile
Gemfile.lock
LICENSE
README.md
sub_diff.gemspec

README.md

Sean Huber sub_diff

Build Status Code Climate Coverage Gem Version

Inspect the changes of your String#sub and String#gsub replacements.

Installation

gem install sub_diff

Requirements

Ruby 2.0+

For older Ruby versions (1.8+), use the 1.0.7 tag.

Usage

This gem introduces a couple new methods to String objects.

These methods accept the same arguments as their sub and gsub counterparts.

replaced = 'this is a test'.gsub_diff(/(\S*is)/, 'replaced(\1)') #=> #<SubDiff::Collection:0x007fc532049508>

The difference is that it returns a SubDiff::Collection instead. This object behaves like a String.

puts replaced #=> "replaced(this) replaced(is) a test"

But it also allows us to check if the replacement actually changed anything.

replaced.changed? #=> true

For a closer look at the changes, we can iterate thru each Diff in the replacment.

replaced.each do |diff|
  puts diff.inspect
end

#=> "replaced(this)"
#=> " "
#=> "replaced(is)"
#=> " a test"

Each Diff object behaves just like a string, but also includes a few additional methods.

replaced.each do |diff|
  puts "    value: #{diff.value.inspect}"
  puts "value_was: #{diff.value_was.inspect}"
  puts " changed?: #{diff.changed?}"
end

#=>     value: "replaced(this)"
#=> value_was: "this"
#=>  changed?: true

#=>     value: " "
#=> value_was: " "
#=>  changed?: false

#=>     value: "replaced(is)"
#=> value_was: "is"
#=>  changed?: true

#=>     value: " a test"
#=> value_was: " a test"
#=>  changed?: false

API

YARD Documentation

  • String#sub_diff
  • String#gsub_diff
  • SubDiff::Diff#changed?
  • SubDiff::Diff#value
  • SubDiff::Diff#value_was
  • SubDiff::Collection#changed?
  • SubDiff::Collection#clear
  • SubDiff::Collection#diffs
  • SubDiff::Collection#each
  • SubDiff::Collection#reset
  • SubDiff::Collection#size

Testing

bundle exec rspec

Contributing

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with the version or history.
  • Send me a pull request. Bonus points for topic branches.

License

MIT - Copyright © 2011-2015 Sean Huber

Something went wrong with that request. Please try again.