Appending \n to each element in an array

I have an array
DOG
CAT
HAT
BOY HOOD

etc

I want to put a newline at the end of each phrase in the array.

Is there an easy way to do this or must I loop and do it.

thanks in advance
Joe

Hello

I am looking for a good candidate on ruby and rails.

Do you have any names for me?

Thanks !

Jeanne

On Tue, Sep 20, 2011 at 7:27 AM, Joe C. [email protected]
wrote:

I want to put a newline at the end of each phrase in the array.

Is there an easy way to do this

Yes; see the Ruby doc for Array – ‘map’ method

On Tue, Sep 20, 2011 at 8:27 AM, Joe C. [email protected]
wrote:

I have an array

I want to put a newline at the end of each phrase in the array.

words = %w[some of my favourite words]
=> [“some”, “of”, “my”, “favourite”, “words”]

words.map! { |word| “#{word}\n” }
=> [“some\n”, “of\n”, “my\n”, “favourite\n”, “words\n”]

words
=> [“some\n”, “of\n”, “my\n”, “favourite\n”, “words\n”]

Check RDoc Documentation for the methods #map and #map!.

There is also the option of using #each and modifying each string
in-place
as you iterate.

words = %w[some of my favourite words]
=> words.each { |word| word << “\n” }

However, modifying elements while iterating with #each can make your
life
harder, so you should stick to map! to modify the array in-place.

On Tue, Sep 20, 2011 at 4:45 PM, Dominic S. [email protected]
wrote:

On Tue, Sep 20, 2011 at 8:27 AM, Joe C. [email protected] wrote:

I have an array

%w[DOG CAT HAT BOY].join(“\n”)

It’s not exactly what the OP wanted… :slight_smile: Also, I am suspecting a
homework assignment here.

Joe, what do you need those newlines for? Are you aware how
puts(an_array) works? If not, please make yourself familiar with it

Kind regards

robert

Please do not hijack threads - especially not for commercial activities!

robert

On Tue, Sep 20, 2011 at 4:45 PM, Londiche, Jeanne

Hello

I am looking for a good candidate on ruby and rails.

Do you have any names for me?

Thanks !

Jeanne

that worked perfectly (map) - thanks.

Joe

On Tue, Sep 20, 2011 at 3:57 PM, Londiche, Jeanne <
[email protected]> wrote:

I am looking for a good candidate on ruby and rails.

Do you have any names for me?

David Heinemeier H…

ruby-1.9.2-p290 :001 > [“DOG”,“CAT”,“HAT”].map { |element| element +
‘\n’ }
=> [“DOG\n”, “CAT\n”, “HAT\n”]

Mayank

On Tue, Sep 20, 2011 at 10:57, Londiche, Jeanne
[email protected] wrote (in a thread that had
already been going on a completely different topic, appropriate to the
Subject line):

Hello

I am looking for a good candidate on ruby and rails.

Do you have any names for me?

Thanks !

Why yes. Yes I do have names for you. Many of them. But I’m polite
enough not to call you them on a public mailing list. :stuck_out_tongue:

-Dave

On Tue, Sep 20, 2011 at 9:27 AM, Joe C. [email protected]
wrote:

Is there an easy way to do this or must I loop and do it.

thanks in advance
Joe

Considering the use cases for something like this makes it seem decently
questionable. Can you explain the problem you think this will solve? (ie
the
only obvious use case I can think of is embedding text in a template,
but if
you’re doing something like that, then you probably know about map and
each
and join, which could be used to solve this problem anyway)

On Tue, Sep 20, 2011 at 10:06 AM, Adam P. [email protected]
wrote:

On Tue, Sep 20, 2011 at 3:57 PM, Londiche, Jeanne <
[email protected]> wrote:

I am looking for a good candidate on ruby and rails.

Do you have any names for me?

David Heinemeier H…

I I laughed out loud.