Reputation:
I have a hash that looks as follows:
character = {"Name: " => "#$name", "Weapon: " => "#$weapon", "Armor: " => "#$armor"}
I want to print each key and each value to a file so that it looks something like this.
Name: Templar
Weapon: Sword
Armor: Heavy Armor
I would like to use a basic method so that I understand what is going on. I've read that there are some modules that do this for you, such as Marshal, but I would like a basic method that involves beginner-level code.
Upvotes: 0
Views: 2031
Reputation: 988
This should work for you. I would recommend reading up more on Files and iterating through hashes yourself first though.
yourfile = "/some/path/file.txt"
File.open(yourfile, 'w') do |file|
character.each{ |k, v| file.write("#{k}: #{v}\n") }
end
Upvotes: 4