Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/ruby
- #notice that this script uses 3 different kinds of commenting mechanisms. Can you spot all three?
- #'puts' is like 'print' but also adds a newline
- puts "convert from mile or km?"
- #gets asks the user for input and saves it as a string in the local variable 'theScale'
- theScale = gets
- puts "how many?"
- #gets still saves the users input as a string, even though they input a number
- theNumber = gets
- =begin
- because 'theNumber' is a string and not an integer, we have to first convert 'theNumber' to an integer using '.to_i' in order to do math on it;
- then we '.chomp' on the variable 'theScale' so we can test it for equivalence.
- Without chomping, the string would have some extra bites on the end that would make our equivalence test fail
- =end
- theAnswer = (theNumber.to_i / 5.0 * 8) if theScale.chomp == "mile"
- theAnswer = (theNumber.to_i / 8.0 * 5) if theScale.chomp == "km"
- #find out more at http://learnrubyonthemac.com
- puts "The answer is #{theAnswer}" #try figuring out how to add the distance unit to the end! :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement