From d77e5d7f2337353816bbf58a6d7a8179621f3632 Mon Sep 17 00:00:00 2001 From: Michael Kohl Date: Mon, 3 May 2010 19:30:24 +0200 Subject: [PATCH] Cleaning up, fixing typos --- coreruby/exercise_set1/e0104ageinseconds.rb | 8 ++++---- coreruby/exercise_set1/e0105minutesinayear.rb | 4 +--- coreruby/exercise_set2/e0202convert.rb | 4 ++-- coreruby/exercise_set2/e0204ftoc.rb | 6 +++--- coreruby/exercise_set3/e0301splitstring.rb | 13 +++---------- coreruby/exercise_set3/e0303leapyearcheck.rb | 6 +++--- coreruby/exercise_set3/e0304minutesperyear.rb | 11 ++++++++++- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/coreruby/exercise_set1/e0104ageinseconds.rb b/coreruby/exercise_set1/e0104ageinseconds.rb index da5cbbe..d868bda 100644 --- a/coreruby/exercise_set1/e0104ageinseconds.rb +++ b/coreruby/exercise_set1/e0104ageinseconds.rb @@ -3,8 +3,8 @@ Displays the age (in years) of a person who is 979000000 seconds old. There are 31536000 seconds in a non leap year =end -AgeInSeconds = 979000000 -SecondsPerYear = 60 * 60 * 24 * 365 -age_in_years = AgeInSeconds / SecondsPerYear.to_f +AGE_IN_SECONDS = 979_000_000 +SECONDS_PER_YEAR = 60.0 * 60 * 24 * 365 +age_in_years = AGE_IN_SECONDS / SECONDS_PER_YEAR -puts "979000000 seconds are #{"%.2f" % age_in_years} years." +puts "979000000 seconds are %.2f years." % age_in_years diff --git a/coreruby/exercise_set1/e0105minutesinayear.rb b/coreruby/exercise_set1/e0105minutesinayear.rb index fbbf7fb..314eeb6 100644 --- a/coreruby/exercise_set1/e0105minutesinayear.rb +++ b/coreruby/exercise_set1/e0105minutesinayear.rb @@ -3,7 +3,5 @@ Calculates the minutes in a year 60 minutes/hour - 24 hours/day - 365 days/year =end -MinutesInYear = 60 * 24 * 365 - -puts "There are #{MinutesInYear} minutes in a year" +puts "There are #{60 * 24 * 365} minutes in a year" diff --git a/coreruby/exercise_set2/e0202convert.rb b/coreruby/exercise_set2/e0202convert.rb index 78c3a82..e6cecfe 100644 --- a/coreruby/exercise_set2/e0202convert.rb +++ b/coreruby/exercise_set2/e0202convert.rb @@ -1,7 +1,7 @@ =begin - Function to convert temperatures from Fahreheit to Celsius. + Function to convert temperatures from Fahrenheit to Celsius. Dies if temp_in_fahrenheit is not a number. =end def convert(temp_in_fahrenheit) - (temp_in_fahrenheit - 32) * 5 / 9 + (temp_in_fahrenheit - 32) / 1.8 end diff --git a/coreruby/exercise_set2/e0204ftoc.rb b/coreruby/exercise_set2/e0204ftoc.rb index d460e23..1a42bd4 100644 --- a/coreruby/exercise_set2/e0204ftoc.rb +++ b/coreruby/exercise_set2/e0204ftoc.rb @@ -6,13 +6,13 @@ =end def convert(temp_in_fahrenheit) - (temp_in_fahrenheit - 32) * 5 / 9 + (temp_in_fahrenheit - 32) / 1.8 end print 'Please enter temperature in Fahrenheit: ' STDOUT.flush -temp_to_convert = gets.to_f -puts "#{temp_to_convert} degress Fahrenheit are #{"%.2f" % convert(temp_to_convert)} degrees Celsius." +input_temp = gets.to_f +puts "%.2fF = %.2fC." % [input_temp, convert(input_temp)] diff --git a/coreruby/exercise_set3/e0301splitstring.rb b/coreruby/exercise_set3/e0301splitstring.rb index f0f7b82..1135dd6 100644 --- a/coreruby/exercise_set3/e0301splitstring.rb +++ b/coreruby/exercise_set3/e0301splitstring.rb @@ -2,20 +2,13 @@ =begin Breaks a multiline string up into individual lines and outputs them including line numbers. -=end - -s="Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n" -s.split.each_with_index do |line, line_count| - puts "Line #{line_count + 1}: #{line}" -end - -=begin - Ruby 1.9: - s.each_line.with_index { |l, i| puts "Line #{i+1}: #{l}"} + Needs Ruby 1.9 =end +s="Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n" +s.each_line.with_index { |line, i| puts "Line #{i+1}: #{line}" } diff --git a/coreruby/exercise_set3/e0303leapyearcheck.rb b/coreruby/exercise_set3/e0303leapyearcheck.rb index 3472bf6..b4bd833 100644 --- a/coreruby/exercise_set3/e0303leapyearcheck.rb +++ b/coreruby/exercise_set3/e0303leapyearcheck.rb @@ -4,12 +4,12 @@ =end def leap_year?(year) - (( year % 4 == 0 ) && (year % 100 != 0 )) || year % 400 == 0 + (year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 end print 'Please enter the year to be checked: ' STDOUT.flush -year_to_check = gets.to_i +input_year = gets.to_i -puts "%s:#{' not' unless leap_year?(year_to_check)} a leap year" % year_to_check +puts "#{input_year}:#{' not' unless leap_year?(input_year)} a leap year" diff --git a/coreruby/exercise_set3/e0304minutesperyear.rb b/coreruby/exercise_set3/e0304minutesperyear.rb index 03417de..8e0423f 100644 --- a/coreruby/exercise_set3/e0304minutesperyear.rb +++ b/coreruby/exercise_set3/e0304minutesperyear.rb @@ -1,7 +1,16 @@ #!/usr/bin/ruby +=begin + Write a method leap_year?. It should accept a year value from the user, + check whether it's a leap year, and then return true or false. + + With the help of this method calculate and display the number of minutes + in a leap year or non-leap year. + + Note: a century year is a leap year only if it is divisible by 4. +=end def leap_year?(year) - (( year % 4 == 0 ) && (year % 100 != 0 )) || year % 400 == 0 + (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 end print 'Please enter a year: '