Created
October 24, 2009 17:00
-
-
Save cannikin/217625 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# If you have a ratio of two numbers that you want to scale up or down you can | |
# use this command line tool to provide the three known parts and it will | |
# calculate the fourth. | |
# | |
# Usage | |
# | |
# ratio a/b x/y | |
# | |
# Where a, b and y are known numbers and x is the one you want to calculate | |
# for. For example lets say you are buying a new HDTV. The ratio of a widescreen | |
# TV is 16:9. You have a home entertainment center that will only allow a TV | |
# a maximum of 50" wide. How tall will that TV be? | |
# | |
# ratio 16/9 50/x | |
# => 28.125 | |
# | |
# So a TV screen that is 50" wide would be 28 1/8" tall. | |
if !ARGV.size == 2 || !ARGV.first.match('/') || !ARGV.last.match('/') | |
puts "\nInvalid Ratio\n ratio 16/9 33/x\n\n" | |
else | |
top1, bottom1 = ARGV.first.split('/') | |
top2, bottom2 = ARGV.last.split('/') | |
if top1 == 'x' | |
puts (top2.to_f * bottom1.to_f / bottom2.to_f).to_s | |
elsif top2 == 'x' | |
puts (top1.to_f * bottom2.to_f / bottom1.to_f).to_s | |
elsif bottom1 == 'x' | |
puts (top1.to_f * bottom2.to_f / top2.to_f).to_s | |
elsif bottom2 == 'x' | |
puts (top2.to_f * bottom1.to_f / top1.to_f).to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment