Recently I added diff_as_hash to rspec and I’d like to show you what I did, so you can add your own differs, and it’s only a couple of lines of code. You can edit your own local gem copy of rspec using the find_gem gem’s edit_gem command.

Let’s use the example that you want to add a BigDecimal differ.

lib/spec/expectations/fail_with.rb

First of all you’re going to need to add a line to this file, something that looks like the lines here, along with the other elsifs:

lib/spec/runner/differs/default.rb

Secondly, in this file inside the Spec::Expectations::Differs::Default class you’ll have to define your diff_as_big_decimal differ:

Now when you diff BigDecimal numbers in your rspec tests, you’ll see this output, rather than the ugly output.

However, what would be nicer is if you could expand on this diffing syntax in your own project, rather than having to hack rspec and have it wiped next time you upgrade. More on that later though.