including newlines in a .sub

Discussion in 'Ruby' started by Alan Munn, Jul 20, 2009.

  1. Alan Munn

    Alan Munn Guest

    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
     
    Alan Munn, Jul 20, 2009
    #1
    1. Advertisements

  2. Am Montag 20 Juli 2009 21:30:06 schrieb Alan Munn:
    In ruby "\n" is a newline, while '\n' is backslash followed by an n.

    HTH,
    Sebastian
     
    Sebastian Hungerecker, Jul 20, 2009
    #2
    1. Advertisements

  3. Alan Munn

    Alan Munn Guest

    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
     
    Alan Munn, Jul 20, 2009
    #3
  4. Am Montag 20 Juli 2009 22:05:05 schrieb Alan Munn:
    Well you could also use double quotes exclusively and use \\1, \\2 etc. to
    refer to the captures.

    HTH,
    Sebastian
     
    Sebastian Hungerecker, Jul 20, 2009
    #4
  5. Alan Munn

    Xavier Noria Guest

    Just in case... "\n" is a newline in any operating system.
     
    Xavier Noria, Jul 20, 2009
    #5
  6. Alan Munn

    7stud -- Guest

    You don't know what you are talking about.
     
    7stud --, Jul 20, 2009
    #6
  7. Alan Munn

    Xavier Noria Guest

    Huh?
     
    Xavier Noria, Jul 20, 2009
    #7
  8. Alan Munn

    Ryan Davis Guest

    Ignore him. He's a dick.
     
    Ryan Davis, Jul 20, 2009
    #8
  9. Alan Munn

    Jacky Cheung Guest

    [Note: parts of this message were removed to make it a legal post.]


    you need to know some basic thing of programing


    JackyCheung
     
    Jacky Cheung, Jul 22, 2009
    #9
  10. Alan Munn

    Xavier Noria Guest

    El 22/07/2009, a las 13:57, Jacky Cheung <> =20=

    escribi=C3=B3:
    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 =20=

    platform where "\n" =3D=3D "\015", and that the I/O layer is stdio =
    instead =20
    of PerIO (in MRI).

    Sent from my iPhone=
     
    Xavier Noria, Jul 22, 2009
    #10
  11. Alan Munn

    Jacky Cheung Guest

    [Note: parts of this message were removed to make it a legal post.]

    r u a programmer ? don't know '\n' is what mean???
     
    Jacky Cheung, Jul 24, 2009
    #11
  12. Alan Munn

    Kyle Smith Guest

    You're a programmer and you can't type well??? Please don't be rude.
     
    Kyle Smith, Jul 24, 2009
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.