Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created August 18, 2012 02:57
Show Gist options
  • Save jcuffe/3384070 to your computer and use it in GitHub Desktop.
Save jcuffe/3384070 to your computer and use it in GitHub Desktop.
require 'test/unit'
def find substr, str
str.split('').each_with_index do |char, index|
if substr.chr == char
if substr.length == 1 or find(substr[1..-1], str[index+1..-1]) == 0
return index
end
end
end
return nil
end
class FindTest < Test::Unit::TestCase
# should return index of first letter in matched string
def test_found
assert_equal 1, find('bcd', 'abcdefg')
end
# should return nil if the substring is not matched
def test_not_found
assert_equal nil, find('x', 'abcdefg')
end
# should accurately match from the beginning after a partial match
def test_consumes_partial_matches
assert_equal 5, find('bcd', 'abcbabcd')
end
# should return nil when attempting to match for or against the empty string
def test_empty_string
assert_equal nil, find('', 'abcdabe')
assert_equal nil, find('abc', '')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment