Ruby on Rails is an open-source framework designed to simplify the development of web applications using the Ruby programming language. Ruby on Rails has a built-in module for localization that uses YAML as the standard file format (along with RB). Phrase supports translation and localization with YAML files.
Loading a YAML Document Safely Using safe_load() safe_load(stream) Parses the given and returns a Python object constructed from the first document in the stream. safe_load recognizes only standard YAML tags and cannot construct an arbitrary Python object.
YAML syntaxYAML has features that come from Perl, C, XML, HTML, and other programming languages. YAML is also a superset of JSON, so JSON files are valid in YAML. YAML uses Python-style indentation to indicate nesting. Tab characters are not allowed, so whitespaces are used instead.
Maybe I'm missing something, but why try to parse the file? Why not just load the YAML and examine the object(s) that result?
If your sample YAML is in some.yml
, then this:
require 'yaml'
thing = YAML.load_file('some.yml')
puts thing.inspect
gives me
{"javascripts"=>[{"fo_global"=>["lazyload-min", "holla-min"]}]}
I had the same problem but also wanted to get the content of the file (after the YAML front-matter).
This is the best solution I have found:
if (md = contents.match(/^(?<metadata>---\s*\n.*?\n?)^(---\s*$\n?)/m))
self.contents = md.post_match
self.metadata = YAML.load(md[:metadata])
end
Source and discussion: https://practicingruby.com/articles/tricks-for-working-with-text-and-files
Here is the one liner i use, from terminal, to test the content of yml file(s):
$ ruby -r yaml -r pp -e 'pp YAML.load_file("/Users/za/project/application.yml")'
{"logging"=>
{"path"=>"/var/logs/",
"file"=>"TacoCloud.log",
"level"=>
{"root"=>"WARN", "org"=>{"springframework"=>{"security"=>"DEBUG"}}}}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With