[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem loading a YAML file

Keith Carter

1/28/2008 8:19:00 PM

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

6 Answers

Ilan Berci

1/28/2008 8:29:00 PM

0

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

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

lemurific@gmail.com

1/28/2008 8:30:00 PM

0

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

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

Phrogz

1/28/2008 8:31:00 PM

0

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"

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

Keith Carter

1/28/2008 8:55:00 PM

0

Ilan Berci wrote:
> hth
>
> ilan

It definitely helped! Thank you sir.

--
Posted via http://www.ruby-....

Andrea Fazzi

1/29/2008 10:58:00 AM

0

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?
>


George Malamidis

1/29/2008 11:12:00 AM

0

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?
>>
>
>