Thousand ways to rome

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-forum.com/.

"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-forum.com/\.

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

Functionally slightly different, but:

words_array = separated_words.split(/, /)

···

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 ???

Hi --

···

On Thu, 9 Nov 2006, Chris Mueller 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

Here's one way:

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

And another:

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

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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 : john.carter@tait.co.nz
New Zealand

···

On Thu, 9 Nov 2006, Chris Mueller wrote:

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

Chris Mueller 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"

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

Split recieves a regexp as an argument. So you can be as tricky as you like
with it.

"hi, you, guys".split( %r{\s*,\s*} )
=>[ "hi","you","guys"]

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

···

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

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

···

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(/, /)

Charged Particle wrote:

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 :wink:

···

--
Posted via http://www.ruby-forum.com/\.

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-forum.com/.

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-forum.com/\.

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 :wink:

Cheers
Robert

···

On 11/9/06, spooq <spoooq@gmail.com> wrote:

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-forum.com/\.
>

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

>
> 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.

Robert, I don't really understand what you mean. My understanding is that
these do different things. Can I walk through this in pseudo pseudo code to
increase my understanding;)

split the string into an unref'ed array
take the unreffed array and strip each element in place, creating a new
string at each element in the original array

Total Arrays created 2

How does this not differ from
"he, nice, guys".split(',').map{|x| x.strip}
Where, my understanding would be

split the string into an unref'ed array
take the unref'ed array, and create a new array from the result of each
element stripped.
ie. a new string object for each element put into a new array

Total Arrays created 3

What about

x= "he, nice".split(",")
x.map!{|x|x.strip}

split the string and assign it to an array
modify each element in place and strip the result. (creating a new string
for each element)

try

x.map!{|x|x.strip!}
would you like to use this?

Nasty... Turns the first element (with no whitespace) into nil

Have I understood the difference/similarities here or have I missed the
ball?

···

On 11/9/06, Robert Dober <robert.dober@gmail.com> wrote:

On 11/9/06, spooq <spoooq@gmail.com> wrote:

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 :wink:

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-forum.com/\.
> >
>

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

>
> >
> > 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}

"he, nice, guys".split(",") ===> tmp1 <-- ["he", " nice", " guys" ]
tmp1.map!{|x| x.strip} ===> tmp2 <-- ["he", "nice", "guys"] ** and
modifies the object referenced by tmp1 in place ***

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.

The idea was to work on the differences between xxx and xxx!

Robert, I don't really understand what you mean. My understanding is that

these do different things. Can I walk through this in pseudo pseudo code
to
increase my understanding;)

Excellent idea.

split the string into an unref'ed array

take the unreffed array and strip each element in place, creating a new
string at each element in the original array

which will get lost, the only thing you use is the result of the
expression, s

Total Arrays created 2

I have no idea :wink:

How does this not differ from

"he, nice, guys".split(',').map{|x| x.strip}

"he, nice, guys".split(",") ===> tmp1 <-- ["he", " nice", " guys" ]
tmp1.map!{|x| x.strip} ===> tmp2 <-- ["he", "nice", "guys"] ***
without modifiying tmp1 inplace ***

Performance is not an issue, but I guess it is important to understand why
one would apply map! (ignoring the existance of map would not be a good
reason)

Where, my understanding would be

split the string into an unref'ed array
take the unref'ed array, and create a new array from the result of each
element stripped.
ie. a new string object for each element put into a new array

exactly (this is done too above, it would not work else)

Total Arrays created 3

Again I have no idea :wink:

What about

> x= "he, nice".split(",")
> x.map!{|x|x.strip}

split the string and assign it to an array
modify each element in place and strip the result. (creating a new string
for each element)

try
> x.map!{|x|x.strip!}
> would you like to use this?

Nasty... Turns the first element (with no whitespace) into nil

No for the same reason as above why use x.strip! modifiying x when x will
be discarded immediately? I thaught this would be the ice breaker example
:frowning:

Have I understood the difference/similarities here or have I missed the

ball?

Baseball?

BTW Sometimes I get caught in the urge to explain, forgetting that
experience has shown to me that I am quite a bad teacher :frowning:

Cheers
Robert

···

On 11/9/06, Daniel N <has.sox@gmail.com> wrote:

On 11/9/06, Robert Dober <robert.dober@gmail.com> wrote:
> On 11/9/06, spooq <spoooq@gmail.com> wrote:

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