So I've seen questions on here about how to write to an external text file etc. For example to write my hash to another file I put:
hash = {
Key1: Value1,
Key2: Value2
}
open(FileToWriteTo, 'w') do |f|
hash.each { |key, value| f.puts "#{key}: #{value}" }
But what I'd like to achieve is if I run the program and add something to my hash list, then the next time I run and display the hash, the new addition will be there. Here's the code I'm using to add to my hash:
puts "Type 'add' to add an item to the hash"
choice = gets.chomp.downcase
case choice
when 'add'
puts "What do you want to add?"
addition = gets.chomp
if hash[addition.to_sym].nil?
puts "What value will #{addition} have? (integer)"
add_value = gets.chomp
hash[addition.to_sym] = add_value.to_i
puts "#{addition} has been added with a value of #{value}."
else
puts "That item already exists! Its value is #{hash[addition.to_sym]}."
end
So if I add the item, rerun the program and choose to display instead of add, how should I get the last addition to show. Thanks.