A
Albert Chou
------_=_NextPart_001_01C3C684.8AE1E58E
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I've wanted a String.strip( string_to_strip ) method to augment the
existing strip() method for a long time, and today I TDD'ed myself one.
Anyone else have a use for such a thing?
Al
# cat StringTest.rb
require 'test/unit'
require 'stringadditions'
class StringTest < Test::Unit::TestCase
def test_String_strip
# Make sure original behavior of strip() is unchanged.
s =3D ' string not surrounded by two spaces '
assert_equal( 'string not surrounded by two spaces', s.strip )
end
def test_String_strip_quotes
s =3D '"string without quotes"'
assert_equal( 'string without quotes', s.strip( '"' ) )
end
def test_String_strip_2quotes
s =3D '""string without 2 quotes""'
assert_equal( 'string without 2 quotes', s.strip( '"' ) )
end
end
# cat stringadditions.rb
class String
alias original_strip strip
def strip( string_to_strip =3D nil )
unless string_to_strip
return original_strip
else
i =3D index( string_to_strip )
while i do
if i =3D=3D 0 then
self[0..(string_to_strip.length - 1)] =3D ''
i =3D index( string_to_strip )
else
break
end
end
i =3D rindex( string_to_strip )
while i do
if self[i..-1] =3D=3D string_to_strip then
self[i..-1] =3D ''
i =3D rindex( string_to_strip )
else
break
end
end
end
return self
end
end
------_=_NextPart_001_01C3C684.8AE1E58E--
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I've wanted a String.strip( string_to_strip ) method to augment the
existing strip() method for a long time, and today I TDD'ed myself one.
Anyone else have a use for such a thing?
Al
# cat StringTest.rb
require 'test/unit'
require 'stringadditions'
class StringTest < Test::Unit::TestCase
def test_String_strip
# Make sure original behavior of strip() is unchanged.
s =3D ' string not surrounded by two spaces '
assert_equal( 'string not surrounded by two spaces', s.strip )
end
def test_String_strip_quotes
s =3D '"string without quotes"'
assert_equal( 'string without quotes', s.strip( '"' ) )
end
def test_String_strip_2quotes
s =3D '""string without 2 quotes""'
assert_equal( 'string without 2 quotes', s.strip( '"' ) )
end
end
# cat stringadditions.rb
class String
alias original_strip strip
def strip( string_to_strip =3D nil )
unless string_to_strip
return original_strip
else
i =3D index( string_to_strip )
while i do
if i =3D=3D 0 then
self[0..(string_to_strip.length - 1)] =3D ''
i =3D index( string_to_strip )
else
break
end
end
i =3D rindex( string_to_strip )
while i do
if self[i..-1] =3D=3D string_to_strip then
self[i..-1] =3D ''
i =3D rindex( string_to_strip )
else
break
end
end
end
return self
end
end
------_=_NextPart_001_01C3C684.8AE1E58E--