Created
September 6, 2013 19:20
-
-
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:…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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