#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Edinburgh, UK
    Posts
    84
    Rep Power
    11

    Printing double-quotes to files in Ruby


    Hi All,

    I had a brilliant thought this morning, why not use ruby to generate some xml for me.

    Was a great idea, but I have fallen flat on my face when it comes to adding " to my xml file.

    I am trying to create this line: puts results_file.puts "<component timestamp="" name=""#{x}"" />"

    But, my issue is that it is giving me:
    <component timestamp= name=some_name.dll />

    as you can probably tell, this doesnt look good. Could some kind soul let me know how to put the " char correctly into my output please?

    Cheers
    -
    Karym6
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Edinburgh, UK
    Posts
    84
    Rep Power
    11
    and I figured it out by using \" to include them in the file making my code look like:

    puts results_file.puts "<component timestamp=\"\" name=\"#{x}\" />

    Originally Posted by karym6
    Hi All,

    I had a brilliant thought this morning, why not use ruby to generate some xml for me.

    Was a great idea, but I have fallen flat on my face when it comes to adding " to my xml file.

    I am trying to create this line: puts results_file.puts "<component timestamp="" name=""#{x}"" />"

    But, my issue is that it is giving me:
    <component timestamp= name=some_name.dll />

    as you can probably tell, this doesnt look good. Could some kind soul let me know how to put the " char correctly into my output please?

    Cheers
    -
    Karym6
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Alternatively, you could enclose your string in %Q{}
    Code:
    #!/usr/bin/ruby
    
    x = "hello"
    f = File.open("file.txt", "w")
    f.puts %Q{<component timestamp="" name="#{x}" />}
    f.close()
    Note that %q won't substitute #{x} and will leave it in literally, whereas %Q will interpolate the variable.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Edinburgh, UK
    Posts
    84
    Rep Power
    11
    Even more graceful - thanks

    Originally Posted by Scorpions4ever
    Alternatively, you could enclose your string in %Q{}
    Code:
    #!/usr/bin/ruby
    
    x = "hello"
    f = File.open("file.txt", "w")
    f.puts %Q{<component timestamp="" name="#{x}" />}
    f.close()
    Note that %q won't substitute #{x} and will leave it in literally, whereas %Q will interpolate the variable.

IMN logo majestic logo threadwatch logo seochat tools logo