How do I parse a YAML file in Ruby?

前端 未结 3 856
时光取名叫无心
时光取名叫无心 2020-11-29 16:01

I would like to know how to parse a YAML file with the following contents:

--- 
javascripts: 
- fo_global:
  - lazyload-min
  - holla-min

C

3条回答
  •  不知归路
    2020-11-29 16:57

    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"]}]}
    

提交回复
热议问题