[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: Need examples comparing Ruby to Python
From: "David A. Black" <dblack () wobblini ! net>
Date: 2004-02-23 22:06:47
Message-ID: Pine.LNX.4.44.0402231403000.11586-100000 () wobblini
[Download RAW message or body]
Hi --
On Tue, 24 Feb 2004, Jim Weirich wrote:
>
> David MacQuigg said:
> > Here is what I would like to say in Python, but can't because
> > line.split() produces a list, and the subsequent methods don't apply
> > to lists. I'll bet this would work in Ruby, but I don't know enough
> > Ruby to say for sure.
> >
> > file, length, name, title = line.split('|').strip.split.join()
> >
> > The goal is to produce a list of strings with whitespace and newlines
> > stripped off the ends, and whitespace squeezed out of the middle of
> > each string.
>
> Is this what you want ...
>
> file, length, name, title = line.split('|').collect{|it| t.squeeze.strip}
^^
s/it/t/ :-)
(I wonder whether it would be faster/slower/same to do:
f,l,n,t = line.squeeze.split('|').collect{|t| t.strip}
but I'm feeling too lazy to haul out benchmark.rb right now :-)
David
--
David A. Black
dblack@wobblini.net
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic