Printing varible with quotes around it

Discussion in 'Ruby' started by Jack Smith, Oct 6, 2008.

  1. Jack Smith

    Jack Smith Guest

    I am trying to print my variable in a puts statment but add quotes
    around it.

    This works but does not put quotes around my text:
    z = "This is my text"
    puts z

    I have tried

    puts %Q!z!
    and
    puts '"#{z}"'

    but I can't seem to get my text to print out like:

    "This is my text"

    thanks

    John
    --
    Posted via http://www.ruby-forum.com/.
     
    Jack Smith, Oct 6, 2008
    #1
    1. Advertising

  2. On Mon, Oct 6, 2008 at 11:29 AM, Jack Smith <> wrote:
    > I am trying to print my variable in a puts statment but add quotes
    > around it.
    >
    > This works but does not put quotes around my text:
    > z = "This is my text"
    > puts z
    >
    > I have tried
    >
    > puts %Q!z!
    > and
    > puts '"#{z}"'
    >
    > but I can't seem to get my text to print out like:
    >
    > "This is my text"
    >

    You need to escape the " with \.

    puts "\"This is my text\""

    HTH,
    Michael Guterl
     
    Michael Guterl, Oct 6, 2008
    #2
    1. Advertising

  3. Jack Smith

    Thomas B. Guest

    Jack Smith wrote:
    > I am trying to print my variable in a puts statment but add quotes
    > around it.
    >
    > This works but does not put quotes around my text:
    > z = "This is my text"
    > puts z
    >
    > I have tried
    >
    > puts %Q!z!
    > and
    > puts '"#{z}"'
    >
    > but I can't seem to get my text to print out like:
    >
    > "This is my text"
    >
    > thanks
    >
    > John


    You get this result if you use p instead of puts. But there's also one
    great thing to remember: what p does is in fact:

    def p(x) # I skipped the multiple argument variant for simplicity
    puts x.inspect
    end

    The function inspect is also used when irb presents results of last
    operation after the => sign, so you can also do this with arrays, hashes
    and all other types of objects, if you want to print them in a nice way,
    with all dangerous characters escaped and so on.

    TPR.
    --
    Posted via http://www.ruby-forum.com/.
     
    Thomas B., Oct 6, 2008
    #3
  4. Jack Smith wrote:
    > I have tried
    >
    > puts %Q!z!
    > and
    > puts '"#{z}"'


    #{...} is only interpolated inside double-quoted strings. So you need:

    puts "\"#{z}\""

    (where the double-quotes inside the double-quotes need to be escaped
    with a backslash), or

    puts %Q{"#{z}"}

    (where they do not)

    Or as others have said,

    puts z.inspect

    will show your string with double quotes around (but it will also
    perform additional transformations, such as turning " to \" and showing
    control characters in their escaped form, like \n for newline)
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Oct 6, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,909
    Hermit Dave
    Sep 22, 2004
  2. Chris
    Replies:
    1
    Views:
    13,752
    Oisin
    Mar 24, 2006
  3. Lawrence Tierney

    Multiline quotes - escaping quotes - et al

    Lawrence Tierney, Dec 24, 2003, in forum: Java
    Replies:
    3
    Views:
    4,544
    Andrew Thompson
    Dec 24, 2003
  4. jOhn
    Replies:
    1
    Views:
    267
    Phlip
    Jan 29, 2008
  5. Richard Sandoval
    Replies:
    5
    Views:
    229
    7stud --
    Apr 26, 2011
Loading...

Share This Page