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

  • SEARCH
  • Home
  • 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 843045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:03:44+00:00 2026-05-15T06:03:44+00:00

I’m having trouble deserializing a ruby class that I wrote to YAML. Where I

  • 0

I’m having trouble deserializing a ruby class that I wrote to YAML.

Where I want to be

I want to be able to pass one object around as a full ‘question’ which includes the question text, some possible answers (For multi. choice) and the correct answer. One module (The encoder) takes input, builds a ‘question’ class out of it and appends it to the question pool. Another module reads a question pool and builds an array of ‘question’ objects.

Where I am currently

Sample Question Pool

--- |
 --- !ruby/object:MultiQ 
 a: "no"
 answer: "no"
 b: "no"
 c: "no"
 d: "no"
 text: "yes?"

Encoder dump to YAML file. Object is a MultiQ filled up with input. (See below.)

def dump(file, object)
  File.open(file, 'a') do |out|
    YAML.dump(object.to_yaml, out)
  end
  object = nil
end

MultiQ Class definition

class MultiQ
  attr_accessor :text, :answer, :a, :b, :c, :d

  def initialize(text, answer, a, b, c, d)
    @text = text
    @answer = answer
    @a = a
    @b = b
    @c = c
    @d = d
  end
end

The decoder (I’ve been trying different things, so what’s here wasn’t my first or best guess. But I’m at a loss and the documentation doesn’t really explain things thoroughly enough.)

  File.open( "test_set.yaml" ) do |yf|
    YAML.load_documents( yf ) { |item|
      new = YAML.object_maker( MultiQ, item)
      puts new
    }
  end

Questions you can answer

  1. How do I achieve my goal? What methods should I use, between parsing, loading files or documents, to successfully deserialize a Ruby class? I’ve already looked over the YAML Rdoc, and I didn’t absorb very much, so please don’t just link me to it.
  2. What other methods would you suggest using? Is there a better way to store questions like this? Should I be using document db, relational db, xml? Some other format?
  • 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-05-15T06:03:44+00:00Added an answer on May 15, 2026 at 6:03 am

    When you write to yaml, you don’t need to first call to_yaml, just pass the object itself to YAML.dump( object )

    This probably led you into other problems because the output of to_yaml was a string.. and the YAML.dump actually wrote your object as a string to to the file (that’s why you have an initial “– |” line. Anything code loading that file would load that data as a string.

    Load a single object like this:

    File.open( 'test_set.yaml', 'r') { |fh|  mq_loaded = YAML.load( fh ) }
    

    The “new” you’re using is generally confusing because new is a keyword.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.