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

Am Montag 20 Juli 2009 21:30:06 schrieb Alan M.:

line = line.sub(/ /, ‘\n’)

In ruby “\n” is a newline, while ‘\n’ is backslash followed by an n.

HTH,
Sebastian

In article [email protected],
Sebastian H. [email protected] wrote:

Am Montag 20 Juli 2009 21:30:06 schrieb Alan M.:

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

Am Montag 20 Juli 2009 22:05:05 schrieb Alan M.:

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.

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

HTH,
Sebastian

Xavier N. wrote:

On Mon, Jul 20, 2009 at 9:30 PM, Alan M.removed_email_addr[email protected] 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.

On Mon, Jul 20, 2009 at 9:30 PM, Alan M.[email protected] 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.

On Tue, Jul 21, 2009 at 12:13 AM, 7stud –[email protected]
wrote:

You don’t know what you are talking about.

Huh?

On Jul 20, 2009, at 15:18 , Xavier N. wrote:

On Tue, Jul 21, 2009 at 12:13 AM, 7stud –[email protected]
wrote:

You don’t know what you are talking about.

Huh?

Ignore him. He’s a dick.

2009/7/21 Ryan D. [email protected]

Ignore him. He’s a dick.

you need to know some basic thing of programing

JackyCheung

El 22/07/2009, a las 13:57, Jacky C. [email protected]
escribió:

Huh?

Ignore him. He’s a dick.

you need to know some basic thing of programing

Don’t know what you mean.

This article may give some light:

http://onlamp.com/pub/a/onlamp/2006/08/17/understanding-newlines.html

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

Sent from my iPhone

On Fri, 24 Jul 2009 13:34:41 +0900, Jacky C.
[email protected] wrote:

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs