Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:59:14+00:00 2026-06-13T04:59:14+00:00

I’m trying to build some basic object-persistence (save game, load game) into my sandbox

  • 0

I’m trying to build some basic object-persistence (save game, load game) into my sandbox app.

I’m using YAML to dump objects to a file, and then I want to recover them. I have two different classes of instantiated objects, and they get written like this, which all seems fine:

---
- !ruby/object:Game::Room
    reference: :largecave
    name: Large cave
    description: a large empty cave
    connections:
    :west: :smallcave
    :south: :waterfall
- !ruby/object:Game::Room
    reference: :smallcave
    name: Small cave
    description: a small cave
    connections:
    :east: :largecave
- !ruby/object:Game::Room
    reference: :waterfall
(... etc)
---
- !ruby/object:Game::GameObject
    reference: :key
    name: Key
    description: A small key
    room: :smallcave
- !ruby/object:Game::GameObject
    reference: :bowl
(....etc)

The problem comes trying to recover and populate the two different arrays of objects:

YAML.load(datastore.read)

works fine but is only good for one class of object, and I’ve tried the select method, like this, but without success:

@rooms = datastore.select("!ruby/object:Game::Room")
@objects = datastore.select("!ruby/object:Game::GameObject") 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:59:15+00:00Added an answer on June 13, 2026 at 4:59 am

    As a suggestion for data-usability, I’d recommend creating some methods for your classes to generate hashes of the contents of an object. That way you can store YAML representations of hashes, which are universal, rather than Ruby objects, which are only usable in Ruby, not other languages.

    I’d forgo creating multiple documents in your YAML file and would use something like:

    require 'pp'
    require 'yaml'
    
    places =   [
      {
        reference: :largecave,
        name: 'Large cave',
        description: 'a large empty cave',
        connections: {
          west: :smallcave,
          south: :waterfall
        }
      },
      {
        reference: :smallcave,
        name: 'Small cave',
        description: 'a small cave',
        connections: {
          east: :largecave
        }
      },
      {
        reference: :waterfall
      }
    ]
    
    things = [
      {
        reference: :key,
        name: 'Key',
        description: 'A small key',
        room: :smallcave
      },
      {
        reference: :bowl
      }
    ]
    
    data = [
      places,
      things
    ]
    
    puts data.to_yaml
    

    Which outputs:

    ---
    - - :reference: :largecave
        :name: Large cave
        :description: a large empty cave
        :connections:
          :west: :smallcave
          :south: :waterfall
      - :reference: :smallcave
        :name: Small cave
        :description: a small cave
        :connections:
          :east: :largecave
      - :reference: :waterfall
    - - :reference: :key
        :name: Key
        :description: A small key
        :room: :smallcave
      - :reference: :bowl
    

    Looking at the data after a round-trip through YAML-land:

    places, things = YAML.load(data.to_yaml)
    
    => [[{:reference=>:largecave,
      :name=>"Large cave",
      :description=>"a large empty cave",
      :connections=>{:west=>:smallcave, :south=>:waterfall}},
      {:reference=>:smallcave,
      :name=>"Small cave",
      :description=>"a small cave",
      :connections=>{:east=>:largecave}},
      {:reference=>:waterfall}],
    [{:reference=>:key,
      :name=>"Key",
      :description=>"A small key",
      :room=>:smallcave},
      {:reference=>:bowl}]]
    
    pp places
    
    [{:reference=>:largecave,
      :name=>"Large cave",
      :description=>"a large empty cave",
      :connections=>{:west=>:smallcave, :south=>:waterfall}},
    {:reference=>:smallcave,
      :name=>"Small cave",
      :description=>"a small cave",
      :connections=>{:east=>:largecave}},
    {:reference=>:waterfall}]
    
    pp things
    
    [{:reference=>:key,
      :name=>"Key",
      :description=>"A small key",
      :room=>:smallcave},
    {:reference=>:bowl}]
    

    You could also take advantage of YAML::Store and do it like:

    require 'yaml/store'
    require 'pp'
    
    store = YAML::Store.new('objects.store')
    store.transaction do
      store['places'] = places
      store['things'] = things
    end
    
    places = things = nil
    
    store.transaction(true) do
      places = store['places']
      things = store['things']
    end
    
    pp places
    pp things
    

    Which looks like:

    [{:reference=>:largecave,
      :name=>"Large cave",
      :description=>"a large empty cave",
      :connections=>{:west=>:smallcave, :south=>:waterfall}},
    {:reference=>:smallcave,
      :name=>"Small cave",
      :description=>"a small cave",
      :connections=>{:east=>:largecave}},
    {:reference=>:waterfall}]
    
    [{:reference=>:key,
      :name=>"Key",
      :description=>"A small key",
      :room=>:smallcave},
    {:reference=>:bowl}]
    

    YAML::Store will create your YAML file on disk for you:

    ---
    places:
    - :reference: :largecave
      :name: Large cave
      :description: a large empty cave
      :connections:
        :west: :smallcave
        :south: :waterfall
    - :reference: :smallcave
      :name: Small cave
      :description: a small cave
      :connections:
        :east: :largecave
    - :reference: :waterfall
    things:
    - :reference: :key
      :name: Key
      :description: A small key
      :room: :smallcave
    - :reference: :bowl
    

    Which is basically a hash, where places and things are the keys to the embedded data.

    Storing the configuration of “Adventure” in a YAML file might become unwieldy, so you might want to look into using something like Sequel and a SQLite database eventually. You could store the data in a common format still, but it’d also be set up for fast random access and wouldn’t need to be contained in memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.