Best way to store data to be retrieved later

Ok so Im attempting my first ruby program. I am attempting to record
details for my work expenses such as date, mileage etc. Basically what I
am trying to do is record a separate set of data for each day. I have so
far tried reading and writing to a file and using Pstore and am able to
store individual days in the file and can get Pstore to store one day
but then it overwrites when used again.

I would eventually like to put this in a web app but have no rails
experience yet. I have submitted my code below. Am I on the right track
or is there a better way to store the data? Like I say its my first
program so apologies if it doesnt make sense or the question is too
vague.

Thanks.

Pstore is commented out while I tried the file way:

require “pstore”

#Variables

day = 0
clinic = “”
milegage_google = 0
milegage_actual = 0
total_miles = 0
num_Apps = 0
bonus = 0
data = PStore.new(“data.pstore”) #Creates new data store

day = Time.new

#Conditional statements

print "Enter Clinic: "
clinic = gets.chomp.to_s
print "Enter google mileage: "
mileage_google = gets.chomp.to_i
print "Enter actual mileage: "
mileage_actual = gets.chomp.to_i
print "Enter number of appointments seen today: "
num_Apps = gets.chomp.to_i

if num_Apps == 5
bonus = bonus + 20
elsif num_Apps == 6
bonus = bonus + 60
elsif num_Apps == 7
bonus = bonus + 100
else
end

open(‘data.txt’, ‘a’) { |f|
f.puts “#{day}”
f.puts “#{clinic}”
f.puts “#{mileage_google}”
f.puts “#{mileage_actual}”
f.puts “#{num_Apps}”
f.puts “#{bonus}”
}

#Write variable data to store - name_of_store[“Category”] = variable

=begin data.transaction do
data[“Day”] = “#{day}”
data[“Clinic”] = “#{clinic}”
data[“Google Miles”] = “#{mileage_google}”
data[“Actual Miles”] = “#{mileage_actual}”
data[“Appointments”] = “#{num_Apps}”
data[“Bonus”] = “#{bonus}”

data.commit #Changes wont take place until committed
end
end=
=begin puts “Day - #{day}”
puts “Clinic - #{clinic}”
puts “Google miles - #{mileage_google}”
puts “Actual Mileage - #{mileage_actual}”
puts “Appointments seen - #{num_Apps}”
puts “Bonus so far - #{bonus}”
=end

#Read data from store

=begin data.transaction do
data[“Day”]
data[“Clinic”]
data[“Google Miles”]
data[“Actual Miles”]
data[“Appointments”]
data[“Bonus”]
end

#Print read data (may not need to do this)

data.transaction do
puts data[“Day”]
puts data[“Clinic”]
puts data[“Google Miles”]
puts data[“Actual Miles”]
puts data[“Appointments”]
puts data[“Bonus”]
end
=end

I would turn your current code into a class.

I would also consider using yaml - it is much easier to store in yaml
isn’t it?

Thanks for your reply. I will give YAML a go. Any tips on how to set
this up. I have looked at a few tutorials that all seem to presume
everything is set up already. Is YAML able to create separate entires by
date rather than overwriting each time I run the program?

Thanks

Simon

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs