My First Program


Sat down last night and decided to use my new knowledge of Ruby (I’ve been taking the course over at Codeacademy) to write a little program. It’s nothing fancy…like, seriously, it is just a few chicken scratches of code…but it is my first functioning program. I wrote it without any outside assistance save for the bit where I had to convert a string into a fixed number with .to_f.

my_tax = {
“AL” => 0.04,
“AK” => 0,
“AZ” => 0.056,
“AR” => 0.065,
“CA” => 0.075,
“CO” => 0.029,
“CT” => 0.0635,
“DE” => 0,
“DC” => 0.0575,
“FL” => 0.06,
“GA” => 0.04,
“HA” => 0.04,
“ID” => 0.06

print “What state do you live in?”
my_state = gets.chomp

print “How much does it cost?”
my_cost = gets.chomp

sales_tax = my_tax[my_state]
sales_tax_pct = sales_tax * 100

total_cost = (sales_tax.to_f * my_cost.to_f) + my_cost.to_f

puts “The sales tax is #{sales_tax_pct}% and the total cost is $#{total_cost}.”

That’s it. Just a simple sales tax calculator that creates a hash, requests user input, and then compares the user input against the hash to produce an equation that would add sales tax to a purchase price depending on what state you live in.

I’ll keep tweaking it…fill out the rest of the states…add an if/else statement to make sure people are putting in a real state…let me know what else you think I should try in the comments below.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s