ruby / ruby/psych

Preserve quote style while changing a YAML file

Open
#671 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
597
Forks
223
Avg merge
11h 23m
Merged PRs (30d)
3

Description

I have Rails I18n en.yml file which contains values with or without qoutes.

I want to change this file programmatically, but if I use Psych#load and Psych#dump approach it removes all the quotes from values. I don't want to change styling of existing values, how can I do that?

As I understand parsing the ast is the way, but I want to preserve simplicity of working with YAML as the hash and then simply dump it back as the original file but with a few new key/values.

Input:

en:
  account:
    invoices:
      empty: 'It''s empty here'
      tabs:
        donations: Donations
        giftcards: 'Gift Cards'
        membership: Membership
        purchases: Purchases
        subscription: Subscription
      title: 'Invoices History'

Expected result (all styling preserved, new key added):

en:
  account:
    invoices:
      empty: 'It''s empty here'
      tabs:
        donations: Donations
        giftcards: 'Gift Cards'
        membership: Membership
        purchases: Purchases
        subscription: Subscription
        my_new_key: New Value
      title: 'Invoices History'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Psych#load and Psych#dump behavior described in the issue, then investigate the YAML AST path and how quote style is represented for en.yml values. Done means adding a key while preserving the existing quoting and other styling shown in the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby, yaml
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.