diff --git a/ch09-writing-your-own-methods/ch9_improvedAskMethod.rb b/ch09-writing-your-own-methods/ch9_improvedAskMethod.rb new file mode 100644 index 000000000..290581dd1 --- /dev/null +++ b/ch09-writing-your-own-methods/ch9_improvedAskMethod.rb @@ -0,0 +1,17 @@ +def ask question + while true + puts question + reply = gets.chomp.downcase + + if (reply == "yes" || reply == "no") + if reply == "yes" + return true + else + return false + end + break + else + puts "Please answer 'yes' or 'no'." + end + end +end \ No newline at end of file diff --git a/ch09-writing-your-own-methods/ch9_modern_roman_numerals.rb b/ch09-writing-your-own-methods/ch9_modern_roman_numerals.rb new file mode 100644 index 000000000..f4e7c9a85 --- /dev/null +++ b/ch09-writing-your-own-methods/ch9_modern_roman_numerals.rb @@ -0,0 +1,36 @@ +def roman (n) + + thousands = (n / 1000) + hundreds = (n % 1000) / 100 + tens = (n % 100) / 10 + ones = (n % 10) + + roman = "M" * thousands + + if hundreds == 9 + roman = roman + "CM" + elsif hundreds == 4 + roman = roman + "CD" + else + roman = roman + "D" * (n % 1000 / 500) + roman = roman + "C" * (n % 500 / 100) + end + + if tens == 9 + roman = roman + "XC" + elsif tens == 4 + roman = roman + "XL" + else + roman = roman + "L" * (n % 100 / 50) + roman = roman + "X" * (n % 50 / 10) + end + + if ones == 9 + roman = roman + "IX" + elsif ones == 4 + roman = roman + "IV" + else + roman = roman + "V" * (n % 10 /5) + roman = roman + "I" * (n % 5 /1) + end +end diff --git a/ch09-writing-your-own-methods/ch9_old_school_roman_numerals.rb b/ch09-writing-your-own-methods/ch9_old_school_roman_numerals.rb new file mode 100644 index 000000000..268347cd0 --- /dev/null +++ b/ch09-writing-your-own-methods/ch9_old_school_roman_numerals.rb @@ -0,0 +1,15 @@ +def roman n + roman_numeral = "" + + roman_numeral = roman_numeral + "M" * (n / 1000) + roman_numeral = roman_numeral + "D" * (n%1000 / 500) + roman_numeral = roman_numeral + "C" * (n%500 / 100) + roman_numeral = roman_numeral + "L" * (n%100 / 50) + roman_numeral = roman_numeral + "X" * (n%50 / 10) + roman_numeral = roman_numeral + "V" * (n%10 / 5) + roman_numeral = roman_numeral + "I" * (n%5 / 1) + + return roman_numeral +end + +