RubyQuestions
12/3/2003 2:15:00 AM
I'm trying to use the transform method in YAML to grab the values from
my
classes. Everything works correctly, except when I try to get the
price data (Floats) from my class. I'm trying to understand if I
should be doing something different.
Thanks.
Here is my sample script and IRB session:
require 'yaml'
class Video
attr_accessor :price, :title, :director, :year
def initialize(p, t, d, y)
@price = p
@title = t
@director = d
@year = y
end
end
myVideoCollection = [
Video.new(22.45, "Moonshine Hustler", "Moose McDaniel", 1928),
Video.new(19.72, "Crazy Cool Camels", "Rob Crowfeet", 1985)
]
videos = {"Videos" => myVideoCollection}
##########IRB#############
irb(main):035:0> puts videos.to_yaml
---
Videos:
- !ruby/object:Video
director: Moose McDaniel
price: 22.45
title: Moonshine Hustler
year: 1928
- !ruby/object:Video
director: Rob Crowfeet
price: 19.72
title: Crazy Cool Camels
year: 1985
##########################
videoListYaml = YAML::parse(videos.to_yaml)
Summary
#########################
irb(main):065:0> p videoListYaml.select("Videos//title").transform
["Moonshine Hustler", "Crazy Cool Camels"]
## WORKS...
irb(main):066:0> p videoListYaml.select("Videos//director").transform
["Moose McDaniel", "Rob Crowfeet"]
## WORKS...
irb(main):067:0> p videoListYaml.select("Videos//year").transform
[1928, 1985]
## WORKS...
irb(main):068:0> p videoListYaml.select("Videos//price").transform
[#<YAML::Syck::DomainType:0x2be0888 @value="22.45",
@type_id="float#fix", @domai
n="yaml.org,2002">, #<YAML::Syck::DomainType:0x2be04e0 @value="19.72",
@type_id=
"float#fix", @domain="yaml.org,2002">]
## Why isn't this parsing out the two Float values for price?
#########################