# General purpose chomp?

**URL:** <https://www.ruby-forum.com/t/general-purpose-chomp/103615>\
**Category:** Ruby\
**Created:** [May 8, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/general-purpose-chomp/103615 "2007-05-08T14:12:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 8, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/general-purpose-chomp/103615/1 "2007-05-08T14:12:15Z")

</div>

I wonder why we do not have one. Or maybe I just overlooked something?

class String  
def lstripc c = ’ \r\t\n’  
c = c.chr if c.kind\_of? Integer  
idx = 0  
while c.include? self[idx]  
idx += 1  
end  
self[idx…-1]  
end  
end

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 8, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/general-purpose-chomp/103615/2 "2007-05-08T14:36:20Z")

</div>

On 08.05.2007 14:11, Michal S. wrote:

> end  
> end

irb(main):016:0\> s = “\r\n\tfoo”  
=\> “\r\n\tfoo”  
irb(main):017:0\> s.lstrip!  
=\> “foo”  
irb(main):018:0\> s  
=\> “foo”

If you want more control, the “general purpose” methods would be sub,  
sub!, gsub and gsub!:

irb(main):001:0\> s = “\r\n\tfoo”  
=\> “\r\n\tfoo”  
irb(main):002:0\> s.sub! %r{\A[\r\n\s]+}, ‘’  
=\> “foo”  
irb(main):003:0\> s  
=\> “foo”

irb(main):009:0\> s = “\r\n\tfoo”  
=\> “\r\n\tfoo”  
irb(main):010:0\> s.sub! %r{\A\s+}, ‘’  
=\> “foo”  
irb(main):011:0\> s  
=\> “foo”

Regards

robert

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 8, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/general-purpose-chomp/103615/3 "2007-05-08T14:34:20Z")

</div>

On 5/8/07, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end
> 
> Thanks
> 
> Michal

Some random thoughts

- Chomp strips on the RHS of the string, right?
- Sometimes I would just use sub /^/, “” (or a potential  
#delete as suggested in my RCR idea 😉
- I feel rgxs should be accepted as params
- I do not like the use a string with character set semantics (again  
kindly have a look at the RCR thread where somebody smarter than me  
has made a very good point about this).

That all said, this might actually be a nice feature to have, but  
maybe there are just to many methods there, already doing _almost_ the  
same.  
This is basically one of the reasons against my RCR idea too.

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 8, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/general-purpose-chomp/103615/4 "2007-05-08T15:48:36Z")

</div>

On Tue, May 08, 2007 at 09:33:43PM +0900, Robert D. wrote:

> > self[idx…-1]
> 
> - Sometimes I would just use sub /^/, “”  
> Except that would be wrong in this case, since it wouldn’t only chomp  
> from  
> the left-hand side of the string.

Use /\A…/ not /^…/

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 8, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/general-purpose-chomp/103615/5 "2007-05-08T14:51:23Z")

</div>

On 08/05/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > self[idx…-1]  
> > If you want more control, the “general purpose” methods would be sub,  
> > =\> “\r\n\tfoo”  
> > irb(main):010:0\> s.sub! %r{\A\s+}, ‘’  
> > =\> “foo”  
> > irb(main):011:0\> s  
> > =\> “foo”

Yes, sub! is the thing that would do chomp for things other than \r\n\t.  
Not as easy but it surely works.

Thanks

Michal
