-1

I have a small class which is for a character and we can assign to it from outside the class.

I need to know how I can dump all the information in that class into another that can be used to create a YAML file.

require "yaml"

module Save
  filename = "data.yaml"
  character = []
  sex = []
  race = []
  stats = [Str=[], Dex=[], Con=[], Int=[], Wis=[], Cha=[]]
  inventory = []
  saving_throws = [fortitude=[], reflex=[], will=[]]
  #Armor Class, Flat footed Armor Class, and Touch armor Class
  armor_class = [ac=[], fac=[], tac=[]]
  armor_worn = [head=[], eyes=[], neck=[], shoulders=[], body=[], torso=[],     arms_wrists=[], hands=[], ring1=[], ring2=[], waist=[], feet=[]] 
  money = []
  god = []
  speciality_school = [] #wizard
  companion = [] #also used for familirs and psicrystals
  skills = []
  class_race_traits = []
  feats = []
  languages = []

  program_data = {
    character: character,
    sex: sex,
    race: race,
    stats: stats,
    inventory: inventory,
    saving_throws: saving_throws,
    armor_class: armor_class,
    armor_worn: armor_worn,
    mony: money,
    god: god,
    speciality_school: speciality_school,
    companion: companion,
    skills: skills,
    class_race_traits: class_race_traits,
    feats: feats,
    languages: languages
  }

  File.write filename, YAML.dump(program_data)
end

This is the code I want to use to obtain the user content from the player:

class Character
  attr_reader :name, :race, :description

  def initialize (name, race, description)
    @name = name
    @race = race
    @description = description  
  end
end

def prompt
  print "Enter Command >"
end

puts "What is your name?"
prompt; name = gets.chomp.downcase

puts "What is your race?"
prompt; race = gets.chomp.downcase

puts "What do you look like?"
prompt; desc = gets.chomp.downcase

player_one = Character.new(name, race, desc)
puts player_one

I'm stuck on how to load it back and refill the character content to make it continue where the player left off.

jmarkmurphy
  • 11,030
  • 31
  • 59
  • 1
    This is very difficult to read, and I'm not sure what you think you're doing with code like `armor_class = [ac=[], fac=[], tac=[]]` – Dave Newton Jan 29 '16 at 19:46
  • I'm not sure this question is precise enough for Stackoverflow. Try to learn the base of what your doing, and narrow the focus for another question. That would help the people who can answer. – Jonathan Allard Jan 29 '16 at 19:59
  • You're off to an okay start. Did you mean to put the code that's in the Save module inside a method, though? And it's not clear why you're initializing every variable to an empty array (`[]`). Also, variable names that start with a capital letter (e.g. `Str`, `Dex`) are constants in Ruby, which probably isn't what you want. I suggest working on your code a bit longer until you have a more specific question to ask. – Jordan Running Jan 29 '16 at 20:42
  • okay thank you. all for you input. For the most part i want everything about the player saveable. I just don't know what i needed to pull it from the class. The empty arrays are where i want the information to go when save is done. for example you decide to save and the save module will search out all the information in the list and saves it in yaml format. If i am still not clear then i'll let this be until i get more experience. – Havatr_Einar Jan 29 '16 at 21:32

1 Answers1

1

Meditate on this bit of fictional code:

require 'yaml'

SAVED_STATE_FILE = 'saved_state.yaml'

class User
  def initialize(name=nil, address=nil)
    @name = name
    @address = address
  end

  def to_h
    {
      'name'    => @name,
      'address' => @address
    }
  end

  def save
    File.write(SAVED_STATE_FILE, self.to_h.to_yaml)
  end

  def reload
    state = YAML.load_file(SAVED_STATE_FILE)
    @name, @address = state.values
  end
end

We can create a new user with some properties:

user = User.new('Popeye', '123 E. Main St.')
# => #<User:0x007fe361097058 @name="Popeye", @address="123 E. Main St.">

To write that information to a file you should probably start by using YAML, which results in a very readable output and is readable by many different languages, making the data file reusable. A hash results in a very readable output:

user.to_h
# => {"name"=>"Popeye", "address"=>"123 E. Main St."}
user.to_h.to_yaml
# => "---\nname: Popeye\naddress: 123 E. Main St.\n"

Save the YAML serialized hash:

user.save

Create a new version of the user without any state:

user = User.new
# => #<User:0x007fe361094a88 @name=nil, @address=nil>

Load the saved information from the file back into the blank object:

user.reload

Which results in:

user
# => #<User:0x007fe361094a88 @name="Popeye", @address="123 E. Main St.">

That will give you enough to work from.

Your current code isn't going to work well though; I'd recommend reading some tutorials about Ruby classes and modules, as a Module isn't what you want, at least for your initial code.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • thank you for this and i will definitely read up on that as well as study this code. I have one last question for you before we continue on. Is there a way to serialize all this as a uuid, or would that make it to complicated for a person who is new to ruby like me? – Havatr_Einar Jan 29 '16 at 23:15
  • A UUID is a totally different question. Don't extend questions, especially using comments as it makes it very hard to follow, and even harder for others searching for that information to find it in a question. – the Tin Man Jan 31 '16 at 20:46