# Rtrim and ltrim function for ruby: is this ok?

**URL:** <https://www.ruby-forum.com/t/rtrim-and-ltrim-function-for-ruby-is-this-ok/110299>\
**Category:** Ruby\
**Created:** [July 25, 2007, 12:56am UTC](https://www.ruby-forum.com/t/rtrim-and-ltrim-function-for-ruby-is-this-ok/110299 "2007-07-25T00:56:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 25, 2007, 12:56am UTC](https://www.ruby-forum.com/t/rtrim-and-ltrim-function-for-ruby-is-this-ok/110299/1 "2007-07-25T00:56:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 25, 2007, 2:50am UTC](https://www.ruby-forum.com/t/rtrim-and-ltrim-function-for-ruby-is-this-ok/110299/2 "2007-07-25T02:50:39Z")

</div>

On 7/24/07, LeonB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 25, 2007, 3:20am UTC](https://www.ruby-forum.com/t/rtrim-and-ltrim-function-for-ruby-is-this-ok/110299/3 "2007-07-25T03:20:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 26, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/rtrim-and-ltrim-function-for-ruby-is-this-ok/110299/4 "2007-07-26T21:33:20Z")

</div>

On 7/26/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 30, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/rtrim-and-ltrim-function-for-ruby-is-this-ok/110299/5 "2007-07-30T22:01:49Z")

</div>

Trans wrote:

> On Jul 24, 3:55 pm, LeonB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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.

Thanks! To bad for the missing left-chomp

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 26, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/rtrim-and-ltrim-function-for-ruby-is-this-ok/110299/6 "2007-07-26T21:24:20Z")

</div>

On Jul 24, 3:55 pm, LeonB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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