Problem loading a YAML file

K

Keith Carter

I'm having issues loading a YAML file. Here is the YAML file (named
test.yml):

foo: 5
bar: some string

Here is my Ruby:

C:\noozler\trunk>ruby script/console
Loading development environment (Rails 2.0.2)
require 'yaml' => []
File.exists?("test.yml") => true
t = YAML::load("test.yml") => "test.yml"
t['bar'] => nil

I would expect t['bar'] to show: "some string".

Any ideas?
 
I

Ilan Berci

Keith said:
I'm having issues loading a YAML file. Here is the YAML file (named
test.yml):

foo: 5
bar: some string

Here is my Ruby:

C:\noozler\trunk>ruby script/console
Loading development environment (Rails 2.0.2)
require 'yaml' => []
File.exists?("test.yml") => true
t = YAML::load("test.yml") => "test.yml"
t['bar'] => nil

I would expect t['bar'] to show: "some string".

Any ideas?

The "t" you are loading is a String.. always check the types for a
clue.. :)

irb(main):002:0> require 'yaml'
=> true
irb(main):003:0> File.open("test.yml") {|f| YAML::load(f)['bar']}
=> "some string"

hth

ilan
 
L

lemurific

I'm having issues loading a YAML file. Here is the YAML file (named
test.yml):

foo: 5
bar: some string

Here is my Ruby:

C:\noozler\trunk>ruby script/console
Loading development environment (Rails 2.0.2)
require 'yaml' => []
File.exists?("test.yml") => true
t = YAML::load("test.yml") => "test.yml"
t['bar']
=> nil

I would expect t['bar'] to show: "some string".

You want YAML::load_file which, erm, loads a file; not YAML::load
which parses from the string you pass it.
 
P

Phrogz

I'm having issues loading a YAML file. Here is the YAML file (named
test.yml):

foo: 5
bar: some string

Here is my Ruby:

C:\noozler\trunk>ruby script/console
Loading development environment (Rails 2.0.2)
require 'yaml' => []
File.exists?("test.yml") => true
t = YAML::load("test.yml")
=> "test.yml"

WARNING! Right here, you see that the result of loading "test.yml" is
"test.yml". It's not a Hash like you expected.

Apparently YAML.load takes a raw YAML string, not a filename. Try:

irb(main):004:0> y = YAML.load( IO.read( 'test.yml' ) )
=> {"foo"=>5, "bar"=>"some string"}
 
A

Andrea Fazzi

Try this:

require 'yaml'

t = YAML::load( File.open('test.yaml') )
t['bar']

Keith Carter ha scritto:
I'm having issues loading a YAML file. Here is the YAML file (named
test.yml):

foo: 5
bar: some string

Here is my Ruby:

C:\noozler\trunk>ruby script/console
Loading development environment (Rails 2.0.2)
=> []
=> true
=> "test.yml"
=> nil


I would expect t['bar'] to show: "some string".

Any ideas?
 
G

George Malamidis

Or

t = YAML::load_file 'test.yaml'

George

Try this:

require 'yaml'

t = YAML::load( File.open('test.yaml') )
t['bar']

Keith Carter ha scritto:
I'm having issues loading a YAML file. Here is the YAML file (named
test.yml):

foo: 5
bar: some string

Here is my Ruby:

C:\noozler\trunk>ruby script/console
Loading development environment (Rails 2.0.2)
require 'yaml'
=> []
File.exists?("test.yml")
=> true
t = YAML::load("test.yml")
=> "test.yml"
=> nil

I would expect t['bar'] to show: "some string".

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top