From 6226ec155fa5f885a18007b4e98b736e450a8453 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 12 Sep 2016 16:40:54 +0100 Subject: [PATCH 01/46] Committing chapter 8 refresher --- ch08-arrays.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ch08-arrays.rb diff --git a/ch08-arrays.rb b/ch08-arrays.rb new file mode 100644 index 000000000..ebe66878a --- /dev/null +++ b/ch08-arrays.rb @@ -0,0 +1,24 @@ +# Type as many words as we want (one word per line), continuing until we press Enter on an empty line +# Repeat the words back to us in alphabetical order. +# Test program thoroughly. + +words = [] +puts "Hello, why don't you type some lines? Enter one word per line. When you get fed up, press Enter on an empty line." + +userInputEmpty = false + +while userInputEmpty == false + + word = gets.chomp + + if word != "" + words.push word + puts "How about another word? Leave empty if you're done" + + else + userInputEmpty = true + end +end + +puts "And here is a lovely alphabetical list of your words!" +puts words.sort From 3e23448c1ff270af583024723ec492db19350056 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Tue, 13 Sep 2016 13:58:29 +0100 Subject: [PATCH 02/46] Committing the improved ask method --- ch09-writing-your-own-methods/ask.rb | 40 ++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..23e9a058d 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,39 @@ def ask question - # your code here -end \ No newline at end of file + 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 + + answer +end + +puts "Hello, and thank you for participating." +puts + +ask "Do you like eating tacos?" +ask "Do you like eating burritos?" +wets_bed = ask "Do you wet the bed?" +ask "Do you like eating chimichangas?" +ask "Do you like eating sopapillas?" +puts "Just a few more questions..." +ask "Do you like drinking horchata?" +ask "Do you like eating flautas?" + +puts +puts "DEBRIEFING" +puts "Thanks for taking part. Actually it wasn't about mexican food; it was about bed wetting." +puts +puts "And your answer to that question was..." +puts wets_bed From 0fcab9d156f99bb33856078a5f604eed5be35edc Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Tue, 13 Sep 2016 15:22:49 +0100 Subject: [PATCH 03/46] Adding chapter nine old school roman numerals --- .../old_school_roman_numerals.rb | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index ca6589f2d..68e8d5d3b 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,3 +1,55 @@ +# Write a method that when passed an integer between 1 and 3000 (or so) +# returns a string containing the proper old-school Roman numeral. + +# M = 1000 +# D = 500 +# C = 100 +# L = 50 +# X = 10 +# V = 5 +# I = 1 + +# Hint: use the integer division and modulus methods + +# How many times does each of those roman numerals factor into numberRequested? + +# Example: 1759 + +# 1 x 1000 = M +# 1 x 500 = D +# 2 x 100 = CC +# 1 x 50 = L +# 0 x 10 = no Xs +# 1 x 5 = V +# 4 x 1 = IIII + +# MDCCLVIIII + +# How many times does 1,000 go into it? Put that many Ms. +# How many times does 500 go into the remainder? Put that many Ds. +# How many times does 100 go into the remainder? Put that many Cs. +# How many times does 50 go into the remainder? Put that many Ls. +# How many times does 10 go into the remainder? Put that many Xs. +# How many times does 5 go into the remainder? Put that many Vs. +# How many times does 1 go into the remainder? Put that many Is. + def old_roman_numeral num - # your code here -end \ No newline at end of file + + numberMs = "M" * (num / 1000) + numberDs = "D" * ((num % 1000) / 500) + numberCs = "C" * ((num % 500) / 100) + numberLs = "L" * ((num % 100) / 50) + numberXs = "X" * ((num % 50) / 10) + numberVs = "V" * ((num % 10) / 5) + numberIs = "I" * ((num % 5) / 1) + + puts numberMs + numberDs + numberCs + numberLs + numberXs + numberVs + numberIs + +end + +old_roman_numeral 1759 +old_roman_numeral 3000 +old_roman_numeral 520 +old_roman_numeral 3621 +old_roman_numeral 860 +old_roman_numeral 16 From 8d811b26d7d399b80dd898929777f33d8bd80ae4 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Tue, 13 Sep 2016 15:31:56 +0100 Subject: [PATCH 04/46] Re-adding old school roman numerals taking my own tests out --- ch09-writing-your-own-methods/old_school_roman_numerals.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index 68e8d5d3b..6ca476caa 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -46,10 +46,3 @@ def old_roman_numeral num puts numberMs + numberDs + numberCs + numberLs + numberXs + numberVs + numberIs end - -old_roman_numeral 1759 -old_roman_numeral 3000 -old_roman_numeral 520 -old_roman_numeral 3621 -old_roman_numeral 860 -old_roman_numeral 16 From e6a468988c71993c48bda12f5ae8a9fff805d39d Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Tue, 13 Sep 2016 16:26:14 +0100 Subject: [PATCH 05/46] Adding the classic roman numerals and an updated old school roman numerals. --- .../old_school_roman_numerals.rb | 2 +- .../roman_numerals.rb | 55 ++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index 6ca476caa..7153aa0c2 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -43,6 +43,6 @@ def old_roman_numeral num numberVs = "V" * ((num % 10) / 5) numberIs = "I" * ((num % 5) / 1) - puts numberMs + numberDs + numberCs + numberLs + numberXs + numberVs + numberIs + return numberMs + numberDs + numberCs + numberLs + numberXs + numberVs + numberIs end diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..2fecf2422 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,54 @@ +# M = 1000 +# D = 500 +# C = 100 +# L = 50 +# X = 10 +# V = 5 +# I = 1 + def roman_numeral num - # your code here -end \ No newline at end of file + + number1000s = (num / 1000) + number100s = (num % 1000) / 100 + number10s = (num % 100) / 10 + number1s = (num % 10) / 1 + + numberDs = (num % 1000) / 500 + numberCs = (num % 500) / 100 + numberLs = (num % 100) / 50 + numberXs = (num % 50) / 10 + numberVs = (num % 10) / 5 + numberIs = (num % 5) / 1 + + result = "M" * number1000s + + if number100s == 9 + result = result + "CM" + elsif number100s == 4 + result = result + "CD" + else + result = result + "D" * numberDs + result = result + "C" * numberCs + end + + if number10s == 9 + result = result + "XC" + elsif number10s == 4 + result = result + "XL" + else + result = result + "L" * numberDs + result = result + "X" * numberXs + end + + if number1s == 9 + result = result + "IX" + elsif number1s == 4 + result = result + "IV" + else + result = result + "V" * numberVs + result = result + "I" * numberIs + end + + result + +end From 708afbf004db5aa534ced6d0478a6081762e45a4 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Tue, 13 Sep 2016 16:35:03 +0100 Subject: [PATCH 06/46] Fixed roman numerals issue. --- ch09-writing-your-own-methods/roman_numerals.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 2fecf2422..d8a91d9db 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -36,7 +36,7 @@ def roman_numeral num elsif number10s == 4 result = result + "XL" else - result = result + "L" * numberDs + result = result + "L" * numberLs result = result + "X" * numberXs end From c6ec3ea6975d9ef6de3069996045d65987ef52d1 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Wed, 14 Sep 2016 13:23:48 +0100 Subject: [PATCH 07/46] Adding sort.rb --- ch10-nothing-new/sort.rb | 48 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..67768c8c4 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,47 @@ +# Which of two words comes first in the dictionary (using <) +# Keep a list of already-sorted words, the other is a list of still-unsorted words +#Take our list of words, find the smallest word (that would come first in the dictionary) +# and stick it at the end of the alread-sorted list +# All of the other words go into the still unsorted list +# Then do the same thing still using the unsrted list instead of your original listFind the msallest words +# Move it to the sorted list +# Move the rest to the unsorted list +# Keep going until your still-unsorted list is empty + def sort arr - # your code here -end \ No newline at end of file + recursive_sort arr, [] +end + +def recursive_sort unsorted_array, sorted_array + + # If the unsorted array has no items in it, return the sorted array + if unsorted_array.length <= 0 + return sorted_array + end + + # Remove the last word from the unsorted array and put it into the variable smallest + smallest = unsorted_array.pop + + # Create a new array called still_unsorted to hold all the other words + still_unsorted = [] + + # For each word in the unsorted array, see if it's smaller than the 'smallest' word that you took earlier + # If it is, put the smallest word back into the array, and replace 'smallest' with this new word + # If word is not smaller than the smallest, then push this word into the still unsorted array + + unsorted_array.each do | word | + if word < smallest + still_unsorted.push smallest + smallest = word + else + still_unsorted.push word + end + end + + # Put the smallest word into the sorted array + sorted_array.push smallest + + # Now call the same recursive sort method to sort out the still_unsorted words + recursive_sort still_unsorted, sorted_array + +end From 8af79f4d2ae048e2414e7790344ab39a424a3ca8 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Wed, 14 Sep 2016 14:35:26 +0100 Subject: [PATCH 08/46] Adding my awesome shuffle.rb program and hoping for the best. --- ch10-nothing-new/shuffle.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..9597a06c9 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,23 @@ +# How would I shuffle an array in English? +# Take a random item from the given array and put it in the new array +# Keep going until all the items have moved from the given array into the new array + def shuffle arr - # your code here -end \ No newline at end of file + recursive_shuffle arr, [] +end + +def recursive_shuffle original_array, shuffled_array + + if original_array.length <= 0 + return shuffled_array + end + + # Remove the last item from the original array + randomWord = original_array.pop + + # Insert it in a random place in the shuffled array + shuffled_array.insert(rand(shuffled_array.length+1), randomWord) + + # Call the method again unti + recursive_shuffle original_array, shuffled_array +end From 88df535a870bfb9e4f44e1dbad3b8834259644f5 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Wed, 14 Sep 2016 15:20:57 +0100 Subject: [PATCH 09/46] Adding an awesome dictionary sort method. --- ch10-nothing-new/dictionary_sort.rb | 43 +++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..eadc70253 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,42 @@ +# Okay how do we solve this in English? +# For the purpose of comparison, we should just downcase everything +# But if it were capitalised to begin with, it ought to remain so + def dictionary_sort arr - # your code here -end \ No newline at end of file + recursive_sort arr, [] +end + +def recursive_sort unsorted_array, sorted_array + + # If the unsorted array has no items in it, return the sorted array + if unsorted_array.length <= 0 + return sorted_array + end + + # Remove the last word from the unsorted array and put it into the variable smallest + smallest = unsorted_array.pop + + # Create a new array called still_unsorted to hold all the other words + still_unsorted = [] + + # For each word in the unsorted array, see if it's smaller than the 'smallest' word that you took earlier + # If it is, put the smallest word back into the array, and replace 'smallest' with this new word + # If word is not smaller than the smallest, then push this word into the still unsorted array + + unsorted_array.each do | word | + + if word.downcase < smallest.downcase + still_unsorted.push smallest + smallest = word + else + still_unsorted.push word + end + end + + # Put the smallest word into the sorted array + sorted_array.push smallest + + # Now call the same recursive sort method to sort out the still_unsorted words + recursive_sort still_unsorted, sorted_array + +end From 15003c3786e00fe8fc602397a83f37398bd859f0 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Wed, 14 Sep 2016 22:50:04 +0100 Subject: [PATCH 10/46] Adding english_number.rb woohoo --- ch10-nothing-new/english_number.rb | 97 +++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..95a65ba4c 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,98 @@ def english_number number - # your code here + if number < 0 + return "Please enter a number that isn't negative." + end + + if number == 0 + return "zero" + end + + num_string = "" + + ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] + tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + + left = number + + write = left/1000000000000 + left = left - write*1000000000000 + + if write > 0 + trillions = english_number write + num_string = num_string + trillions + " trillion" + if left > 0 + num_string = num_string + " " + end + end + + write = left/1000000000 + left = left - write*1000000000 + + if write > 0 + billions = english_number write + num_string = num_string + billions + " billion" + if left > 0 + num_string = num_string + " " + end + end + + write = left/1000000 + left = left - write*1000000 + + if write > 0 + millions = english_number write + num_string = num_string + millions + " million" + if left > 0 + num_string = num_string + " " + end + end + + write = left/1000 + left = left - write*1000 + + if write > 0 + thousands = english_number write + num_string = num_string + thousands + " thousand" + if left > 0 + num_string = num_string + " " + end + end + + write = left/100 + left = left - write*100 + + if write > 0 + hundreds = english_number write + num_string = num_string + hundreds + " hundred" + if left > 0 + num_string = num_string + " " + end + end + + write = left/10 + left = left - write*10 + + if write > 0 + if ((write == 1) and (left > 0)) + num_string = num_string + teenagers[left-1] + left = 0 + else + num_string = num_string + tens_place[write-1] + end + + if left > 0 + num_string = num_string + " " + end + end + + write = left + left = 0 + + if write > 0 + num_string = num_string + ones_place[write-1] + end + + num_string + end From dd44275a9e274b8e8151b43222753bd2a97e5971 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Wed, 14 Sep 2016 23:06:29 +0100 Subject: [PATCH 11/46] Adding english_number.rb for the second time. --- ch10-nothing-new/english_number.rb | 95 ++++++++++++++---------------- 1 file changed, 43 insertions(+), 52 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index 95a65ba4c..e9587b28a 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -12,61 +12,45 @@ def english_number number ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + zillions = [['hundred',2], + ['thousand', 3], + ['million', 6], + ['billion', 9], + ['trillion', 12], + ['quadrillion', 15], + ['quintillion', 18], + ['sextillion', 21], + ['septillion', 24], + ['octillion', 27], + ['nonillion', 30], + ['decillion', 33], + ['undecillion', 36], + ['duodecillion', 39], + ['tredecillion', 42], + ['quattuordecillion', 45], + ['quindecillion', 48], + ['sexdicillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novembdecillion', 60], + ['vigintillion', 63], + ['googol', 100]] left = number - write = left/1000000000000 - left = left - write*1000000000000 - - if write > 0 - trillions = english_number write - num_string = num_string + trillions + " trillion" - if left > 0 - num_string = num_string + " " - end - end - - write = left/1000000000 - left = left - write*1000000000 - - if write > 0 - billions = english_number write - num_string = num_string + billions + " billion" - if left > 0 - num_string = num_string + " " - end - end - - write = left/1000000 - left = left - write*1000000 - - if write > 0 - millions = english_number write - num_string = num_string + millions + " million" - if left > 0 - num_string = num_string + " " - end - end - - write = left/1000 - left = left - write*1000 - - if write > 0 - thousands = english_number write - num_string = num_string + thousands + " thousand" - if left > 0 - num_string = num_string + " " - end - end - - write = left/100 - left = left - write*100 - - if write > 0 - hundreds = english_number write - num_string = num_string + hundreds + " hundred" - if left > 0 - num_string = num_string + " " + while zillions.length > 0 + zil_pair = zillions.pop + zil_name = zil_pair[0] + zil_base = 10 ** zil_pair[1] + write = left/zil_base + left = left - write*zil_base + + if write > 0 + prefix = english_number write + num_string = num_string + prefix + " " + zil_name + if left > 0 + num_string = num_string + " " + end end end @@ -96,3 +80,10 @@ def english_number number num_string end + +# puts english_number 937234827364876238423423487686234 +# puts english_number 234987234982398798423 +# puts english_number 23432 +# puts english_number 234 +# puts english_number 3487876879872340983495768762340834876827634234328745768762348768736458983745435 +# puts english_number 9898176572634765762999987987 From 22ec22f1f8a51f2f7e8c6a686fbddf5f066efd47 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Wed, 14 Sep 2016 23:12:55 +0100 Subject: [PATCH 12/46] Adding english_number.rb for the third time. --- ch10-nothing-new/english_number.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index e9587b28a..378814c7c 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -66,7 +66,7 @@ def english_number number end if left > 0 - num_string = num_string + " " + num_string = num_string + "-" end end @@ -84,6 +84,7 @@ def english_number number # puts english_number 937234827364876238423423487686234 # puts english_number 234987234982398798423 # puts english_number 23432 -# puts english_number 234 -# puts english_number 3487876879872340983495768762340834876827634234328745768762348768736458983745435 +puts english_number 99 +puts english_number 234 +puts english_number 109238745102938560129834709285360238475982374561034 # puts english_number 9898176572634765762999987987 From 8bf6b1aede81b9159ff272242c772ba72ced98ac Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Thu, 15 Sep 2016 23:51:33 +0100 Subject: [PATCH 13/46] Committing ninety_nine_bottles_of_beer.rb which I have totally nailed --- .../ninety_nine_bottles_of_beer.rb | 99 ++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..d9cb3aa2f 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,98 @@ -# your code here \ No newline at end of file +def english_number number + if number < 0 + return "Please enter a number that isn't negative." + end + + if number == 0 + return "zero" + end + + num_string = "" + + ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] + tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + zillions = [['hundred',2], + ['thousand', 3], + ['million', 6], + ['billion', 9], + ['trillion', 12], + ['quadrillion', 15], + ['quintillion', 18], + ['sextillion', 21], + ['septillion', 24], + ['octillion', 27], + ['nonillion', 30], + ['decillion', 33], + ['undecillion', 36], + ['duodecillion', 39], + ['tredecillion', 42], + ['quattuordecillion', 45], + ['quindecillion', 48], + ['sexdicillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novembdecillion', 60], + ['vigintillion', 63], + ['googol', 100]] + + left = number + + while zillions.length > 0 + zil_pair = zillions.pop + zil_name = zil_pair[0] + zil_base = 10 ** zil_pair[1] + write = left/zil_base + left = left - write*zil_base + + if write > 0 + prefix = english_number write + num_string = num_string + prefix + " " + zil_name + if left > 0 + num_string = num_string + " " + end + end + end + + write = left/10 + left = left - write*10 + + if write > 0 + if ((write == 1) and (left > 0)) + num_string = num_string + teenagers[left-1] + left = 0 + else + num_string = num_string + tens_place[write-1] + end + + if left > 0 + num_string = num_string + "-" + end + end + + write = left + left = 0 + + if write > 0 + num_string = num_string + ones_place[write-1] + end + + num_string + +end + +def bottles_of_beer num + while num > 0 + english_num = english_number num + puts english_num.capitalize + " bottles of beer on the wall, " + english_num + " bottles of beer!" + english_num = english_number num-1 + num -= 1 + if num == 0 + puts "Take one down, pass it around, no more bottles of beer on the wall" + else + puts "Take one down, pass it around, " + english_num + " bottles of beer on the wall" + end + end +end + +bottles_of_beer 99 From ee67c0fcee1a17554cf8b6b0a28f361c01d77308 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Fri, 16 Sep 2016 10:39:43 +0100 Subject: [PATCH 14/46] Adding safer picture downloading although I haven't really tested it! Fingers crossed. --- .../safer_picture_downloading.rb | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..7ae7caa54 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,36 @@ -# your code here \ No newline at end of file +pic_names = Dir["../*.{JPG,jpg}"] # Search in the parent directory + +puts "What would you like to call this batch?" +batch_name = gets.chomp + +puts +print "Downloading #{pic_names.length} files: " + +pic_number = 1 + +pic_names.each do |name| + print "." + new_name = if pic_number < 10 + "#{batch_name}0#{pic_number}.jpg" + else + "#{batch_name}#{pic_number}.jpg" + end + if File.exist? new_name + overwrite = gets.chomp "A file already exists with the name " + new_name + ". Do you want to overwrite? Type \"Yes\" or \"No\"." + if overwrite.downcase == "yes" + File.rename name, new_name + pic_number += 1 + elsif overwrite.downcase == "no" + puts "Skipping " + new_name + " because it already exists." + end + end +end + +puts +puts "Done, cutie!" + +# TO DO +# Add safety features to sure you never overwrite a file +# File.exist? (pass it a filename and it will return true or false) +# exit - kills your program right where it stands - good for spitting out an error message and then quitting +# ßSolve this in English first Frances! From c81f971f57c77d4375de2ff42b845e4cfffe7eba Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Fri, 16 Sep 2016 15:09:26 +0100 Subject: [PATCH 15/46] Adding playlist file. --- .../build_your_own_playlist.rb | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..1a69acdbd 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,36 @@ -# your code here \ No newline at end of file +# Search for music files +# Build you a playlist, shuffling the items +# Save it as an m3u file + +def shuffle arr + recursive_shuffle arr, [] +end + +def recursive_shuffle original_array, shuffled_array + + if original_array.length <= 0 + return shuffled_array + end + + # Remove the last item from the original array + random_element = original_array.pop + + # Insert it in a random place in the shuffled array + shuffled_array.insert(rand(shuffled_array.length+1), random_element) + + # Call the method again until all the elements have been shuffled + recursive_shuffle original_array, shuffled_array +end + +audio_files = Dir["../**/*.{MP3,mp3}"] # Search in the parent directory and all its subdirectories + +shuffled_files = [] +shuffled_files = shuffle audio_files + +playlist = "" + +shuffled_files.each do |filename| + playlist += filename + "\n" +end + +puts playlist From e8aa1e6c2ed201c459a1c4c3f4f80232dfa1550b Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 18:16:21 +0100 Subject: [PATCH 16/46] Build a better playlist --- .../build_a_better_playlist.rb | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..ded31f97c 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,38 @@ def music_shuffle filenames - # your code here + + filenames = filenames.sort + len = filenames.length + + 2.times do + l_idx = 0 # index of next card in left pile + r_idx = len/2 # index of next card in right pile + shuf = [] + + while shuf.length < len + if shuf.length%2 == 0 + shuf.push(filenames[r_idx]) + r_idx = r_idx + 1 + else + shuf.push(filenames[l_idx]) + l_idx = l_idx + 1 + end + end + + filenames = shuf + end + + arr = [] + cut = rand(len) # index of card to cut at + idx = 0 + + while idx < len + arr.push(filenames[(idx+cut)%len]) + idx = idx + 1 + end + + arr end + +songs = ['aa/bbb', 'aa/ccc', 'aa/ddd', 'AAA/xxxx', 'AAA/yyyy', 'AAA/zzzz', 'foo/bar'] + +puts(music_shuffle(songs)) From 5a483e2558d46d83de67bd4c14f2e3e9483047e7 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 18:16:52 +0100 Subject: [PATCH 17/46] Added improved build a playlist --- .../build_your_own_playlist.rb | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 1a69acdbd..75e77a6f0 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1,7 +1,3 @@ -# Search for music files -# Build you a playlist, shuffling the items -# Save it as an m3u file - def shuffle arr recursive_shuffle arr, [] end @@ -12,25 +8,32 @@ def recursive_shuffle original_array, shuffled_array return shuffled_array end - # Remove the last item from the original array random_element = original_array.pop - - # Insert it in a random place in the shuffled array shuffled_array.insert(rand(shuffled_array.length+1), random_element) - - # Call the method again until all the elements have been shuffled recursive_shuffle original_array, shuffled_array end -audio_files = Dir["../**/*.{MP3,mp3}"] # Search in the parent directory and all its subdirectories +audio_files = Dir["../**/*.{MP3,mp3}"] -shuffled_files = [] -shuffled_files = shuffle audio_files +if audio_files.length == 0 + puts "I'm sorry, I couldn't find any audio files in your collection." +else + puts "I found #{audio_files.length} audio files in your collection." + puts "What do you want to call your playlist?" + playlist_name = gets.chomp + playlist_name += ".m3u" -playlist = "" + shuffled_files = [] + shuffled_files = shuffle audio_files -shuffled_files.each do |filename| - playlist += filename + "\n" -end + playlist = "" -puts playlist + shuffled_files.each do |filename| + playlist += filename + "\n" + end + + File.open playlist_name, 'w' do |f| + f.write playlist + puts "I just wrote you a file called #{playlist_name}. Enjoy!" + end +end From 13074de7802d7406c51d2de82ebd5c0028f9035c Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 18:25:16 +0100 Subject: [PATCH 18/46] Slightly edited better playlist. --- ch11-reading-and-writing/build_a_better_playlist.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index ded31f97c..1df218d2e 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -16,9 +16,9 @@ def music_shuffle filenames shuf.push(filenames[l_idx]) l_idx = l_idx + 1 end - end + end - filenames = shuf + filenames = shuf end arr = [] From 528c98d65de3ec000c98e6900b8e6ae9bfcdd6ad Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 19:16:50 +0100 Subject: [PATCH 19/46] Adding some of the chapter 12 exercises. Getting there woohoo --- ch10-nothing-new/shuffle.rb | 23 ++++++++++++++++++- ch12-new-classes-of-objects/happy_birthday.rb | 21 ++++++++++++++++- .../one_billion_seconds.rb | 5 +++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index 9597a06c9..eab10598a 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -18,6 +18,27 @@ def recursive_shuffle original_array, shuffled_array # Insert it in a random place in the shuffled array shuffled_array.insert(rand(shuffled_array.length+1), randomWord) - # Call the method again unti + # Call the method again until all the elements have been shuffled recursive_shuffle original_array, shuffled_array end + + +#def shuffle arr # Chris Pine's shuffle method +# shuf = [] +# while arr.length > 0 +# rand_index = rand(arr.length) +# curr_index = 0 +# new_arr = [] +# +# arr.each do |item| +# if curr_index == rand_index +# shuf.push item +# else +# new_arr.push item +# end +# curr_index += 1 +# end +# arr = new_arr +# end +# shuf +#end diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..d1ec42a39 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,20 @@ -# your code here \ No newline at end of file +puts "What year were you born in?" +year = gets.chomp + +puts "What month were you born in? (write as a number please)" +month = gets.chomp + +puts "What day were you born?" +day = gets.chomp + +birthday = Time.gm(year, month, day) + +puts "Your birthday is " + birthday.to_s + +years_alive = ((Time.new - birthday) / 31536000).to_i + +puts "Here, have a spank for the #{years_alive} years you've been alive!" + +years_alive.times do + puts "SPANK!" +end diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..8f646ab12 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,4 @@ -# your code here \ No newline at end of file +# Figure out what second I was born +# Calculate my one billionth second birthday + +puts Time.gm(1979, 5, 11, 02, 01) + 1000000000 From a772ad44094865eabbd8528bcace0741902d071d Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 19:46:58 +0100 Subject: [PATCH 20/46] Adding that horrible roman to integer program. Hated it. --- ...party_like_its_roman_to_integer_mcmxcix.rb | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb b/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb index 037b6cb09..7727edcae 100644 --- a/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb +++ b/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb @@ -1,3 +1,31 @@ def roman_to_integer roman - # your code here -end \ No newline at end of file + # Reject strings which are not valid Roman numerals + + digit_vals = {"i" => 1, "v" => 5, "x" => 10, "l" => 50, "c" => 100, "d" => 500, "m" => 1000} + + total = 0 + prev = 0 + index = roman.length - 1 + + while index >= 0 + c = roman[index].downcase + index = index - 1 + val = digit_vals[c] + if !val + puts "This is not a valid roman numeral" + return + end + + if val < prev + val = val * -1 + else + prev = val + end + total = total + val + end + + total +end + +puts roman_to_integer "mcmxcix" +puts roman_to_integer "CCCLXV" From f7c8515c8739120740c4404b6a92227793cdc3ee Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 20:50:53 +0100 Subject: [PATCH 21/46] Adding birthday_helper.rb and birthdates.txt files --- ch12-new-classes-of-objects/birthdates.txt | 8 ++++++ .../birthday_helper.rb | 27 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 ch12-new-classes-of-objects/birthdates.txt diff --git a/ch12-new-classes-of-objects/birthdates.txt b/ch12-new-classes-of-objects/birthdates.txt new file mode 100644 index 000000000..ea3492642 --- /dev/null +++ b/ch12-new-classes-of-objects/birthdates.txt @@ -0,0 +1,8 @@ +Christopher Alexander, Oct 4, 1936 +Christopher Lambert, Mar 29, 1957 +Christopher Lee, May 27, 1922 +Christopher Lloyd, Oct 22, 1938 +Christopher Pine, Aug 3, 1976 +Christopher Plummer, Dec 13, 1927 +Christopher Walken, Mar 31, 1943 +The King of Spain, Jan 5, 1938 diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..072da5915 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,26 @@ -# your code here \ No newline at end of file +birth_dates = {} + +File.read('birthdates.txt').each_line do |line| + + line = line.chomp + first_comma = 0 + + while line[first_comma] != ',' && first_comma < line.length + first_comma += 1 + end + + name = line[0..(first_comma - 1)] + date = line[-12..-1] + + birth_dates[name] = date +end + +puts "Whose birthday would you like to know?" +name = gets.chomp +date = birth_dates[name] + +if date == nil + puts "Oooh, I don't know that one..." +else + puts date[0..5] +end From 79070979fecb0d1885fd5d49d610f9070fe924ab Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 21:09:55 +0100 Subject: [PATCH 22/46] Adding extend built in classes. --- ch13-creating-new-classes/extend_built_in_classes.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..e252a6616 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,9 @@ class Integer - # your code here -end \ No newline at end of file + def factorial + if self <=1 + 1 + else + self * (self-1).factorial + end + end +end From fd87546286c85d82275e7024d678ed3b5ed4bacb Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 21:46:31 +0100 Subject: [PATCH 23/46] Added the roman method extension to the Integer class. --- .../extend_built_in_classes.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index e252a6616..f54484a28 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -6,4 +6,17 @@ def factorial self * (self-1).factorial end end + + def to_roman + numberMs = "M" * (self / 1000) + numberDs = "D" * ((self % 1000) / 500) + numberCs = "C" * ((self % 500) / 100) + numberLs = "L" * ((self % 100) / 50) + numberXs = "X" * ((self % 50) / 10) + numberVs = "V" * ((self % 10) / 5) + numberIs = "I" * ((self % 5) / 1) + + return numberMs + numberDs + numberCs + numberLs + numberXs + numberVs + numberIs + + end end From 499dc38b07277408206eea3ad27e012c6b3c1634 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 22:32:43 +0100 Subject: [PATCH 24/46] Adding orange_tree.rb and hoping for the best --- ch13-creating-new-classes/orange_tree.rb | 160 ++++++++++++++++++++++- 1 file changed, 158 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..ff10f668d 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -5,7 +5,163 @@ # have the tree die after 25 years. # check out the rspec spec/ch13/orange_tree_spec.rb to see what strings we're looking for in the responses. - class OrangeTree - # your code here + + def initialize + @age = 0 + @height = 0 + @growth_rate = 0.4 + @age_first_fruit = 6 + @age_of_death = 25 + @orange_count = 0 + + puts "Your tree has been planted." + end + + def height + puts "Your tree is currently #{(@height = @age * @growth_rate).round(2)}m in height." + end + + def one_year_passes + @age += 1 + puts "Your tree is now #{@age}" + if @age == 25 + puts "Ah I'm sorry, your tree just died. But it lived till it was 25 and had a good life." + exit + end + puts + end + + def count_the_oranges + if @age < @age_first_fruit + @orange_count + puts "Your tree has no oranges." + else + @orange_count = (@height * rand(15..25)).to_i + puts "Your tree has #{@orange_count} oranges." + end + end + + def pick_an_orange + if @orange_count > 0 + puts "Mmm you've picked an orange. How yummy - it's very juicy." + @orange_count -= 1 + puts "You tree now has #{@orange_count} oranges left." + else + "You can't pick any oranges, as there aren't any I'm afraid. Try again in a year or so." + end + end + end + +=begin +tree = OrangeTree.new +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +tree.height +tree.count_the_oranges +tree.pick_an_orange +tree.one_year_passes +=end From 0e188570870a707c32fcf5fadc5adbe7ed8a931e Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 22:35:43 +0100 Subject: [PATCH 25/46] Adding orange_tree.rb with a few edits. --- ch13-creating-new-classes/orange_tree.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index ff10f668d..a829de7e2 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -15,18 +15,19 @@ def initialize @age_of_death = 25 @orange_count = 0 - puts "Your tree has been planted." + #puts "Your tree has been planted." end def height - puts "Your tree is currently #{(@height = @age * @growth_rate).round(2)}m in height." + @height = @age * @growth_rate + #puts "Your tree is currently #{(@height = @age * @growth_rate).round(2)}m in height." end def one_year_passes @age += 1 - puts "Your tree is now #{@age}" + #puts "Your tree is now #{@age}" if @age == 25 - puts "Ah I'm sorry, your tree just died. But it lived till it was 25 and had a good life." + #puts "Ah I'm sorry, your tree just died. But it lived till it was 25 and had a good life." exit end puts @@ -35,20 +36,20 @@ def one_year_passes def count_the_oranges if @age < @age_first_fruit @orange_count - puts "Your tree has no oranges." + #puts "Your tree has no oranges." else @orange_count = (@height * rand(15..25)).to_i - puts "Your tree has #{@orange_count} oranges." + #puts "Your tree has #{@orange_count} oranges." end end def pick_an_orange if @orange_count > 0 - puts "Mmm you've picked an orange. How yummy - it's very juicy." + #puts "Mmm you've picked an orange. How yummy - it's very juicy." @orange_count -= 1 - puts "You tree now has #{@orange_count} oranges left." - else - "You can't pick any oranges, as there aren't any I'm afraid. Try again in a year or so." + #puts "You tree now has #{@orange_count} oranges left." + #else + # "You can't pick any oranges, as there aren't any I'm afraid. Try again in a year or so." end end From 2ab9ed86f7b09f7de3dbf932b9a0200b41a88c39 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 22:37:49 +0100 Subject: [PATCH 26/46] Adding orange_tree.rb with a few more edits. --- ch13-creating-new-classes/orange_tree.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index a829de7e2..62b7450af 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -19,7 +19,7 @@ def initialize end def height - @height = @age * @growth_rate + return @height = @age * @growth_rate #puts "Your tree is currently #{(@height = @age * @growth_rate).round(2)}m in height." end @@ -35,10 +35,10 @@ def one_year_passes def count_the_oranges if @age < @age_first_fruit - @orange_count + return @orange_count #puts "Your tree has no oranges." else - @orange_count = (@height * rand(15..25)).to_i + return @orange_count = (@height * rand(15..25)).to_i #puts "Your tree has #{@orange_count} oranges." end end From 84308f0e66bccdd45ad22fe048ee4d077bb3d506 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sat, 17 Sep 2016 22:58:34 +0100 Subject: [PATCH 27/46] Reckon I've nailed this very enjoyable orange_tree task! Fingers crossed. --- ch13-creating-new-classes/orange_tree.rb | 146 +++++------------------ 1 file changed, 28 insertions(+), 118 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 62b7450af..0d3e37f87 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -14,42 +14,46 @@ def initialize @age_first_fruit = 6 @age_of_death = 25 @orange_count = 0 - - #puts "Your tree has been planted." + @alive = true end def height - return @height = @age * @growth_rate - #puts "Your tree is currently #{(@height = @age * @growth_rate).round(2)}m in height." + if @alive == false + puts "A dead tree is not very tall." + else + @height = (@age * @growth_rate).round(2) + return "#{@height}m" + end end def one_year_passes @age += 1 - #puts "Your tree is now #{@age}" - if @age == 25 - #puts "Ah I'm sorry, your tree just died. But it lived till it was 25 and had a good life." - exit + if @age < 25 + puts "This year your tree grew to #{self.height}m tall, and produced #{self.count_the_oranges} oranges." + elsif @age == 25 + @alive = false + puts "Oh, no! The tree is too old, and has died. :(" + elsif @age > 25 + puts "A year later, the tree is still dead. :(" end - puts end def count_the_oranges if @age < @age_first_fruit return @orange_count #puts "Your tree has no oranges." + elsif @alive == false + puts "A dead tree has no oranges. :(" else - return @orange_count = (@height * rand(15..25)).to_i - #puts "Your tree has #{@orange_count} oranges." + return @orange_count = (@height * 15 - 25).to_i end end def pick_an_orange if @orange_count > 0 - #puts "Mmm you've picked an orange. How yummy - it's very juicy." @orange_count -= 1 - #puts "You tree now has #{@orange_count} oranges left." - #else - # "You can't pick any oranges, as there aren't any I'm afraid. Try again in a year or so." + elsif @alive == false + puts "A dead tree has nothing to pick. :(" end end @@ -57,112 +61,18 @@ def pick_an_orange =begin tree = OrangeTree.new -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes -tree.height +23.times do + tree.one_year_passes +end + tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes tree.height -tree.count_the_oranges -tree.pick_an_orange -tree.one_year_passes + +5.times do + tree.one_year_passes +end + tree.height tree.count_the_oranges tree.pick_an_orange -tree.one_year_passes =end From 4f22f3cf6ddc81498a1da96649264760e71a66ae Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sun, 18 Sep 2016 11:08:44 +0100 Subject: [PATCH 28/46] Fixed minor error in orange tree program. --- ch13-creating-new-classes/orange_tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 0d3e37f87..b29bc506d 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -29,7 +29,7 @@ def height def one_year_passes @age += 1 if @age < 25 - puts "This year your tree grew to #{self.height}m tall, and produced #{self.count_the_oranges} oranges." + puts "This year your tree grew to #{self.height} tall, and produced #{self.count_the_oranges} oranges." elsif @age == 25 @alive = false puts "Oh, no! The tree is too old, and has died. :(" From aadced99258095e022057cf73c6f2b2a162219da Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sun, 18 Sep 2016 11:14:48 +0100 Subject: [PATCH 29/46] More minor edits to orange_tree.rb - here's hoping this time. --- ch13-creating-new-classes/orange_tree.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index b29bc506d..1acac85ad 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -12,7 +12,7 @@ def initialize @height = 0 @growth_rate = 0.4 @age_first_fruit = 6 - @age_of_death = 25 + @age_of_death = 26 @orange_count = 0 @alive = true end @@ -28,12 +28,12 @@ def height def one_year_passes @age += 1 - if @age < 25 + if @age < @age_of_death puts "This year your tree grew to #{self.height} tall, and produced #{self.count_the_oranges} oranges." - elsif @age == 25 + elsif @age == @age_of_death @alive = false puts "Oh, no! The tree is too old, and has died. :(" - elsif @age > 25 + elsif @age > @age_of_death puts "A year later, the tree is still dead. :(" end end From 641a9b0ee18bf8ed33ab74dad1555fb3064c4fe6 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sun, 18 Sep 2016 23:31:17 +0100 Subject: [PATCH 30/46] Adding orange tree program for the final time after learning how to run tests locally. --- ch13-creating-new-classes/orange_tree.rb | 108 ++++++++++++++++++----- 1 file changed, 85 insertions(+), 23 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 1acac85ad..417a30d79 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -5,6 +5,86 @@ # have the tree die after 25 years. # check out the rspec spec/ch13/orange_tree_spec.rb to see what strings we're looking for in the responses. +class OrangeTree + + def initialize + @height = 0 + @orange_count = 0 + @alive = true + end + + def height + if @alive + @height.round(1) + else + 'A dead tree is not very tall. :(' + end + end + + def count_the_oranges + if @alive + @orange_count + else + 'A dead tree has no oranges. :(' + end + end + + def one_year_passes + if @alive + @height = @height + 0.4 + @orange_count = 0 # old oranges fall off + if @height > 10 && rand(2) > 0 + # tree dies + @alive = false + 'Oh, no! The tree is too old, and has died. :(' + elsif @height > 2 + # new oranges grow + @orange_count = (@height * 15 - 25).to_i + "This year your tree grew to #{@height.round(1)}m tall," + + " and produced #{@orange_count} oranges." + else + "This year your tree grew to #{@height.round(1)}m tall," + + " but is still too young to bear fruit." + end + else + 'A year later, the tree is still dead. :(' + end + end + + def pick_an_orange + if @alive + if @orange_count > 0 + @orange_count = @orange_count - 1 + 'You pick a juicy, delicious orange!' + else + 'You search every branch, but find no oranges.' + end + else + 'A dead tree has nothing to pick. :(' + end + end + end + +=begin +tree = OrangeTree.new +23.times do + tree.one_year_passes +end + +tree.one_year_passes +tree.count_the_oranges +tree.height + +5.times do + tree.one_year_passes +end + +tree.height +tree.count_the_oranges +tree.pick_an_orange +=end + +=begin class OrangeTree def initialize @@ -21,15 +101,15 @@ def height if @alive == false puts "A dead tree is not very tall." else - @height = (@age * @growth_rate).round(2) - return "#{@height}m" + @height end end def one_year_passes @age += 1 if @age < @age_of_death - puts "This year your tree grew to #{self.height} tall, and produced #{self.count_the_oranges} oranges." + @height = (@age * @growth_rate).round(2) + puts "This year your tree grew to #{self.height}m tall, and produced #{self.count_the_oranges} oranges." elsif @age == @age_of_death @alive = false puts "Oh, no! The tree is too old, and has died. :(" @@ -40,12 +120,11 @@ def one_year_passes def count_the_oranges if @age < @age_first_fruit - return @orange_count - #puts "Your tree has no oranges." + @orange_count elsif @alive == false puts "A dead tree has no oranges. :(" else - return @orange_count = (@height * 15 - 25).to_i + @orange_count = (@height * 15 - 25).to_i end end @@ -58,21 +137,4 @@ def pick_an_orange end end - -=begin -tree = OrangeTree.new -23.times do - tree.one_year_passes -end - -tree.count_the_oranges -tree.height - -5.times do - tree.one_year_passes -end - -tree.height -tree.count_the_oranges -tree.pick_an_orange =end From b011587d8ff1bf071fe65cbee928a3f46830299a Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sun, 18 Sep 2016 23:38:37 +0100 Subject: [PATCH 31/46] Again committing this blasted orange_tree.rb --- ch13-creating-new-classes/orange_tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 417a30d79..98a49aa2c 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -33,7 +33,7 @@ def one_year_passes if @alive @height = @height + 0.4 @orange_count = 0 # old oranges fall off - if @height > 10 && rand(2) > 0 + if @height >= 10 # tree dies @alive = false 'Oh, no! The tree is too old, and has died. :(' From 523dbfa57b44f49d261fdff7111cf29b5eefa2b2 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Sun, 18 Sep 2016 23:57:45 +0100 Subject: [PATCH 32/46] Again committing this blasted orange_tree.rb grrrrr --- ch13-creating-new-classes/orange_tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 98a49aa2c..ae05c27ee 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -33,7 +33,7 @@ def one_year_passes if @alive @height = @height + 0.4 @orange_count = 0 # old oranges fall off - if @height >= 10 + if @height > 10 # tree dies @alive = false 'Oh, no! The tree is too old, and has died. :(' From 4631ffafc62a8779a667bc8e412da33902413edd Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 00:01:36 +0100 Subject: [PATCH 33/46] I really need this to pass now. --- ch13-creating-new-classes/orange_tree.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index ae05c27ee..c945b996c 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -33,21 +33,19 @@ def one_year_passes if @alive @height = @height + 0.4 @orange_count = 0 # old oranges fall off - if @height > 10 + if @height > 10.1 # tree dies @alive = false - 'Oh, no! The tree is too old, and has died. :(' + puts 'Oh, no! The tree is too old, and has died. :(' elsif @height > 2 # new oranges grow @orange_count = (@height * 15 - 25).to_i - "This year your tree grew to #{@height.round(1)}m tall," + - " and produced #{@orange_count} oranges." + puts "This year your tree grew to #{@height.round(1)}m tall, and produced #{@orange_count} oranges." else - "This year your tree grew to #{@height.round(1)}m tall," + - " but is still too young to bear fruit." + puts "This year your tree grew to #{@height.round(1)}m tall, but is still too young to bear fruit." end else - 'A year later, the tree is still dead. :(' + puts 'A year later, the tree is still dead. :(' end end @@ -55,12 +53,12 @@ def pick_an_orange if @alive if @orange_count > 0 @orange_count = @orange_count - 1 - 'You pick a juicy, delicious orange!' + puts 'You pick a juicy, delicious orange!' else - 'You search every branch, but find no oranges.' + puts 'You search every branch, but find no oranges.' end else - 'A dead tree has nothing to pick. :(' + puts 'A dead tree has nothing to pick. :(' end end end From ecb493aa629c3d0cd26abb1d7db37741a930f9e6 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 00:05:07 +0100 Subject: [PATCH 34/46] After all this, I find it doesn't work because I put puts in there. Dear god. --- ch13-creating-new-classes/orange_tree.rb | 33 ++++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index c945b996c..c7f717335 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -4,7 +4,7 @@ # starting in its sixth year have it produce oranges at a rate of (height * 15 - 25) per year. # have the tree die after 25 years. # check out the rspec spec/ch13/orange_tree_spec.rb to see what strings we're looking for in the responses. - +=begin class OrangeTree def initialize @@ -36,16 +36,16 @@ def one_year_passes if @height > 10.1 # tree dies @alive = false - puts 'Oh, no! The tree is too old, and has died. :(' + 'Oh, no! The tree is too old, and has died. :(' elsif @height > 2 # new oranges grow @orange_count = (@height * 15 - 25).to_i - puts "This year your tree grew to #{@height.round(1)}m tall, and produced #{@orange_count} oranges." + "This year your tree grew to #{@height.round(1)}m tall, and produced #{@orange_count} oranges." else - puts "This year your tree grew to #{@height.round(1)}m tall, but is still too young to bear fruit." + "This year your tree grew to #{@height.round(1)}m tall, but is still too young to bear fruit." end else - puts 'A year later, the tree is still dead. :(' + 'A year later, the tree is still dead. :(' end end @@ -53,16 +53,16 @@ def pick_an_orange if @alive if @orange_count > 0 @orange_count = @orange_count - 1 - puts 'You pick a juicy, delicious orange!' + 'You pick a juicy, delicious orange!' else - puts 'You search every branch, but find no oranges.' + 'You search every branch, but find no oranges.' end else - puts 'A dead tree has nothing to pick. :(' + 'A dead tree has nothing to pick. :(' end end end - +=end =begin tree = OrangeTree.new 23.times do @@ -82,7 +82,7 @@ def pick_an_orange tree.pick_an_orange =end -=begin + class OrangeTree def initialize @@ -97,7 +97,7 @@ def initialize def height if @alive == false - puts "A dead tree is not very tall." + "A dead tree is not very tall." else @height end @@ -107,12 +107,12 @@ def one_year_passes @age += 1 if @age < @age_of_death @height = (@age * @growth_rate).round(2) - puts "This year your tree grew to #{self.height}m tall, and produced #{self.count_the_oranges} oranges." + "This year your tree grew to #{self.height}m tall, and produced #{self.count_the_oranges} oranges." elsif @age == @age_of_death @alive = false - puts "Oh, no! The tree is too old, and has died. :(" + "Oh, no! The tree is too old, and has died. :(" elsif @age > @age_of_death - puts "A year later, the tree is still dead. :(" + "A year later, the tree is still dead. :(" end end @@ -120,7 +120,7 @@ def count_the_oranges if @age < @age_first_fruit @orange_count elsif @alive == false - puts "A dead tree has no oranges. :(" + "A dead tree has no oranges. :(" else @orange_count = (@height * 15 - 25).to_i end @@ -130,9 +130,8 @@ def pick_an_orange if @orange_count > 0 @orange_count -= 1 elsif @alive == false - puts "A dead tree has nothing to pick. :(" + "A dead tree has nothing to pick. :(" end end end -=end From e4c0422e33de00fd6b512dad3381a949c17536e3 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 00:07:07 +0100 Subject: [PATCH 35/46] This time it failed due to a lack of smiley! Hahaha --- ch13-creating-new-classes/orange_tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index c7f717335..f54b5d53a 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -97,7 +97,7 @@ def initialize def height if @alive == false - "A dead tree is not very tall." + "A dead tree is not very tall. :(" else @height end From 79a2ae487676ee7d9c095023adf9d241619c4bfb Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 00:10:02 +0100 Subject: [PATCH 36/46] Okay that time it failed for a legitimate error that I have now fixed. COME ON THIS TIME PLEASE. --- ch13-creating-new-classes/orange_tree.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index f54b5d53a..a95f7a6f9 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -127,10 +127,10 @@ def count_the_oranges end def pick_an_orange - if @orange_count > 0 - @orange_count -= 1 - elsif @alive == false + if @alive == false "A dead tree has nothing to pick. :(" + elsif @orange_count > 0 + @orange_count -= 1 end end From 042cbc727e99f30cd680dabb9267f9bd68af4b96 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 00:30:42 +0100 Subject: [PATCH 37/46] Added a very fun interactive baby dragon. Hoping to pass --- .../interactive_baby_dragon.rb | 143 +++++++++++++++++- 1 file changed, 142 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..3210a2d91 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,142 @@ -# your code here \ No newline at end of file +class Dragon + + def initialize name + @name = name + @asleep = false + @stuff_in_belly = 10 # He's full. + @stuff_in_intestine = 0 # He doesn't need to go. + puts "#{@name} is born." + self.ask_for_action + end + + def ask_for_action + puts + puts "What would you like to do with #{@name}? Please type 'feed', 'toss', 'walk', 'put to bed', or 'rock'." + action = gets.chomp + self.method_dispatch action.downcase + end + + def method_dispatch action + if action == "feed" + self.feed + elsif action == "toss" + self.toss + elsif action == "walk" + self.walk + elsif action == "put to bed" + self.put_to_bed + elsif action == "rock" + self.rock + else + puts "I'm sorry. I didn't understand your action." + self.ask_for_action + end + end + + def feed + puts "You feed #{@name}." + @stuff_in_belly = 10 + passage_of_time + end + + def walk + puts "You walk #{@name}." + @stuff_in_intestine = 0 + passage_of_time + end + + def put_to_bed + puts "You put #{@name} to bed." + @asleep = true + 3.times do + if @asleep + passage_of_time + end + if @asleep + puts "#{@name} snores, filling the room with smoke." + end + end + + if @asleep + @asleep = false + puts "#{@name} wakes up slowly." + end + end + + def toss + puts "You toss #{@name} up into the air." + puts 'He giggles, which singes your eyebrows.' + passage_of_time + end + + def rock + puts "You rock #{@name} gently." + @asleep = true + puts 'He briefly dozes off...' + passage_of_time + if @asleep + @asleep = false + puts '...but wakes when you stop.' + end + end + + private + + # "private" means that the methods defined here are + # methods internal to the object. (You can feed your + # dragon, but you can't ask him whether he's hungry.) + + def hungry? + + # Method names can end with "?". + # Usually, we do this only if the method + # returns true or false, like this: + @stuff_in_belly <= 2 + end + + def poopy? + @stuff_in_intestine >= 8 + end + + def passage_of_time + + if @stuff_in_belly > 0 + # Move food from belly to intestine. + @stuff_in_belly = @stuff_in_belly - 1 + @stuff_in_intestine = @stuff_in_intestine + 1 + else # Our dragon is starving! + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name} is starving! In desperation, he ate YOU!" + exit # This quits the program. + end + + if @stuff_in_intestine >= 10 + @stuff_in_intestine = 0 + puts "Whoops! #{@name} had an accident..." + end + + if hungry? + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name}'s stomach grumbles..." + end + + if poopy? + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name} does the potty dance..." + end + + self.ask_for_action + end + +end + +pet = Dragon.new 'Norbert' From 4d31971eb2124e797977bcb49470e2e2211b372b Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 00:46:08 +0100 Subject: [PATCH 38/46] Adding better profiling. --- .../even_better_profiling.rb | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..3073ed91d 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,30 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + + profiling_on = false + + if profiling_on + start_time = Time.new + block.call + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + block.call + end + +end + +profile '25000 doublings' do + number = 1 + 25000.times do + number = number + number + end + puts "#{number.to_s.length} digits" + # That's the number of digits in this HUGE number. +end + +profile 'count to a million' do + number = 0 + 1000000.times do + number = number + 1 + end +end From 7f986cb6eaaa18b607dba21d8ee5576e09344d89 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 00:48:45 +0100 Subject: [PATCH 39/46] Changed profiling to be true in order to pass tests. Fingers crossed. --- ch14-blocks-and-procs/even_better_profiling.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index 3073ed91d..ee7873792 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,6 +1,6 @@ def profile block_description, &block - profiling_on = false + profiling_on = true if profiling_on start_time = Time.new @@ -10,7 +10,7 @@ def profile block_description, &block else block.call end - + end profile '25000 doublings' do From 40c924102734216a77931f2829b334ccb79ae4ff Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 01:07:58 +0100 Subject: [PATCH 40/46] Committing grandfather_clock.rb solution. --- ch14-blocks-and-procs/grandfather_clock.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..a8323b464 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,19 @@ def grandfather_clock &block - # your code here -end \ No newline at end of file + hour = Time.new.hour + + if hour >= 13 + hour -= 12 + end + + if hour == 0 + hour = 12 + end + + hour.times do + block.call + end +end + +grandfather_clock do + puts "DONG!" +end From 0e5b28fe77bf26bb35fbfd147bdefd47863cf3b9 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 01:27:23 +0100 Subject: [PATCH 41/46] Adding program logger. --- ch14-blocks-and-procs/program_logger.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..9a6d7d324 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,16 @@ def log desc, &block - # your code here -end \ No newline at end of file + puts "Beginning " + desc + "..." + result = block.call + puts "..." + desc + " finished, returning: " + result.to_s +end + +log "outer block" do + log "some little block" do + 5 + end + log "yet another block" do + "I like Thai food!" + end + + "0" == 0 +end From f2a97311864759b5e8010727aaa56fab0badb5b0 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 01:38:47 +0100 Subject: [PATCH 42/46] Improved program logger. --- .../better_program_logger.rb | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..85659babc 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,21 @@ +$nest_depth = 0 + def log desc, &block - # your code here -end \ No newline at end of file + prefix = " "*$nest_depth + puts prefix + "Beginning " + desc + "..." + $nest_depth += 1 + result = block.call + puts prefix + "..." + desc + " finished, returning: " + result.to_s + +end + +log "outer block" do + log "some little block" do + 5 + end + log "yet another block" do + "I like Thai food!" + end + + '0' == "0" +end From 67bdcb4f0724258c166c471d91dcac7342b397ff Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 01:43:14 +0100 Subject: [PATCH 43/46] Adding revision to better program logger. --- ch14-blocks-and-procs/better_program_logger.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 85659babc..06c5a53bd 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -5,6 +5,7 @@ def log desc, &block puts prefix + "Beginning " + desc + "..." $nest_depth += 1 result = block.call + $nest_depth -= 1 puts prefix + "..." + desc + " finished, returning: " + result.to_s end From b3796358dab75387a6f9c7c2e7dce6c84b778734 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 01:47:09 +0100 Subject: [PATCH 44/46] Surely final edits. --- .../better_program_logger.rb | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 06c5a53bd..f6aaa6373 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,22 +1,24 @@ -$nest_depth = 0 +$logger_depth = 0 def log desc, &block - prefix = " "*$nest_depth - puts prefix + "Beginning " + desc + "..." - $nest_depth += 1 - result = block.call - $nest_depth -= 1 - puts prefix + "..." + desc + " finished, returning: " + result.to_s - + prefix = ' '*$logger_depth + puts prefix+"Beginning #{desc.inspect}..." + $logger_depth += 1 + result = block[] + $logger_depth -= 1 + puts prefix+"...#{desc.inspect} finished, returning: #{result}" end -log "outer block" do - log "some little block" do - 5 - end - log "yet another block" do - "I like Thai food!" +log 'outer block' do + log 'some little block' do + log 'teeny-tiny block' do + 'lOtS oF lOVe'.downcase + end + 7 * 3 * 2 end + log 'yet another block' do + '!doof naidnI evol I'.reverse + end '0' == "0" end From 3a2af2c0ae76ae8329377f347dd42806c0de12f5 Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 01:50:02 +0100 Subject: [PATCH 45/46] Surely please this time... --- ch14-blocks-and-procs/program_logger.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 9a6d7d324..559789486 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,16 +1,17 @@ def log desc, &block - puts "Beginning " + desc + "..." + puts 'Beginning "' + desc + '"...' result = block.call - puts "..." + desc + " finished, returning: " + result.to_s + puts '..."' + desc + '" finished, returning: ' + result.to_s end -log "outer block" do - log "some little block" do - 5 +log 'outer block' do + log 'some little block' do + 1**1 + 2**2 end - log "yet another block" do - "I like Thai food!" + + log 'yet another block' do + '!doof iahT ekil I'.reverse end - "0" == 0 + '0' == 0 end From 593652fb8e8e38c864b9913c23ff1606ef72d94a Mon Sep 17 00:00:00 2001 From: Frances Maxwell Date: Mon, 19 Sep 2016 01:59:25 +0100 Subject: [PATCH 46/46] Really hoping this is my last commit for the Chris Pine exercises. PLEASE! I'll do anything :) Fingers crossed. --- ch14-blocks-and-procs/better_program_logger.rb | 10 +++++----- ch14-blocks-and-procs/program_logger.rb | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index f6aaa6373..896bf8eb2 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,6 +1,6 @@ $logger_depth = 0 -def log desc, &block +def better_log desc, &block prefix = ' '*$logger_depth puts prefix+"Beginning #{desc.inspect}..." $logger_depth += 1 @@ -9,15 +9,15 @@ def log desc, &block puts prefix+"...#{desc.inspect} finished, returning: #{result}" end -log 'outer block' do - log 'some little block' do - log 'teeny-tiny block' do +better_log 'outer block' do + better_log 'some little block' do + better_log 'teeny-tiny block' do 'lOtS oF lOVe'.downcase end 7 * 3 * 2 end - log 'yet another block' do + better_log 'yet another block' do '!doof naidnI evol I'.reverse end '0' == "0" diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 559789486..3438199fb 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,15 +1,15 @@ -def log desc, &block +def program_log desc, &block puts 'Beginning "' + desc + '"...' result = block.call puts '..."' + desc + '" finished, returning: ' + result.to_s end -log 'outer block' do - log 'some little block' do +program_log 'outer block' do + program_log 'some little block' do 1**1 + 2**2 end - log 'yet another block' do + program_log 'yet another block' do '!doof iahT ekil I'.reverse end