sub_diff
Inspect the changes of your String#sub and String#gsub replacements.
Installation
gem install sub_diff
Requirements
Ruby 1.8.7+
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)') #=> #<DiffCollection:0x007fc532049508>
The difference is that it returns a DiffCollection instead. This object behaves just 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
Diff#changed?Diff#valueDiff#value_wasDiffCollection#changed?DiffCollection#diffsDiffCollection#eachDiffCollection#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 Sean Huber