[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

thousand ways to rome

Chris Mueller

11/9/2006 11:49:00 AM

Hi,
i am new to ruby and i am wondering how many more ways (and what ways)
there are to do this:

seperated_words = "hi, you, guys"
words_array = seperated_words.split(",")
for i in 0 ... words_array.length
words_array[i] = words_array[i].strip
end

i suppose there's a one-liner that you write before you can think "ruby"
doesn't it ???

thx,

chris


--
Posted via http://www.ruby-....

12 Answers

Luke Graham

11/9/2006 11:59:00 AM

0

"hi, you, guys".split(",").map! { |x| x.strip }

On 11/9/06, Chris Mueller <damngoodcoffee@gmail.com> wrote:
> Hi,
> i am new to ruby and i am wondering how many more ways (and what ways)
> there are to do this:
>
> seperated_words = "hi, you, guys"
> words_array = seperated_words.split(",")
> for i in 0 ... words_array.length
> words_array[i] = words_array[i].strip
> end
>
> i suppose there's a one-liner that you write before you can think "ruby"
> doesn't it ???
>
> thx,
>
> chris
>
>
> --
> Posted via http://www.ruby-....
>
>

Bruno Michel

11/9/2006 12:00:00 PM

0

Chris Mueller a écrit :
> Hi,
> i am new to ruby and i am wondering how many more ways (and what ways)
> there are to do this:
>
> seperated_words = "hi, you, guys"
> words_array = seperated_words.split(",")
> for i in 0 ... words_array.length
> words_array[i] = words_array[i].strip
> end
>
> i suppose there's a one-liner that you write before you can think "ruby"
> doesn't it ???
>
> thx,
>
> chris

Hi,

this is my version :

"hi, you, guys".split(',').map { |word| word.strip }
=> ["hi", "you", "guys"]

--
Bruno Michel

Chris Mueller

11/9/2006 12:21:00 PM

0

good timing to you first two guys;)

thx very much. got some new ruby cells working in my brain.

i tried same thing as ".map" with "array.each" before, but this does
only apply on a copy i suppose...

and of course the regexp way is ... groovy!

--
Posted via http://www.ruby-....

Luke Graham

11/9/2006 12:22:00 PM

0

Your version has no side-effect on the array, so it's not exactly the
same as the original ... :)

On 11/9/06, Bruno Michel <bruno@exceliance.fr> wrote:
> Chris Mueller a écrit :
> > Hi,
> > i am new to ruby and i am wondering how many more ways (and what ways)
> > there are to do this:
> >
> > seperated_words = "hi, you, guys"
> > words_array = seperated_words.split(",")
> > for i in 0 ... words_array.length
> > words_array[i] = words_array[i].strip
> > end
> >
> > i suppose there's a one-liner that you write before you can think "ruby"
> > doesn't it ???
> >
> > thx,
> >
> > chris
>
> Hi,
>
> this is my version :
>
> "hi, you, guys".split(',').map { |word| word.strip }
> => ["hi", "you", "guys"]
>
> --
> Bruno Michel
>
>

Luke Graham

11/9/2006 12:28:00 PM

0

use map! instead

On 11/9/06, Chris Mueller <damngoodcoffee@gmail.com> wrote:
> good timing to you first two guys;)
>
> thx very much. got some new ruby cells working in my brain.
>
> i tried same thing as ".map" with "array.each" before, but this does
> only apply on a copy i suppose...
>
> and of course the regexp way is ... groovy!
>
> --
> Posted via http://www.ruby-....
>
>

Luke Graham

11/9/2006 1:15:00 PM

0

you are absolutely correct, my mind is rotting from weeks of sitting
at my desk twiddling my thumbs with no work to do.

On 11/9/06, Robert Dober <robert.dober@gmail.com> wrote:
> On 11/9/06, spooq <spoooq@gmail.com> wrote:
> >
> > use map! instead
>
>
> nope,
> try to understand the difference between split(regexp), map and map!
> than decide for yourself
> Look at this for example
>
> "he, nice, guys".split(',').map!{|x| x.strip}
> why would you use map! ?
> try to put the above expression into a context
> e.g.
> x = ...
>
> Hint: using map! on unreferenced objects is quite useless.
>
> What about
> x= "he, nice".split(",")
> x.map!{|x|x.strip}
>
> try
> x.map!{|x|x.strip!}
> would you like to use this?
>
> If you want to walk do not learn to drive ;)
>
> Cheers
> Robert
>
> On 11/9/06, Chris Mueller <damngoodcoffee@gmail.com> wrote:
> > > good timing to you first two guys;)
> > >
> > > thx very much. got some new ruby cells working in my brain.
> > >
> > > i tried same thing as ".map" with "array.each" before, but this does
> > > only apply on a copy i suppose...
> > >
> > > and of course the regexp way is ... groovy!
> > >
> > > --
> > > Posted via http://www.ruby-....
> > >
> > >
> >
> >
>
>
> --
> The reasonable man adapts himself to the world; the unreasonable one
> persists in trying to adapt the world to himself. Therefore all progress
> depends on the unreasonable man.
>
> - George Bernard Shaw
>
>

Kero van Gelder

11/9/2006 6:04:00 PM

0

On 2006-11-09, Chris Mueller <damngoodcoffee@gmail.com> wrote:
> Hi,
> i am new to ruby and i am wondering how many more ways (and what ways)
> there are to do this:
>
> seperated_words = "hi, you, guys"
> words_array = seperated_words.split(",")
> for i in 0 ... words_array.length
> words_array[i] = words_array[i].strip
> end
>
> i suppose there's a one-liner that you write before you can think "ruby"
> doesn't it ???

Functionally slightly different, but:

words_array = separated_words.split(/, /)

dblack

11/9/2006 6:39:00 PM

0

Robert Klemme

11/9/2006 7:06:00 PM

0

Kero <kero@chello.single-dot.nl> wrote:
> On 2006-11-09, Chris Mueller <damngoodcoffee@gmail.com> wrote:
>> Hi,
>> i am new to ruby and i am wondering how many more ways (and what
>> ways) there are to do this:
>>
>> seperated_words = "hi, you, guys"
>> words_array = seperated_words.split(",")
>> for i in 0 ... words_array.length
>> words_array[i] = words_array[i].strip
>> end
>>
>> i suppose there's a one-liner that you write before you can think
>> "ruby" doesn't it ???
>
> Functionally slightly different, but:
>
> words_array = separated_words.split(/, /)

And yet another one

irb(main):002:0> "hi, you, guys".split /\s*,\s*/
=> ["hi", "you", "guys"]
irb(main):003:0> "hi, you, guys".scan /\w+/
=> ["hi", "you", "guys"]

Cheers

robert

John Carter

11/10/2006 3:43:00 AM

0