mercredi 16 mars 2016

YAML in Ruby. What is the difference between load and load_file?

I have this practice.yml file:

foo: whatever 
bar: 
 - 
   fruit: apple 
   name: steve 
   sport: baseball 
 - more 
 - 
   python: rocks 
   perl: papers 
   ruby: scissorses 

What is the difference under the hood between #load and #load_file?

#load seems to do this magic and takes a string:

pry(main)> YAML.load("'a'")
"a"
[11] pry(main)> YAML.load("a:1")
"a:1"
[12] pry(main)> YAML.load("-a")
"-a"
[13] pry(main)> YAML.load("[a]")
[
    [0] "a"
]
[14] pry(main)> YAML.load("[a,bc]")
[
    [0] "a",
    [1] "bc"
]

Whereas load_file seems to just take a file with valid yaml inside:

irb(main):002:0> YAML.load_file("practice.yml")
=> {"foo"=>"whatever", "bar"=>[{"fruit"=>"apple", "name"=>"steve", "sport"=>"baseball"}, "more", {"python"=>"rocks", "perl"=>"papers", "ruby"=>"scissorses"}]}

Aucun commentaire:

Enregistrer un commentaire