rtrim and ltrim function for ruby: is this ok?

L

LeonB

I created this function:

class String
def rtrim(character)

if self[-1, 1] == character.to_s
self[0, self.length - 1]
end
return self
end

def ltrim(character)
if self[0, 1] == character.to_s
self[1, self.length]
end
return self
end
end

Is this ok? Am I not duplicating any built-in ruby functions? Does
anyone see any possible problems with these functions?

Thanks in advance!
 
T

Todd Benson

I created this function:

class String
def rtrim(character)

if self[-1, 1] == character.to_s
self[0, self.length - 1]
end
return self
end

def ltrim(character)
if self[0, 1] == character.to_s
self[1, self.length]
end
return self
end
end

I'm not sure this does what you want it to. The object referenced by
self has not changed because the [] method does not alter the String.

Todd
 
N

Nobuyoshi Nakada

Hi,

At Wed, 25 Jul 2007 07:55:04 +0900,
LeonB wrote in [ruby-talk:261630]:
Am I not duplicating any built-in ruby functions?

Perhaps. See String#rstrip and #lstrip.
 
T

Trans

I created this function:

class String
def rtrim(character)

if self[-1, 1] == character.to_s
self[0, self.length - 1]
end
return self
end

def ltrim(character)
if self[0, 1] == character.to_s
self[1, self.length]
end
return self
end
end

Is this ok? Am I not duplicating any built-in ruby functions? Does
anyone see any possible problems with these functions?

Thanks in advance!

#chomp!

but there is no reverse or left chomp --wish there were! And it does
not support regexp. Maybe worth an RCR.

T.
 
R

Robert Dober

but there is no reverse or left chomp --wish there were! And it does
not support regexp. Maybe worth an RCR.

Well there is, it is just sooo expensive ;)
James knows what I mean.

cheers
Robert
 
L

Leon Bogaert

Trans said:
#chomp!

but there is no reverse or left chomp --wish there were! And it does
not support regexp. Maybe worth an RCR.

T.

Thanks! To bad for the missing left-chomp
 

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,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top