Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > general purpose chomp?

Reply
Thread Tools

general purpose chomp?

 
 
Michal Suchanek
Guest
Posts: n/a
 
      05-08-2007
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      05-08-2007
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"

Regards

robert
 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      05-08-2007
On 5/8/07, Michal Suchanek <(E-Mail Removed)> 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
>
> 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


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

 
Reply With Quote
 
Michal Suchanek
Guest
Posts: n/a
 
      05-08-2007
On 08/05/07, Robert Klemme <(E-Mail Removed)> 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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      05-08-2007
On Tue, May 08, 2007 at 09:33:43PM +0900, Robert Dober wrote:
> On 5/8/07, Michal Suchanek <(E-Mail Removed)> 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
> >
> >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 /^.../

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
recommended general-purpose string template packages? John Machin Python 6 08-16-2006 01:35 PM
NEWB: General purpose list iteration? Donald Newcomb Python 4 08-12-2005 10:33 PM
Storing general-purpose and specific entries Robert Bj?rn XML 0 06-01-2004 11:36 AM
Python is the best and most popular general purpose scripting language; the universal scripting language Ron Stephens Python 23 04-12-2004 05:32 PM
Looking for very simple general purpose tokenizer Maarten van Reeuwijk Python 6 01-21-2004 04:37 AM



Advertisments