From: Ian Whitlock Date: 2007-07-26T08:39:55+09:00 Subject: Re: rtrim and ltrim function for ruby: is this ok? Leon Bogaert wrote: > This works. But I would rather see the first object changed. Is this > possible? I would argue that ltrim and rtrim should not remove one character, but a sequence of the given character. The following code probably does not meet all ruby conventions, but it does what you request and it does reserve ! for its intended purpose. class String def rtrim!(character) n = 0 while (self[-(n+1), 1] == character.to_s) n +=1 end self.slice!(self.length - n,n) if n > 0 end def ltrim!(character) n = 0 while (self[n, 1] == character.to_s) n += 1 end self.slice!(0 , n) if n > 0 end end s = "aaaaaabacbccccc" p s s.rtrim!("c") p s s.ltrim!("b") p s s.ltrim!("a") p s Ian -- Posted via http://www.ruby-forum.com/.