thousand ways to rome

C

Chris Mueller

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 = words_array.strip
end

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

thx,

chris
 
B

Bruno Michel

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 = words_array.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"]
 
C

Chris Mueller

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!
 
S

spooq

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

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

seperated_words =3D "hi, you, guys"
words_array =3D seperated_words.split(",")
for i in 0 ... words_array.length
words_array =3D words_array.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 }
=3D> ["hi", "you", "guys"]
 
S

spooq

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

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
 
K

Kero

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 = words_array.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(/, /)
 
D

dblack

Hi --

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 = words_array.strip
end


Here's one way:

separated_words.scan(/[^\s,]+/)

And another:

require 'csv'
CSV.parse_line(separated.words.delete(' '))


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
R

Robert Klemme

Kero said:
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 = words_array.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
 
J

John Carter

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

My favourite cheat is....

$ irb
irb(main):001:0> %w{hi you guys}
=> ["hi", "you", "guys"]
irb(main):002:0>



Not quite as much as a cheat as you think....

People keep creating small ugly special purpose once off data
languages like xml, csv, yaml,...

Unless there is a pressing need to store in language neutral format,
for pity sake don't.

Just store your data as plain old ruby.

Simpler and more expressive.

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
 
C

CParticle

Chris said:
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 = words_array.strip
end

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


Chris,
Assuming that the string convention stays a comma plus a single space
between words why not add the single space in to the split string and
write

seperated_words = "hi, you, guys"
word_array = seperated_words.split(", ")

CParticle
 
C

Chris Mueller

Charged said:
Assuming that the string convention stays a comma plus a single space
between words why not add the single space in to the split string and
write

i forgot to mention that the comma seperated list comes from a text_area
and may not include a whitespace after each ","

thank you all for your posts, i learned quite a lot about the many roads
to rome that ruby (and regex) provides

p.s: maybe i should start another competiton ;)
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top