On Behalf Of Erik Boling:
# Although, im not sure how databases work.. maybe its faster
if you have data. a database is the way to go. the fundamental idea is that data and code should be separate. data should not be seen by the programmer, only knowledge of its structure. Your program in turn, will be structured too. You'll have to create a data entry program separate from your report program. The data entry program will be in charged of putting the data into the database. But anyway, in your case, just relax, go slowly (since you'll be learning ruby also). a database is another beast to tame and you'll be learning new things too specially on normalization and indexes (there are many databases, you'll have to scout it yourself, a quick list are: pstore, dbm/berkelydb, kirbybase, sqlite, mysql, postgresql, etc.. ). i'll show you another scheme using yaml... see below...
# than hashes. You idea is pretty good, I would ajust it a bit
# to make it easier to read. Instead of listing all the prices
# and things just simply put *for example* strawberries: vendor#1.
# What if you saped the vendor and the pricing in you hash? that way
# you could just copy ans paste the prices into it, as long as the
# prices are in the correct order.
maybe you can show us how you want the data structured/formatted. do not worry, as long as you can read it, it is structured.
as said, i'm going to show you another style. The data will be structured/stored by vendor, then by product, this assumes like "I am vendor#x, and this is my a price list of all products i'm selling". But the report will rotate it so that you get product pricing w corresponding vendors, wc assumes like "I am the owner. I need a list of my products w corresponding pricing sorted for each vendor provider". In this example, the data is stored separate on a yaml text file (just read on yaml, it's highly documented).
C:\family\ruby>cat dbpricing.yaml
···
---
vendor3:
product1: 90
product2: 110
vendor1:
product1: 110
product2: 100
product3: 105
vendor2:
product1: 115
product2: 110
product3: 105
product4: 105
C:\family\ruby>cat test2.rb
# let's load our data from our yaml database
# the yaml data is just a flat text file
# so you can edit it using any text editor
# just be mindful of the format
require 'yaml'
dbpricing = YAML.load(File.open("dbpricing.yaml"))
# then print it
# ppp stands for pricing per product
puts "------------------"
puts "Vendors Price List"
puts "------------------"
dbpricing.each do |vendor, ppp|
puts "Vendor: #{vendor}"
ppp.each do |product,price|
puts " #{product}: #{price}"
end
end
# now, let's build another pricing db
# which is product centric
# for each product, it will list the vendors
# selling that product and their corresponding prices
# vendors are sorted by pricing
dbpricing2 = Hash.new{|h,k| h[k]={} }
dbpricing.each do |vendor, ppp|
ppp.each do |product,price|
dbpricing2[product][vendor] = price
end
end
# then print it
# ppv stands for pricing per vendor
puts
puts "--------------"
puts "Our Price List"
puts "--------------"
dbpricing2.each do |product, ppv|
puts "Product: #{product}"
ppv.sort{|v1,v2| v1[1] <=> v2[1] }.each do |pair|
puts " #{pair[0]}: #{pair[1].to_s.rjust(4)}"
end
end
C:\family\ruby>ruby test2.rb
------------------
Vendors Price List
------------------
Vendor: vendor3
product1: 90
product2: 110
Vendor: vendor1
product1: 110
product2: 100
product3: 105
Vendor: vendor2
product1: 115
product2: 110
product3: 105
product4: 105
--------------
Our Price List
--------------
Product: product1
vendor3: 90
vendor1: 110
vendor2: 115
Product: product2
vendor1: 100
vendor3: 110
vendor2: 110
Product: product3
vendor1: 105
vendor2: 105
Product: product4
vendor2: 105
C:\family\ruby>
hope that helps.
kind regards -botp