general purpose chomp?

  • Thread starter Michal Suchanek
  • Start date
M

Michal Suchanek

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
 
R

Robert Klemme

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"

Regards

robert
 
R

Robert Dober

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
Some random thoughts
* Chomp strips on the RHS of the string, right?
* Sometimes I would just use sub /^<clever rgx>/, "" (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
 
M

Michal Suchanek

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
 
B

Brian Candler

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
Some random thoughts
* Chomp strips on the RHS of the string, right?
* Sometimes I would just use sub /^<clever rgx>/, ""

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top