From: Michal Suchanek Date: 2007-05-08T21:50:54+09:00 Subject: Re: general purpose chomp? On 08/05/07, Robert Klemme wrote: > On 08.05.2007 14:11, Michal Suchanek wrote: > > 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 > > 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" > 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