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

  • Home
  • SEARCH
  • 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

Related Questions

following code is used to find url from a string with php. Here is
Following String causes PatternSyntaxException : Pattern.compile(*\\.*); I want to create a pattern so that
Following this JavaFX 2.0 tutorial I created a css file in eclipse that includes
Following: <% if ((String) session.getAttribute(userID) != null) { String username= (String) session.getAttribute(userID); %> <li><a><small>Logged
Following is my page & control hierarchy. Page UserControl 1 <Repeater1> //Contents of UserControl
Following code: <?php $test_array = array(); $test_array['string_index'] = data in string index; $test_array[] =
Following my question regarding a .NET YAML Library ... as there doesn't seem to
Following on from this question what would be the best way to write a
I'm trying to work out why it won't let me attach a file from
Following is the method adopted to upload file to specific folder using Gdrive V2

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.