Am I misunderstanding something with my YAML code?

I thought I was grasping the concepts of yaml, but I am growing confused as
I am messing with it doing some example coding with it. Here is a gist of my
code.

It works fine until it goes to load the main.yaml file at which point I get
the following error.

yaml1.rb:37:in `<main>': undefined method `width' for
#<String:0x000001010fb9d0> (NoMethodError)

It seems like it should work. Am I just missing something or am I
approaching it wrong.

Thanks,
Buddy

···

---
Buddy Lindsey
http://www.buddylindsey.com
http://www.twitter.com/buddylindsey

I thought I was grasping the concepts of yaml, but I am growing confused as
I am messing with it doing some example coding with it. Here is a gist of my
code.

Loading YAML from a file Problem · GitHub

It works fine until it goes to load the main.yaml file at which point I get
the following error.

yaml1.rb:37:in `<main>': undefined method `width' for
#<String:0x000001010fb9d0> (NoMethodError)

It seems like it should work. Am I just missing something or am I
approaching it wrong.

You're serializing yaml to yaml:

serialized = Square.new(2,3).to_yaml

# save yaml to file
File.open('main.yaml', 'w') do |out|
  YAML.dump(serialized, out)
end

% ri YAML.dump
= YAML.dump

(from ruby core)

···

On Feb 8, 2011, at 15:41 , Buddy Lindsey, Jr. wrote:
------------------------------------------------------------------------------
  dump( obj, io = nil )

------------------------------------------------------------------------------

Converts obj to YAML and writes the YAML result to io.
    
  File.open( 'animals.yaml', 'w' ) do |out|
    YAML.dump( ['badger', 'elephant', 'tiger'], out )
  end

If no io is provided, a string containing the dumped YAML is returned.

  YAML.dump( :locked )
     #=> "--- :locked"

Okay, that is what I get for refactoring code. I totally missed that. I was
trying to make the code a little prettier than it was and completely
disregarded YAML.dump. That makes sense

Thanks,
Buddy

···

---
Buddy Lindsey

http://www.twitter.com/buddylindsey

On Tue, Feb 8, 2011 at 6:16 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Feb 8, 2011, at 15:41 , Buddy Lindsey, Jr. wrote:

> I thought I was grasping the concepts of yaml, but I am growing confused
as
> I am messing with it doing some example coding with it. Here is a gist of
my
> code.
>
> Loading YAML from a file Problem · GitHub
>
> It works fine until it goes to load the main.yaml file at which point I
get
> the following error.
>
> yaml1.rb:37:in `<main>': undefined method `width' for
> #<String:0x000001010fb9d0> (NoMethodError)
>
> It seems like it should work. Am I just missing something or am I
> approaching it wrong.

You're serializing yaml to yaml:

> serialized = Square.new(2,3).to_yaml
>
> # save yaml to file
> File.open('main.yaml', 'w') do |out|
> YAML.dump(serialized, out)
> end

% ri YAML.dump
= YAML.dump

(from ruby core)

------------------------------------------------------------------------------
dump( obj, io = nil )

------------------------------------------------------------------------------

Converts obj to YAML and writes the YAML result to io.

File.open( 'animals.yaml', 'w' ) do |out|
   YAML.dump( ['badger', 'elephant', 'tiger'], out )
end

If no io is provided, a string containing the dumped YAML is returned.

YAML.dump( :locked )
    #=> "--- :locked"