ruby/psych

Preserve quote style while changing a YAML file

Opened this issue · 0 comments

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'