On Mar 27, 2007, at 8:13 PM, Timothy H. wrote:
else delete first char of string1
I don’t really know about this one yet… I haven’t gotten to it.
Can anyone pls suggest what I might do?
Thanx
Daniel
Take a look at the MatchData class, particularly the #begin and
#offset methods.
What you might do is take a TDD approach (Test Driven Development)…
Change the method body until the tests pass and you’re all set! Of
course, seeing the tests might help you solidify what you expect the
behavior to be.
class String
intersection of self and other
def &(other)
‘’
end
end
if FILE == $0
require ‘test/unit’
class StringIntersectionTest < Test::Unit::TestCase
def setup
@empty = ‘’
end
def test_empty_string
assert_equal @empty, @empty & @empty, "empty with empty"
assert_equal @empty, 'not' & @empty, "non-empty with empty"
assert_equal @empty, @empty & 'not', "empty with non-empty"
end
def test_equal_strings
s = "Some long string that I'm ready to take to the intersection"
assert_equal s, s & s, "string with self"
end
def test_prefix
assert_equal 'f', 'f' & 'fr', 'single char string as prefix of
other’
assert_equal ‘f’, ‘fr’ & ‘f’, ‘other is single char prefix of
string’
assert_equal ‘fred’, ‘fred’ & ‘fredrick’, ‘multi-char string
as suffix of other’
assert_equal ‘fred’, ‘fredrick’ & ‘fred’, ‘other is multi-char
suffix of string’
end
def test_suffix
assert_equal 'r', 'r' & 'fr', 'single char string as suffix of
other’
assert_equal ‘r’, ‘fr’ & ‘r’, ‘other is single char suffix of
string’
assert_equal ‘rick’, ‘rick’ & ‘fredrick’, ‘multi-char string
as suffix of other’
assert_equal ‘rick’, ‘fredrick’ & ‘rick’, ‘other is multi-char
suffix of string’
end
def test_other_intersections
assert_equal 'site', 'new web site' & 'site-wide policy'
end
def test_other_empty_intersections
assert_equal @empty, 'fred' & 'ethel'
assert_equal @empty, 'The quick brown fox jumps over the lazy
dog.’ & ‘The lazy dog lies under the jumping fox.’
assert_equal @empty, ‘Blah, Blah, blah’ & ‘yada, yada, yada’
end
end
end
Note that two of the six tests already pass just by returning the
empty string
-Rob
Rob B. http://agileconsultingllc.com
[email protected]