Skip to content

Instantly share code, notes, and snippets.

@ernie
Created September 6, 2013 19:20
Show Gist options
  • Save ernie/6468635 to your computer and use it in GitHub Desktop.
Save ernie/6468635 to your computer and use it in GitHub Desktop.
So, I was in the process of writing this code today, and went on a wild goose chase due to this weird error message. I'd expected to see the nil come back from String#index, and give a different error, but no NilClass to be found in this error. String isn't even a type that String#index can return. It should only return a Fixnum or nil. Instead:…
#!/usr/bin/env ruby
require 'minitest/autorun'
class Reorder
SINGLE_CHAR = /./
def initialize(word, order)
@word, @order = word, order
end
def order
@word.scan(SINGLE_CHAR).sort { |a, b| @order.index(a) <=> @order.index(b) }.
join
end
end
describe Reorder do
it 'order banana with abc' do
Reorder.new('banana', 'abc').order.must_equal 'aaabnn'
end
end
# 1) Error:
# Reorder#test_0001_order banana with abc:
# ArgumentError: comparison of String with String failed
# ./reorder.rb:13:in `sort'
# ./reorder.rb:13:in `order'
# ./reorder.rb:20:in `block (2 levels) in <main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment