Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Inspect the changes of your `String#sub` and `String#gsub` replacements
branch: master

Require gsub instead of sub

gsub already requires sub
latest commit 225627200d
Sean Huber authored

README.rdoc

sub_diff -

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

Installation

gem install sub_diff

Usage

This gem introduces two new methods to String, sub_diff and gsub_diff, which accept the same arguments as their sub and gsub counterparts.

replaced = 'this is a test'.gsub_diff(/(\S*is)/, 'replaced(\1)')

The replaced object behaves just like a String.

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

It also allows you to check if the replacement actually changed anything.

replaced.changed? #=> true

For a closer look at the changes, simply enumerate thru the returned replacements. Each iteration yields a Diff object which also behaves like a String but includes a few additional methods: value, value_was, and changed?.

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

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 Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Something went wrong with that request. Please try again.