[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 MacQuigg <dmq () gain ! com>
Date: 2004-02-24 18:54:50
Message-ID: e37n30duqj7epr5mpjjvm86elke4241nmj () 4ax ! com
[Download RAW message or body]
On Tue, 24 Feb 2004 06:59:10 +0900, "Jim Weirich"
<jim@weirichhouse.org> 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}
Yes. That works if I change the 'it' to 't'. Looks like the
'collect' method is equivalent to Python's 'map' function. This
statement is still not as simple as I would like, but it definitely
beats the equivalent Python.
This is the first example I've seen where the difference between Ruby
and Python is more than just personal preference. Thanks.
-- Dave
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic