Including newlines in a .sub

Hi, I'm new to ruby, and am having trouble with the following (\n is
newline on a Mac).

e.g.

line = "foo bar"
line = line.sub(/ /, '\n')
puts line

This produces:

foo\nbar

when what I want (and expected) was:

foo
bar

(This is just a toy example; I don't actually want to split lines on
spaces.) What I don't understand is how to insert a true newline into a
string so that it outputs as such.

Thanks

Alan

In ruby "\n" is a newline, while '\n' is backslash followed by an n.

HTH,
Sebastian

···

Am Montag 20 Juli 2009 21:30:06 schrieb Alan Munn:

line = line.sub(/ /, '\n')

Just in case... "\n" is a newline in any operating system.

···

On Mon, Jul 20, 2009 at 9:30 PM, Alan Munn<amunn@msu.edu> wrote:

Hi, I'm new to ruby, and am having trouble with the following (\n is
newline on a Mac).

In article <200907202136.26888.sepp00@web.de>,

···

Sebastian Hungerecker <sepp2k@googlemail.com> wrote:

Am Montag 20 Juli 2009 21:30:06 schrieb Alan Munn:
> line = line.sub(/ /, '\n')

In ruby "\n" is a newline, while '\n' is backslash followed by an n.

Perfect. Thanks. I had tried that with my more complicated example
(which included reference to capture groups from the regex) and it
didn't work, but now I realise I need to concatenate the pieces together
with a mix of single and double quotes to get it to work.

Alan

Xavier Noria wrote:

···

On Mon, Jul 20, 2009 at 9:30 PM, Alan Munn<amunn@msu.edu> wrote:

Hi, I'm new to ruby, and am having trouble with the following (\n is
newline on a Mac).

Just in case... "\n" is a newline in any operating system.

You don't know what you are talking about.
--
Posted via http://www.ruby-forum.com/\.

Well you could also use double quotes exclusively and use \\1, \\2 etc. to
refer to the captures.

HTH,
Sebastian

···

Am Montag 20 Juli 2009 22:05:05 schrieb Alan Munn:

Perfect. Thanks. I had tried that with my more complicated example
(which included reference to capture groups from the regex) and it
didn't work, but now I realise I need to concatenate the pieces together
with a mix of single and double quotes to get it to work.

Huh?

···

On Tue, Jul 21, 2009 at 12:13 AM, 7stud --<bbxx789_05ss@yahoo.com> wrote:

You don't know what you are talking about.

Ignore him. He's a dick.

···

On Jul 20, 2009, at 15:18 , Xavier Noria wrote:

On Tue, Jul 21, 2009 at 12:13 AM, 7stud --<bbxx789_05ss@yahoo.com> > wrote:

You don't know what you are talking about.

Huh?

you need to know some basic thing of programing

JackyCheung

···

2009/7/21 Ryan Davis <ryand-ruby@zenspider.com>

On Jul 20, 2009, at 15:18 , Xavier Noria wrote:

On Tue, Jul 21, 2009 at 12:13 AM, 7stud --<bbxx789_05ss@yahoo.com> wrote:

You don't know what you are talking about.

Huh?

Ignore him. He's a dick.

Don't know what you mean.

This article may give some light:

It's about Perl but the same principles hold in Ruby except there's no platform where "\n" == "\015", and that the I/O layer is stdio instead of PerIO (in MRI).

···

El 22/07/2009, a las 13:57, Jacky Cheung <jackycheung.king@gmail.com> escribió:

2009/7/21 Ryan Davis <ryand-ruby@zenspider.com>

On Jul 20, 2009, at 15:18 , Xavier Noria wrote:

On Tue, Jul 21, 2009 at 12:13 AM, 7stud --<bbxx789_05ss@yahoo.com> >> wrote:

You don't know what you are talking about.

Huh?

Ignore him. He's a dick.

you need to know some basic thing of programing

Sent from my iPhone

r u a programmer ? don't know '\n' is what mean???

···

--
JackyCheung

You're a programmer and you can't type well??? Please don't be rude.

···

On Fri, 24 Jul 2009 13:34:41 +0900, Jacky Cheung <jackycheung.king@gmail.com> wrote:

r u a programmer ? don't know '\n' is what mean???

--
- Kyle