Home > OS >  Ruby on Rails - How to generate a YAML file with a key that starts with a dot?
Ruby on Rails - How to generate a YAML file with a key that starts with a dot?

Time:12-23

I would like generate on Ruby on Rails a YAML file with a key that start with a dot (. before_script_template), like

.before_script_template:
  before_script:
    - gem install bundler

but I am generating a string like ".before_script_template":

".before_script_template":
  before_script:
  - gem install bundler

This is my code in Ruby to generate .yml file:

  pipeline_file = { 
                    ".before_script_template" => {"before_script" => ["gem install bundler"]}
                  }

  File.open("./my_project/folder/.gitlab-ci-template.yml", "r ") do |f|
    f.write(pipeline_file.to_yaml.gsub(/^---$/, ""))
  end

How can I generate a key like .before_script_template: instead of ".before_script_template":?

CodePudding user response:

Both YAML docs are equivalent. One is simply using quotes to clarify the key.

require 'yaml'

# {".before_script_template"=>{"before_script"=>["gem install bundler"]}}
p YAML.load(<<-END)
".before_script_template":
  before_script:
  - gem install bundler
END

# {".before_script_template"=>{"before_script"=>["gem install bundler"]}}
p YAML.load(<<-END)
.before_script_template:
  before_script:
  - gem install bundler
END

CodePudding user response:

This has nothing to do with YAML specifically as both .before_script_template and ".before_script_template" are valid tags in YAML.

This has to do with the implementation determined by the parser/emitter (Psych in this case).

Specifically this is shown in Psych::Visitors::YAMLTree class in the method visit_String

As far as the quoting style goes this code boils down to:

 PLAIN         = 1
 SINGLE_QUOTED = 2
 DOUBLE_QUOTED = 3
 LITERAL       = 4
 FOLDED        = 5

def quoting_style(o)
  style = PLAIN
  if o.encoding == Encoding::ASCII_8BIT && !o.ascii_only?
    style = LITERAL
  elsif o =~ /\n(?!\Z)/  
    style = LITERAL
  elsif o == '<<'
    style = SINGLE_QUOTED
  elsif o == 'y' || o == 'n'
    style = DOUBLE_QUOTED
  elsif @line_width && o.length > @line_width
    style = FOLDED
  elsif o =~ /^[^[:word:]][^"]*$/
    style = DOUBLE_QUOTED
 # elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/ =~ o
 # commented out so you can see it but this question is not about the ScalarScanner
 # (https://github.com/ruby/psych/blob/v5.0.0/lib/psych/scalar_scanner.rb)
 #   style = SINGLE_QUOTED
  end
  style
end 

quoting_style("before_script") #=> 1
quoting_style(".before_script_template") #=> 3

In this case DOUBLE_QUOTED is utilized because:

".before_script_template" =~ /^[^[:word:]][^"]*$/

Example

  • Related