String.strip( string_to_strip )

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--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,364
Latest member
Stevanida

Latest Threads

Top