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

···

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

Hello

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

Do you have any names for me?

Thanks !

Jeanne

···

-----Original Message-----
From: Joe Collins [mailto:joec_49@hotmail.com]
Sent: 20 September 2011 16:28
To: ruby-talk ML
Subject: 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

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

Search for CVs on-line or sign-up for jobs by email at:
www.progressiverecruitment.com

This electronic transmission is confidential and intended solely for the
addressee(s). If you are not an intended addressee, you must not disclose, copy,
distribute or take any action in reliance upon this transmission. If you have
received this transmission in error, please notify Progressive Recruitment
Limited.

You can find our privacy statement at
http://www.progressiverecruitment.com/en/page/privacy_policy If you do not want
to receive electronic mail from us about our services, or would like to confirm
or qualify how we hold your personal data, please send a request via email to
data-audit@progressiverecruitment.com
http://www.progressiverecruitment.com/en/page/registration_details

I have an array

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

···

On Tue, Sep 20, 2011 at 8:27 AM, Joe Collins <joec_49@hotmail.com> wrote:

Yes; see the Ruby doc for Array -- 'map' method

···

On Tue, Sep 20, 2011 at 7:27 AM, Joe Collins <joec_49@hotmail.com> wrote:

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

Is there an easy way to do this

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

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.

ruby-1.9.2-p290 :001 > ["DOG","CAT","HAT"].map { |element| element + '\n' }
=> ["DOG\\n", "CAT\\n", "HAT\\n"]

Mayank

that worked perfectly (map) - thanks.

Joe

···

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

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

robert

···

On Tue, Sep 20, 2011 at 4:45 PM, Londiche, Jeanne <j.londiche@progressiverecruitment.com> wrote:

Hello

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

Do you have any names for me?

Thanks !

Jeanne

-----Original Message-----
From: Joe Collins [mailto:joec_49@hotmail.com]
Sent: 20 September 2011 16:28
To: ruby-talk ML
Subject: 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

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

Search for CVs on-line or sign-up for jobs by email at:
www.progressiverecruitment.com

This electronic transmission is confidential and intended solely for the
addressee(s). If you are not an intended addressee, you must not disclose, copy,
distribute or take any action in reliance upon this transmission. If you have
received this transmission in error, please notify Progressive Recruitment
Limited.

You can find our privacy statement at
http://www.progressiverecruitment.com/en/page/privacy_policy If you do not want
to receive electronic mail from us about our services, or would like to confirm
or qualify how we hold your personal data, please send a request via email to
data-audit@progressiverecruitment.com
http://www.progressiverecruitment.com/en/page/registration_details

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

On Tue, Sep 20, 2011 at 4:45 PM, Dominic Sisneros <dsisnero@gmail.com> wrote:

On Tue, Sep 20, 2011 at 8:27 AM, Joe Collins <joec_49@hotmail.com> wrote:

I have an array

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hello

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

Do you have any names for me?

Thanks !

Jeanne

···

-----Original Message-----
From: Hassan Schroeder [mailto:hassan.schroeder@gmail.com]
Sent: 20 September 2011 16:50
To: ruby-talk ML
Subject: Re: appending \n to each element in an array

On Tue, Sep 20, 2011 at 7:27 AM, Joe Collins <joec_49@hotmail.com> 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

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

Search for CVs on-line or sign-up for jobs by email at:

This electronic transmission is confidential and intended solely for the
addressee(s.) If you are not an intended addressee, you must not disclose, copy,
distribute or take any action in reliance upon this transmission. If you have
received this transmission in error, please notify Progressive Recruitment
Limited.

You can find our privacy statement at
http://www.progressiverecruitment.com/en/page/privacy_policy If you do not want
to receive electronic mail from us about our services, or would like to confirm
or qualify how we hold your personal data, please send a request via email to
data-audit@progressiverecruitment.com
http://www.progressiverecruitment.com/en/page/registration_details

David Heinemeier Hansson.

···

On Tue, Sep 20, 2011 at 3:57 PM, Londiche, Jeanne < j.londiche@progressiverecruitment.com> wrote:

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

Do you have any names for me?

On Tue, Sep 20, 2011 at 10:57, Londiche, Jeanne
<j.londiche@progressiverecruitment.com> 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

···

--
LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.
Main Web Site: davearonson.com
Programming Blog: codosaur.us
Excellence Blog: dare2xl.com

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

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 9:27 AM, Joe Collins <joec_49@hotmail.com> wrote:

On Tue, Sep 20, 2011 at 10:06 AM, Adam Prescott <adam@aprescott.com> wrote:

On Tue, Sep 20, 2011 at 3:57 PM, Londiche, Jeanne < > j.londiche@progressiverecruitment.com> wrote:

> I am looking for a good candidate on ruby and rails.
>
> Do you have any names for me?
>

David Heinemeier Hansson.

I I laughed out loud.