Skip to content

Instantly share code, notes, and snippets.

@mattetti
Forked from Lara-zz/Hash.rb
Created June 1, 2009 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattetti/121875 to your computer and use it in GitHub Desktop.
Save mattetti/121875 to your computer and use it in GitHub Desktop.
def products
@products ||= { 827 => {'product' => "ARM", 'price' => 349.99},
199 => {'product' => "LEG", 'price' => 224.75},
776 => {'product' => "FIRSTBORN", 'price' => 1499.95},
222 => {'product' => "LIFESAVINGS", 'price' => 49.99},
811 => {'product' => "RETIREMENT", 'price' => 49.99}
}
end
def print_out(string)
STDOUT << string
end
def products
@products ||= { 827 => {'name' => "ARM", 'price' => 349.99},
199 => {'name' => "LEG", 'price' => 224.75},
776 => {'name' => "FIRSTBORN", 'price' => 1499.95},
222 => {'name' => "LIFESAVINGS", 'price' => 49.99},
811 => {'name' => "RETIREMENT", 'price' => 49.99}
}
end
puts
print_out "1. Add a product
2. Update a product
3. Delete a product
4. View all products
5. View names that start with a letter you choose
6. View highest-priced product and lowest-priced product
7. View sum of all product prices
8. Quit
Choose an option: "
choice = gets.to_i
def generate_key
key = rand(998) + 1
while products.has_key?(key)
key = rand(998) + 1
end
key
end
def add_product
print_out "Enter a product name: "
# $stdout.flush
name = gets.chomp.upcase
print_out "Enter the product price using a decimal (as in 44.99), with no dollar sign: "
price = gets.chomp.to_f
key = generate_key
products[key] = {'name' => "#{name}", 'price' => price}
puts "You have entered a new product called #{name} with an assigned key of #{key}."
puts products.inspect.upcase
end
case choice
when 1
add_product
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment