Problem loading a YAML file

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?

···

--
Posted via http://www.ruby-forum.com/\.

Keith Carter wrote:

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.. :slight_smile:

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

hth

ilan

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Jan 28, 3:19 pm, Keith Carter <kmarplecar...@yahoo.com> wrote:

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".

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

···

On Jan 28, 1:19 pm, Keith Carter <kmarplecar...@yahoo.com> wrote:

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"

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"
  

t['bar']
      

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

Any ideas?

Ilan Berci wrote:

hth

ilan

It definitely helped! Thank you sir.

···

--
Posted via http://www.ruby-forum.com/\.

Or

t = YAML::load_file 'test.yaml'

George

···

On 29 Jan 2008, at 10:57, Andrea Fazzi wrote:

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"

t['bar']

=> nil

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

Any ideas?