YAML Question: Using YAML::YamlNode#transform Method to get float values?

Discussion in 'Ruby' started by RubyQuestions, Dec 3, 2003.

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

    #########################
     
    RubyQuestions, Dec 3, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    636
  2. k3n3dy
    Replies:
    15
    Views:
    997
    dan2online
    Apr 20, 2006
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,562
    James Kanze
    Oct 8, 2009
  4. Paul Battley

    YAML.dump/YAML.load bug

    Paul Battley, Aug 3, 2005, in forum: Ruby
    Replies:
    0
    Views:
    182
    Paul Battley
    Aug 3, 2005
  5. Eric Promislow
    Replies:
    4
    Views:
    212
    Eric Promislow
    Oct 31, 2006
Loading...

Share This Page