Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 169573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:39:17+00:00 2026-05-11T12:39:17+00:00

The following… require ‘yaml’ test = I’m a b&d string File.open(‘test.yaml’, ‘w’) do |out|

  • 0

The following…

require 'yaml' test = 'I'm a b&d string' File.open('test.yaml', 'w') do |out|   out.write(test.to_yaml) end 

…outputs …

--- this is a b&d string 

How can I get it to output

--- 'this is a b&d string' 

???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:39:18+00:00Added an answer on May 11, 2026 at 12:39 pm

    If you want to store an escaped string in YAML, escape it using #inspect before you convert it to YAML:

    irb> require 'yaml' => true irb> str = %{This string's a little complicated, but it 'does the job' (man, I hate scare quotes)} => 'This string's a little complicated, but it \'does the job\' (man, I hate scare quotes)' irb> puts str This string's a little complicated, but it 'does the job' (man, I hate scare quotes) => nil irb> puts str.inspect 'This string's a little complicated, but it \'does the job\' (man, I hate scare quotes)' => nil irb> puts str.to_yaml --- This string's a little complicated, but it 'does the job' (man, I hate scare quotes) => nil irb> puts str.inspect.to_yaml --- '\'This string's a little complicated, but it \\\'does the job\\\' (man, I hate scare quotes)\'' => nil 

    YAML doesn’t quote strings unless it has to. It quotes strings if they include things that it would miss if it stored it unquoted – like surrounding quote characters or trailing or leading spaces:

    irb> puts (str + ' ').to_yaml --- 'This string's a little complicated, but it \'does the job\' (man, I hate scare quotes) ' => nil irb> puts %{'#{str}'}.to_yaml --- '\'This string's a little complicated, but it \'does the job\' (man, I hate scare quotes)\'' => nil irb> puts (' ' + str).to_yaml --- ' This string's a little complicated, but it \'does the job\' (man, I hate scare quotes)' => nil 

    However, as a YAML consumer, whether the string is quoted shouldn’t matter to you. You should never be parsing the YAML text yourself – leave that to the libraries. If you need the string to be quoted in the YAML file, that smells bad to me.

    It doesn’t matter whether your strings have ‘&’s in them, YAML will preserve the string:

    irb> test = 'I'm a b&d string' => 'I'm a b&d string' irb> YAML::load(YAML::dump(test)) => 'I'm a b&d string' irb> YAML::load(YAML::dump(test)) == test => true 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 285k
  • Answers 285k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Did you check DataSources for main report and sub report?… May 13, 2026 at 4:49 pm
  • Editorial Team
    Editorial Team added an answer Flash based: Monoslideshow Flabell Slideshow Javascript + AJAX: Building an… May 13, 2026 at 4:49 pm
  • Editorial Team
    Editorial Team added an answer If there is always a number in front, you can… May 13, 2026 at 4:49 pm

Related Questions

I am currently running into a problem where an element is coming back from
The following shell script takes a list of arguments, turns Unix paths into WINE/Windows
The following C++ code uses a ifstream object to read integers from a text
The following PHP code snippet uses GD to resize a browser-uploaded PNG to 128x128.
The following code works great in IE, but not in FF or Safari. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.