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 9088187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:51:55+00:00 2026-06-16T21:51:55+00:00

The to_yaml method produces nice YAML output, but I would like to include comment

  • 0

The to_yaml method produces nice YAML output, but I would like to include comment lines before some of the elements. Is there a way to do so?

For example, I would like to produce:

# hostname or IP address of client
client: host4.example.com
# hostname or IP address of server
server: 192.168.222.222

From something similar to:

{
  :client => 'host4.example.com',
  :server => '192.168.222.222',
}.to_yaml

… but am not sure if the YAML module even has a way to accomplish.

UPDATE: I ended up not using the solution which used regexes to insert the comments, since it required the separation of the data from the comments. The easiest and most understandable solution for me is:

require 'yaml'

source = <<SOURCE
# hostname or IP address of client
client: host4.example.com
# hostname or IP address of server
server: 192.168.222.222
SOURCE

conf = YAML::load(source)

puts source

The benefit to me is that nothing is repeated (for example, ‘client:’ is only specified once), the data and comments are together, the source can be outputted as YAML, and the data structure (available in conf) is available for use.

  • 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. Editorial Team
    Editorial Team
    2026-06-16T21:51:56+00:00Added an answer on June 16, 2026 at 9:51 pm

    You can do a string replace on all the insertions:

    require 'yaml'
    
    source = {
      :client => 'host4.example.com',
      :server => '192.168.222.222',
    }.to_yaml
    
    substitution_list = {
      /:client:/ => "# hostname or IP address of client\n:client:",
      /:server:/ => "# hostname or IP address of server\n:server:"
    }
    
    substitution_list.each do |pattern, replacement|
      source.gsub!(pattern, replacement)
    end
    
    puts source
    

    output:

    --- 
    # hostname or IP address of client
    :client: host4.example.com
    # hostname or IP address of server
    :server: 192.168.222.222
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code require 'yaml' puts YAML.load( is_something: values: ['yes', 'no'] ).to_yaml produces --- is_something:
requrie 'yaml' hash = {:title=>'abc'} hash.to_yaml will output: --- title: abc Is there any
I am new to Django. I would like to run some command from some
I have a YAML file of groups that I would like to get into
A method performs a recursive scan for yaml files and parses them, extracts some
I'm trying to save some lookup table data out to a YAML file so
I'm looking to take an existing YAML file, configured like this: en: calendars: gregorian:
What's the simplest method to convert YAML to dot-separated strings in Ruby? So this:
Is it possible to force Ruby to call an initialize method when using YAML.load_file?
I would like to ask what is the common way for handling role-based security

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.