Forum: Ruby rtrim and ltrim function for ruby: is this ok?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 Leon Bogaert (leonb)
on 2007-07-25 21:31
(Received via mailing list)
I made these two functions to extend the default String object:

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

Are they ok? I mean, aren't there any default Ruby functions I am
copying? I looked on google for functions that did the same as above
functions, but I couldn't find any for Ruby.

Thanks in advance!
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 Leon Bogaert (leonb)
on 2007-07-25 21:42
(Received via mailing list)
Pfff... Sorry for the second post. I couldn't find the first one on
Google groups.
I tried changing the self object. But I wasn't allowed to do it. I got
an error message when I did that:
"Can't change the value of self"

So I changed it to:

class String
  def rtrim(character)

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

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

This works. But I would rather see the first object changed. Is this
possible?
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 Leon Bogaert (leonb)
on 2007-07-25 21:46
(Received via mailing list)
Ow yeah. rstrip and lstrip only strip spaces (and maybe line-endings).
I also want to use it for other characters.
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-07-25 21:54
(Received via mailing list)
On 7/25/07, LeonB <leon@tim-online.nl> wrote:
>
>                 end
>                 return ret
>         end
> end
>
> This works. But I would rather see the first object changed. Is this
> possible?

Leon, look at String#slice!

Todd
F62cfaca4a7a5bf58e08a664c2f41b94?d=identicon&s=25 Ian Whitlock (ian)
on 2007-07-26 01:39
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
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2007-07-26 04:01
(Received via mailing list)
On 7/26/07, Ian Whitlock <iw1junk@comcast.net> wrote:
> 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.

Since we got the power of regex/gsub, let's use it :)

class String
  def rtrim(char)
    dump.rtrim!(char)
  end

  def rtrim!(char)
    gsub!(/#{Regexp.escape(char)}+$/, '')
  end

  def ltrim(char)
    dump.ltrim!(char)
  end

  def ltrim!(char)
    gsub!(/^#{Regexp.escape(char)}+/, '')
  end
end

s = "aaaaaabacbccccc"
p s
s.rtrim!("c")
p s
s.ltrim!("b")
p s
s.ltrim!("a")
p s
F62cfaca4a7a5bf58e08a664c2f41b94?d=identicon&s=25 Ian Whitlock (ian)
on 2007-07-26 18:26
Michael Fellinger wrote:

> Since we got the power of regex/gsub, let's use it :)

Michael,

Thanks.  I agree that in practice your code is better.
I am a newbie who does not yet feel comfortable with
regular expressions, but I see what you have done.

Ian
This topic is locked and can not be replied to.