From 5ac4df0a00a59754afe4b9b29c5076851efbb67e Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Thu, 8 Sep 2016 20:48:13 +0100 Subject: [PATCH 01/27] Initial commit --- ch09-writing-your-own-methods/ask.rb | 38 +++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..bd3958260 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 + 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...' +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 chimicngas?' +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 'Thank you for...' +puts +puts wets_bed + + end \ No newline at end of file From c50d902278e5dd0fbd6b4355728b9ed89f16ca1b Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Thu, 8 Sep 2016 21:14:38 +0100 Subject: [PATCH 02/27] Correct solution --- ch09-writing-your-own-methods/ask.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index bd3958260..56845d983 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -9,13 +9,15 @@ def ask question else return false end - break else puts 'Please answer "yes" or "no".' end + end + answer -end + +end puts 'Hello, and thank you for...' puts @@ -36,4 +38,3 @@ def ask question puts wets_bed -end \ No newline at end of file From b844fbcb7680a9c8c85c509b6a044097bb98b31a Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 15:42:10 +0100 Subject: [PATCH 03/27] completed roman_numerals --- .../old_school_roman_numerals.rb | 27 +++++++++++++++++-- 1 file changed, 25 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..79aca3e0f 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,26 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file +# empty string is assigned to the variable roman. +roman = '' + +# num is divided by 1000. Using the / method that leaves no remainders. The answer is then multplied by the string 'M'. +roman = roman + 'M' * (num / 1000) +# roman now has the new value of 'M'. Now num is divided by 1000, but this time using the % method that returns the remainder. +#The remaineder is then diided by 500 using the /method that doesn't return a remainder. The answer is then multiplied by the string 'D' +# Which is added to the previous roman variable value 'MD' +roman = roman + 'D' * (num % 1000 / 500) +# The method continues to add the new set of strings. +roman = roman + 'C' * (num % 500 / 100) + +roman = roman + 'L' * (num % 100 / 50) + +roman = roman + 'X' * (num % 50 / 10) + +roman = roman + 'V' * (num % 10 / 5) + +roman = roman + 'I' * (num % 5 / 1) + +roman # The complete set. + +end + +puts(old_roman_numeral(1999)) From 2fd6f4b1688901c7774e68a33c7af215a75d00bf Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 15:49:43 +0100 Subject: [PATCH 04/27] Completed roman_numerals --- .../roman_numerals.rb | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..8518ee3c5 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,41 @@ -def roman_numeral num - # your code here -end \ No newline at end of file +=begin +This takes a number and returns the roman numerals but with the small numbers going before the big numbers and subtracting the value to equal the total. +=end + +def roman_numeral num # this method takes a number (num) + thous = (num / 1000) # it divides the num by 1000 leaving no remainders and asigns that value to the variable thous + hunds = (num % 1000 / 100) # the variable hunds is assigned to the sum of the number divided by 1000 and its remainder divided by 100. + tens = (num % 100 / 10) # ect + ones = (num % 10 ) + roman = 'M' * thous # the value of thousand is multiplied by 'M' and assigned to the variable roman + + if hunds == 9 # if statment uses a bolean. If the value of hunds is equal to 9 then it asigns roman the new value of + roman = roman + 'CM' + elsif hunds == 4 + roman = roman + 'CD' + else + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + end + + if tens == 9 + roman = roman + 'XC' + elsif tens == 4 + roman = roman + 'XL' + else + roman = roman + 'L' * (num % 100 / 50) + roman = roman + 'X' * (num % 50 / 10) + end + + if ones == 9 + roman = roman + 'IX' + elsif ones == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (num % 10 / 5) + roman = roman + 'I' * (num % 5 / 1) + end + + roman +end +puts(roman_numeral(1999)) From ae5a9c08eb707cd1581b59dbfba5c37d8260b770 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 15:58:19 +0100 Subject: [PATCH 05/27] Completed dictionary_sort --- ch10-nothing-new/dictionary_sort.rb | 46 +++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..e9438b0ef 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,43 @@ -def dictionary_sort arr - # your code here -end \ No newline at end of file +def dictionary_sort arr # takes an array + rec_dict_sort arr, [] # calls the recursive function +end + +def rec_dict_sort unsorted, sorted # takes the array that isn't sorted and an unsorted array. + if unsorted.length <= 0 # if the unsorted array is empty it returns the sorted array. Function finnished. + return sorted + end + +# So if we got here, then it means we still + +# have work to do. + + smallest = unsorted.pop # Takes the last element of the array. removes it and holds it in the variable 'smallest' + + still_unsorted = [] + + unsorted.each do |tested_object| # block of code. Takes each element from the array and iterates through them. + + if tested_object.downcase <= smallest.downcase # If statment tests to see if each object in downcase compares to the smallest in down.case + + still_unsorted.push smallest # It then takes the smallest and pushes it into the still_unsorted array. + + smallest = tested_object # new object assigned as smallest and used to compare the rest of the code to. + + else + still_unsorted.push tested_object # if the object is not smaller than put it straight into the still_unsorted array. + end +end + +# Now "smallest" really does point to the + +# smallest element that "unsorted" contained, + +# and all the rest of it is in "still_unsorted". + + sorted.push smallest + + rec_dict_sort still_unsorted, sorted + +end + +puts(dictionary_sort(['can','feel','singing.','like','A','can'])) \ No newline at end of file From ce0397ef52f49c9a33da42710024fbfe7a8a038c Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 16:01:25 +0100 Subject: [PATCH 06/27] completed english_number.rb --- ch10-nothing-new/english_number.rb | 207 ++++++++++++++++++++++++++++- 1 file changed, 206 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..453cbb907 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,208 @@ + def english_number number - # your code here + +# We accept numbers from 0 to 100. + +if number < 0 + return 'Please enter a number zero or greater.' end + +if number > 100 + return 'Please enter a number 100 or less.' +end + +num_string = '' # This is the string we will return. + +# "left" is how much of the number + +# we still have left to write out. + +# "write" is the part we are + +# writing out right now. + +# write and left... get it? :) + +left = number + +write = left/100 # How many hundreds left? + +left = left - write*100 # Subtract off those hundreds. + +if write > 0 + return 'one hundred' +end + +write = left/10 # How many tens left? + +left = left - write*10 # Subtract off those tens. + +if write > 0 + +if write == 1 # Uh-oh... + +# Since we can't write "tenty-two" + +# instead of "twelve", we have to + +# make a special exception for these. + +if left == 0 + num_string = num_string + 'ten' +elsif left == 1 + num_string = num_string + 'eleven' +elsif left == 2 + num_string = num_string + 'twelve' +elsif left == 3 + num_string = num_string + 'thirteen' +elsif left == 4 + num_string = num_string + 'fourteen' +elsif left == 5 + num_string = num_string + 'fifteen' +elsif left == 6 + num_string = num_string + 'sixteen' +elsif left == 7 + num_string = num_string + 'seventeen' +elsif left == 8 + +num_string = num_string + 'eighteen' + +elsif left == 9 + +num_string = num_string + 'nineteen' + +end + +# Since we took care of the digit in the + +# ones place already, we have nothing left to write. + +left = 0 + +elsif write == 2 + +num_string = num_string + 'twenty' + +elsif write == 3 + +num_string = num_string + 'thirty' + +elsif write == 4 + +num_string = num_string + 'forty' + +elsif write == 5 + +num_string = num_string + 'fifty' + +elsif write == 6 + +num_string = num_string + 'sixty' + +elsif write == 7 + +num_string = num_string + 'seventy' + +elsif write == 8 + +num_string = num_string + 'eighty' + +elsif write == 9 + +num_string = num_string + 'ninety' + +end + +if left > 0 + +num_string = num_string + '-' + +end + +end + +write = left # How many ones left to write out? + +left = 0 # Subtract off those ones. + +if write > 0 + +if write == 1 + +num_string = num_string + 'one' + +elsif write == 2 + +num_string = num_string + 'two' + + + +elsif write == 3 + +num_string = num_string + 'three' + +elsif write == 4 + +num_string = num_string + 'four' + +elsif write == 5 + +num_string = num_string + 'five' + +elsif write == 6 + +num_string = num_string + 'six' + +elsif write == 7 + +num_string = num_string + 'seven' + +elsif write == 8 + +num_string = num_string + 'eight' + +elsif write == 9 + +num_string = num_string + 'nine' + +end + +end + +if num_string == '' + +# The only way "num_string" could be empty + +# is if "number" is 0. + +return 'zero' + +end + +# If we got this far, then we had a number + +# somewhere in between 0 and 100, so we need + +# to return "num_string". + +num_string + +end + +puts english_number( 0) + +puts english_number( 9) + +puts english_number( 10) + +puts english_number( 11) + +puts english_number( 17) + +puts english_number( 32) + +puts english_number( 88) + +puts english_number( 99) + +puts english_number(100) \ No newline at end of file From aa13a8221b85daf4ac548ddb6aa7864c053b590f Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 16:06:32 +0100 Subject: [PATCH 07/27] Completed shuffle --- ch10-nothing-new/shuffle.rb | 46 ++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..04a75578c 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,43 @@ -def shuffle arr - # your code here -end \ No newline at end of file + def shuffle arr + shuf = [] #asigns empty array to shuff + + while arr.length > 0 + + # Randomly pick one element of the array. + + rand_index = rand(arr.length) # gitves you back a random indext in the array. + + # Now go through each item in the array, + + # putting them all into new_arr except for the + + # randomly chosen one, which goes into shuf. + + curr_index = 0 + + new_arr = [] + + arr.each do |item| # takes each item in the array + + if curr_index == rand_index # this number is placed in the shuff array + shuf.push item + else #if not it is put in the new_arry + new_arr.push item + end + curr_index = curr_index + 1 # This allows it to go through each index in the arr one by one. + end + + # Replace the original array with the new, + + # smaller array. + + arr = new_arr # this will be the array that is all the numbers other then the one where the index has matched with the random_index. + #This is then is passed back through the methods. + + end + + shuf # return the array shuf + +end + +puts(shuffle([1,2,3,4,5,6,7,8,9])) \ No newline at end of file From 96b7180fd8795581ca1129e3ce4ea4d00e54f554 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 16:12:53 +0100 Subject: [PATCH 08/27] Completed sort activity --- ch10-nothing-new/sort.rb | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..fb1c7135c 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,26 @@ -def sort arr - # your code here -end \ No newline at end of file +def sort arr # takes an array + rec_sort arr, [] #calls the function +end + + +def rec_sort unsorted, sorted # takes 2 arrays. sorted will be initially empty. + if unsorted.length <= 0 # # if the unsorted array is less or equal to 0 the return sorted. + return sorted + end + + smallest = unsorted.pop # the number at the end of the array is popped and asigned to the variable smallest. + still_unsorted = [] + + unsorted.each do |tested_object| # This block of code iterates through each item in the array. If the if the tested_object is smaller than the + if tested_object < smallest # smallest is no longer the smallest so it is placed in the still_unsorted array. + still_unsorted.push smallest + smallest = tested_object # now the tested item is assigned to the smallest. + else + still_unsorted.push tested_object # if the tested object is larger than the smallest object than push it to the still_unsorted array + end + end + sorted.push smallest #Take the newly asigned smallest item and push it to the sorted array. + rec_sort still_unsorted, sorted # call the rec_sort function again passing updated still unsorted and sorted arrays through. +end + +puts(sort(['can', 'feel', 'singing', 'like', 'a', 'can'])) \ No newline at end of file From dc176030e5ac9f1a7321f1e3291a7797a613d5a4 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 16:31:29 +0100 Subject: [PATCH 09/27] completed ninety_nine_bottles_of_beer.rb --- .../ninety_nine_bottles_of_beer.rb | 121 +++++++++++++++++- 1 file changed, 120 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..b5559de87 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,120 @@ -# your code here \ No newline at end of file + +def english_number number + if number < 0 # No negative numbers. + return 'Please enter a number that isn\'t negative.' + end + if number == 0 + return 'zero' + end +# No more special cases! No more returns! +num_string = '' # This is the string we will return. +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], + ['sexdecillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novemdecillion', 60], + ['vigintillion', 63], + ['googol', 100]] + # "left" is how much of the number + # we still have left to write out. + # "write" is the part we are + # writing out right now. + # write and left...get it? :) + 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 # How many zillions left? + left = left - write*zil_base # Subtract off those zillions. + + if write > 0 + # Now here's the recursion: + prefix = english_number write + num_string = num_string + prefix + ' ' + zil_name + if left > 0 + # So we don't write 'two billionfifty-one'... + num_string = num_string + ' ' + end + end +end + +write = left/10 # How many tens left? +left = left - write*10 # Subtract off those tens. + +if write > 0 + if ((write == 1) and (left > 0)) + # Since we can't write "tenty-two" instead of + # "twelve", we have to make a special exception + # for these. + num_string = num_string + teenagers[left-1] + # The "-1" is because teenagers[3] is + # 'fourteen', not 'thirteen'. + # Since we took care of the digit in the + # ones place already, we have nothing left to write. + left = 0 +else + num_string = num_string + tens_place[write-1] + # The "-1" is because tens_place[3] is + # 'forty', not 'thirty'. +end + +if left > 0 + # So we don't write 'sixtyfour'... + num_string = num_string + '-' + end +end + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + num_string = num_string + ones_place[write-1] + # The "-1" is because ones_place[3] is + # 'four', not 'three'. +end + # Now we just return "num_string"... + num_string +end + + +num_at_start = 5 # change to 9999 if you want +num_now = num_at_start +while num_now > 2 + puts english_number(num_now).capitalize + ' bottles of beer on the wall, ' + + english_number(num_now) + ' bottles of beer!' + num_now = num_now - 1 + puts 'Take one down, pass it around, ' + + english_number(num_now) + ' bottles of beer on the wall!' +end + +puts "Two bottles of beer on the wall, two bottles of beer!" +puts "Take one down, pass it around, one bottle of beer on the wall!" +puts "One bottle of beer on the wall, one bottle of beer!" +puts "Take one down, pass it around, no more bottles of beer on the wall!" \ No newline at end of file From b549aa1140bade720a8567adb60e2204701cb4ae Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 17:22:36 +0100 Subject: [PATCH 10/27] Completed build_a_better_playlist --- .../build_a_better_playlist.rb | 38 ++++++++++++++++++- 1 file changed, 37 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..9daf8951f 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,39 @@ def music_shuffle filenames - # your code here + +filenames = filenames.sort +len = filenames.length +# Now we shuffle twice. +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 + # take card from right pile + shuf.push(filenames[r_idx]) + r_idx = r_idx + 1 + else + # take card from left pile + shuf.push(filenames[l_idx]) + l_idx = l_idx + 1 + end + end + filenames = shuf + end +# And cut the deck. +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 3b1cefb3fb1638da84d05ed690eeac0c5d0b1c0c Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 17:23:56 +0100 Subject: [PATCH 11/27] Completed build_your_own_playlist --- .../build_your_own_playlist.rb | 56 ++++++++++++++++++- 1 file changed, 55 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..dfeeb79f1 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,55 @@ -# your code here \ No newline at end of file +def shuffle arr + shuf = [] #asigns empty array to shuff + + while arr.length > 0 + + # Randomly pick one element of the array. + + rand_index = rand(arr.length) # gitves you back a random indext in the array. + + # Now go through each item in the array, + + # putting them all into new_arr except for the + + # randomly chosen one, which goes into shuf. + + curr_index = 0 + + new_arr = [] + + arr.each do |item| # takes each item in the array + + if curr_index == rand_index # this number is placed in the shuff array + shuf.push item + else #if not it is put in the new_arry + new_arr.push item + end + curr_index = curr_index + 1 # This allows it to go through each index in the arr one by one. + end + + # Replace the original array with the new, + + # smaller array. + + arr = new_arr # this will be the array that is all the numbers other then the one where the index has matched with the random_index. + #This is then is passed back through the methods. + + end + + shuf # return the array shuf + +end + +puts(shuffle([1,2,3,4,5,6,7,8,9])) + + +# using the shuffle method as defined above +all_oggs = shuffle(Dir['**/*.ogg']) + +File.open 'playlist.m3u', 'w' do |f| + all_oggs.each do |ogg| + f.write ogg+"\n" + end +end + +puts 'Done!' \ No newline at end of file From 167b42864a1a740466f333f630d335109b35b02c Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 17:24:55 +0100 Subject: [PATCH 12/27] Completed safer_picture_downloading --- .../safer_picture_downloading.rb | 91 ++++++++++++++++++- 1 file changed, 90 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..1962a4b15 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,90 @@ -# your code here \ No newline at end of file +def music_shuffle filenames + +require 'win32ole' + +STDOUT.sync = true +Thread.abort_on_exception = true + +Dir.chdir 'C:\Documents and Settings\Chris\Desktop\pictureinbox' + +# Always look here for pics. +pic_names = Dir['!undated/**/*.{jpg,avi}'] + +thm_names = Dir['!undated/**/*.{thm}' ] +# Scan for memory cards in the card reader. +WIN32OLE.new("Scripting.FileSystemObject").Drives.each() do |x| +#driveType 1 is removable disk + if x.DriveType == 1 && x.IsReady + pic_names += Dir[x.DriveLetter+':/**/*.{jpg,avi}'] + thm_names += Dir[x.DriveLetter+':/**/*.{thm}' ] + end + end + months = %w(jan feb mar apr may jun jul aug sep oct nov dec) + + encountered_error = false + + print "Downloading #{pic_names.size} files: " + + pic_names.each do |name| + print '.' + is_movie = (name[-3..-1].downcase == 'avi') + if is_movie + orientation = 0 + new_name = File.open(name) do |f| + f.seek(0x144,IO::SEEK_SET) + f.read(20) + end + + new_name[0...3] = '%.2d' % (1 + months.index(new_name[0...3].downcase)) + new_name = new_name[-4..-1] + ' ' + new_name[0...-5] +else + new_name, orientation = File.open(name) do |f| + f.seek(0x36, IO::SEEK_SET) + orientation_ = f.read(1)[0] + f.seek(0xbc, IO::SEEK_SET) + new_name_ = f.read(19) + [new_name_, orientation_] + end +end + +[4,7,10,13,16].each {|n| new_name[n] = '.'} +if new_name[0] != '2'[0] + encountered_error = true + puts "\n"+'ERROR: Could not process "'+name+ + '" because it\'s not in the proper format!' + next +end + +save_name = new_name + (is_movie ? '.orig.avi' : '.jpg') +# Make sure we don't save over another file!! +while FileTest.exist? save_name + new_name += 'a' + save_name = new_name + (is_movie ? '.orig.avi' : '.jpg') +end + +case orientation + when 6 + `convert "#{name}" -rotate "90>" "#{save_name}"` + File.delete name + when 8 + `convert "#{name}" -rotate "-90>" "#{save_name}"` + File.delete name + else + File.rename name, save_name + end +end +print "\nDeleting #{thm_names.size} THM files: " +thm_names.each do |name| + print '.' + File.delete name +end +# If something bad happened, make sure she +# sees the error message before the window closes. +if encountered_error +puts +puts "Press [Enter] to finish." +puts +gets +end + + From 2263beeefb15d3f020db82739bbb7f78afc0ae1d Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 17:42:50 +0100 Subject: [PATCH 13/27] Completed birthday helper --- .../birthday_helper.rb | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..5cdf28d31 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,40 @@ -# your code here \ No newline at end of file +=begin + +Create a hash with key value pairs. The key will be the name of the person +and the value will be their birthday. +Have a puts statement at the begining that asks "Who's birthday would you like to find?" +Save this to gets.chomp +place this value inside the hash with [] square brackets. Return this vlaue in a string interpolation. + +=end + +birth_dates = {} # this creates an empty hash +File.read('birthdates.txt').each_line do |line| # this access the 'birthdates.txt' file to read only. + line = line.chomp # This takes each line as it comes. removes the /n and assigns it to the variable line + # Find the index of first comma, + # so we know where the name ends. + +first_comma = 0 #creates a counter assigning it to the inital value of 0 +while line[first_comma] != ',' && # Creates a while loop taking each line at the index given. if it is not equal to a comma and + first_comma < line.length # the first comma, which is currently set to 0, is less than the line length. + first_comma = first_comma + 1 # first comma is then incremented +end + + name = line[0..(first_comma - 1)] # the square brackets look for an index of the line. the index is within a range from 0 through to the first comm_a + # minus 1 + date = line[-12..-1] # + + birth_dates[name] = date +end + +# Now ask the user which one they want to know. + +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 \ No newline at end of file From bb7df9170e2f3bfce12a7d2e93e92e0fc9478778 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 17:44:22 +0100 Subject: [PATCH 14/27] Completed happy birthday --- ch12-new-classes-of-objects/happy_birthday.rb | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..b56b5194b 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,41 @@ -# your code here \ No newline at end of file +=begin +1. Takes the prameters Time now and time of birth +2. Uses the gets.chomp to get the year, month and day of the persons birthday. +3. Subtract this from Time.now +3.This will give you the number of seconds. Divide this using the / method to find the amount +of years and then + +=end + + +puts 'What year were you born?' + +b_year = gets.chomp.to_i # take the answer and turn it into an intiger saving it in a variable. + +puts 'What month were you born? (1-12)' + +b_month = gets.chomp.to_i # take answer as a string and turn it into an intiger saving it in a variable. + +puts 'What day of the month were you born?' + +b_day = gets.chomp.to_i # take answer, convert it into an intiger and save it in a variable. + +b = Time.local(b_year, b_month, b_day) # Place these varaible in the Time.local parimiters) and assign to variable. + +t = Time.new # Save the time now in a variable. + +age = 1 # create a counter 'age' asigning it to the value 1 + +while Time.local(b_year + age, b_month, b_day) <= t # Start a while loop. Which uses Time.local using the birhtday + #details in the parameter. + + puts 'SPANK!' # puts spank after one cycle of the while loop indicating one year has passed. + age = age + 1 # Then add 1 to the age counter which is encluded in the Time.local parameter. + #The cycle complete when t - the time now, is equal or less to the value of the Time.local which incriments + # 1 year with each cycle of the while loop. + +end + + + + From c176ced3675c1d5d18ddf5a051414036abee3262 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 17:45:46 +0100 Subject: [PATCH 15/27] Completed one_billion_seconds --- .../one_billion_seconds.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..0c5993b15 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,15 @@ -# your code here \ No newline at end of file +=begin + +Set use Time.local to set my date of birth and add 1 billion from answer. + +=end + +def one_billion_seconds() + + time_one_billion_seconds_from_your_birth = Time.local(1980, 1, 6, 13, 30) + 1000000000 + + print "One billion seconds from the moment your are born will be on the #{time_one_billion_seconds_from_your_birth}\n" + +end + +one_billion_seconds \ No newline at end of file From 1362f252b2c2c0273118d24ca94cd52dc801b2a8 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 17:50:27 +0100 Subject: [PATCH 16/27] Completed party_like_its_roman_to_integer --- ...party_like_its_roman_to_integer_mcmxcix.rb | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 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..fea054623 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,44 @@ -def roman_to_integer roman - # your code here -end \ No newline at end of file +def roman_to_integer roman # Method takes a roman digit + +digit_vals = {'i' => 1, # creates a hash with key being the roman digit and value being the integer + + 'v' => 5, + + 'x' => 10, + + 'l' => 50, + + 'c' => 100, + + 'd' => 500, + + 'm' => 1000} + +total = 0 # creates a counter called 'total' asigning it to the value of 0 +prev = 0 # creates another counter called 'prev' and assigning it to 0 +index = roman.length - 1 # roman.length finds the length of the string and minuses 1 to find the index +while index >= 0 # while loop takes the current index and if it is less or = to 0 loops + c = roman[index].downcase # takes the roman numeral string at the -l index puts it to downcase and saves it as variable c + index = index - 1 # asigns index to the next index up. + val = digit_vals[c] # This take the hash and places the key string that has been found at the specified index. It places it + # in the square brackets so that it can find its value pair which will be a integer. It then asigns it to this intiger. + if !val # if this values has not been found in the hash + puts 'This is not a valid roman numeral!' # then it returns this message. + return # return means that the while loop ends here. + end + + if val < prev # at this moment prev has been set to 0 and val will be one of the hash values which from 5 up to 1000 + val = val * -1 # This creates a negative number and asigns it to the variable val + else + prev = val # prev is no asigned to this negative value + end + total = total + val # total is this negative value plus the value of the total which is currently at 0 but will be eventually + # be a negative value + end + + total +end + +puts(roman_to_integer('mcmxcix')) + +puts(roman_to_integer('CCCLXV')) \ No newline at end of file From eb4e80d928fd1f862fe0c9525d696ceed9ac5518 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 19:00:05 +0100 Subject: [PATCH 17/27] Completed orange_tree --- ch13-creating-new-classes/orange_tree.rb | 75 +++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..e5a7040de 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,78 @@ class OrangeTree - # your code here + + def initialize + @height = 0 + @orange_count = 0 + @alive = true + end + + def height + if @alive + @height + else + 'A dead tree is not very tall. :(' # returns its height + end + end + + def count_the_oranges + if @alive + @orange_count + else + 'A dead tree has no oranges. :(' + end + end + + def one_year_passes # ages the tree one year. Create a counter. Have a while loop that calculates the passing years. + if @alive + @height= @height + 0.4 + @orange_count = 0 + + if @height > 10 && rand(2) > 0 + #tree dies #When the tree gets to a certain age loop breaks and it is passed to a fruit producing if statment/another. + @alive = false + 'Oh, no! The tree is too old, and has died' + elsif @height > 2 + @orange_count = (@height * 15 - 25).to_i + "This year your tree grew #{@height}m tall, " + + "and produced #{@orange_count} oranges." + else + "This year your tree grew to #{@height}m tall,"+ + " but is still to 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 + +ot = OrangeTree.new +23.times do + ot.one_year_passes +end + +puts(ot.one_year_passes) +puts(ot.count_the_oranges) +puts(ot.height) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.height) +puts(ot.count_the_oranges) + + From 18e360523b1ea4323c11b4b37c273e244e43d526 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 19:02:46 +0100 Subject: [PATCH 18/27] Completed interactive_baby_dragon --- .../interactive_baby_dragon.rb | 126 +++++++++++++++++- 1 file changed, 125 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..65f9401a7 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,125 @@ -# your code here \ No newline at end of file +class Dragon + + def initialize name # initialize is a special method in Ruby programs. When you call Dragon.new to create a new Dragon object, + @asleep = false #Ruby creates an uninitialized object and then calls that object's initialize method, + @stuff_in_belly = 10 #passing in any parameters that were passed to new. + @stuff_in_intestine = 0 #This gives you a chance to write code that sets up your object's state. + puts "#{@name} is born." #instance variables asigned to values that when the class is called will be initialized. + # each object represents its own dragon, so we need each of the dragon object to carry around its own + # sleep, stuff in belly, stuff in intestine and name. These are stored as instance variables within the object. + end + def feed + puts "You feed #{@name}." # method within the class that feeds. + @stuff_in_belly = 10 + passage_of_time #passes in the passag_of_time method that subtracts from stuff_in_belly and adds 1 to stuff in intestine + end + def walk + puts "You walk #{@name}." # method within the class that walks. + @stuff_in_intestine = 0 # + passage_of_time # passes in the passage_of_time method that subtracts from stuff_in_belly and adds one to stuff in intestine. + end + def put_to_bed + puts "You put #{@name} to bed." + @asleep = true + 3.times do + if @asleep + passage_of_time #passes in the passage_of_time method that subtracts from stuff_in_belly and adds one to stuff in intestine. + 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 + + def hungry? + @stuff_in_belly <= 2 + end + + def poopy? + @stuff_in_intestine >= 8 + end + + def passage_of_time + if @stuff_in_belly > 0 + @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 + end +end + +puts 'What would you like to name your baby dragon?' +name = gets.chomp +pet = Dragon.new name + +while true + puts + puts 'commands: feed, toss, walk, rock, put to bed, exit' + command = gets.chomp + + if command == 'exit' + exit + elsif command == 'feed' + pet.feed + elsif command == 'toss' + pet.toss + elsif command == 'walk' + pet.walk + elsif command == 'rock' + pet.rock + elsif command == 'put to bed' + pet.put_to_bed + else + puts 'Hug? Please type one of the commands' + end +end From d5b7322d76b69374ab27aed5224da9e5d50df6bd Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 19:32:42 +0100 Subject: [PATCH 19/27] Completed extend_built_in_classes --- .../extend_built_in_classes.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 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..6ea9c3aa1 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,10 @@ -class Integer - # your code here -end \ No newline at end of file +class Integer # in the class Integer add a new method + def factorial # factorial method + if self <= 1 # self refers to the object. Which is in this case an integer + 1 + else + self * (self-1).factorial # it then takes the object and multiplies it by + end # by it's self-1 and then calls the function again repeating the process with + end +end + From b1cec03f5c1b3f6022c830c1ad136863a365c4a7 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 20:38:45 +0100 Subject: [PATCH 20/27] New updated version of Orange Tree --- ch13-creating-new-classes/orange_tree.rb | 128 +++++++++-------------- 1 file changed, 47 insertions(+), 81 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index e5a7040de..0505eec0c 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -1,84 +1,50 @@ -# in order to pass the rspec please follow the below rates of growth, orange production and age of death. -# have your OrangeTree grow by 0.4 per year. -# have it produce no oranges in its first 5 years -# 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. - class OrangeTree - - def initialize - @height = 0 - @orange_count = 0 - @alive = true - end - - def height - if @alive - @height - else - 'A dead tree is not very tall. :(' # returns its height - end - end - - def count_the_oranges - if @alive - @orange_count - else - 'A dead tree has no oranges. :(' - end - end - - def one_year_passes # ages the tree one year. Create a counter. Have a while loop that calculates the passing years. - if @alive - @height= @height + 0.4 - @orange_count = 0 - - if @height > 10 && rand(2) > 0 - #tree dies #When the tree gets to a certain age loop breaks and it is passed to a fruit producing if statment/another. - @alive = false - 'Oh, no! The tree is too old, and has died' - elsif @height > 2 - @orange_count = (@height * 15 - 25).to_i - "This year your tree grew #{@height}m tall, " + - "and produced #{@orange_count} oranges." - else - "This year your tree grew to #{@height}m tall,"+ - " but is still to 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 + def initialize + @age = 0 + @height = 0 + @orange_count = 0 + @alive = true end -end - -ot = OrangeTree.new -23.times do - ot.one_year_passes -end - -puts(ot.one_year_passes) -puts(ot.count_the_oranges) -puts(ot.height) -puts(ot.one_year_passes) -puts(ot.one_year_passes) -puts(ot.one_year_passes) -puts(ot.one_year_passes) -puts(ot.one_year_passes) -puts(ot.height) -puts(ot.count_the_oranges) - - + + def height + @alive ? @height : "A dead tree is not very tall. :(" + end + + def count_the_oranges + @alive ? @orange_count : "A dead tree has no oranges. :(" + end + + def pick_an_orange + if @alive + if @orange_count > 0 + @orange_count -= 1 + "Delicious!" + else + "There are no oranges on the tree this year" + end + else + "A dead tree has nothing to pick. :(" + end + end + + def one_year_passes + @orange_count = 0 + @age += 1 + @height = (@height + 0.4).round(1) + if @alive + + if @age > 25 + @alive = false + "Oh, no! The tree is too old, and has died. :(" + elsif @age > 5 + @orange_count += (@height * 15 - 25).round + "This year your tree grew to #{@height}m tall, and produced #{@orange_count} oranges." + end + + else + "A year later, the tree is still dead. :(" + end + end + +end \ No newline at end of file From da39c25346ca7bd00dba7aecc003e517dd230ae7 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 21:00:13 +0100 Subject: [PATCH 21/27] Completed program_logger --- ch14-blocks-and-procs/program_logger.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..075091387 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 +def program_log desc, &block + puts 'Beginning "' + desc + '"...' + result = block.call + puts '..."' + desc + '" finished, returning: ' + result.to_s +end + program_log 'outer block' do + program_log 'some little block' do + 1**1 + 2**2 + end + + program_log 'yet another block' do + '!doof iahT ekil I'.reverse + end + + '0' == 0 +end From a6006d6ff3b85f4df8e1046d7b65bff2a351bf54 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 21:22:02 +0100 Subject: [PATCH 22/27] Completed Grandfather clock --- ch14-blocks-and-procs/grandfather_clock.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..d55f2dbdb 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,7 @@ def grandfather_clock &block - # your code here -end \ No newline at end of file + hour = (Time.new.hour + 11)%12 + 1 + + hour.times(&block) +end + +grandfather_clock { puts 'DONG!' } From d70d0ad9ee61f1bca0fa25440a6ba52281b431bc Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 21:34:03 +0100 Subject: [PATCH 23/27] Completed better program logger --- ch14-blocks-and-procs/better_program_logger.rb | 11 +++++++++-- 1 file changed, 9 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..7d0c9cc42 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,10 @@ -def log desc, &block - # your code here +$nesting_depth = 0 + +def better_log desc, &block + indent = " " * $nesting_depth + puts "#{indent}Beginning \"#{desc}\"..." + $nesting_depth += 1 + result = block.call + puts "#{indent}...\"#{desc}\" finished, returning: #{result}" + $nesting_depth -= 1 end \ No newline at end of file From 5a39162024bbe1ddb18061a1bca7021aa88488dc Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 21:45:06 +0100 Subject: [PATCH 24/27] Completed even_better_profiling --- ch14-blocks-and-procs/even_better_profiling.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..dff7504f8 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,14 @@ -def profile block_description, &block + $OPT_PROFILING_ON = false + + def profile block_description, &block # your code here + if $OPT_PROFILING_ON + start_time = Time.new + block[] + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + + block[] + end end \ No newline at end of file From c1bbc6379b6e00c4d3f8ebb5fc9bd7179046433b Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 22:02:55 +0100 Subject: [PATCH 25/27] Updated English number --- ch10-nothing-new/english_number.rb | 289 +++++++++-------------------- 1 file changed, 84 insertions(+), 205 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index 453cbb907..96588eee3 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,208 +1,87 @@ def english_number number - -# We accept numbers from 0 to 100. - -if number < 0 - return 'Please enter a number zero or greater.' -end - -if number > 100 - return 'Please enter a number 100 or less.' -end - -num_string = '' # This is the string we will return. - -# "left" is how much of the number - -# we still have left to write out. - -# "write" is the part we are - -# writing out right now. - -# write and left... get it? :) - -left = number - -write = left/100 # How many hundreds left? - -left = left - write*100 # Subtract off those hundreds. - -if write > 0 - return 'one hundred' -end - -write = left/10 # How many tens left? - -left = left - write*10 # Subtract off those tens. - -if write > 0 - -if write == 1 # Uh-oh... - -# Since we can't write "tenty-two" - -# instead of "twelve", we have to - -# make a special exception for these. - -if left == 0 - num_string = num_string + 'ten' -elsif left == 1 - num_string = num_string + 'eleven' -elsif left == 2 - num_string = num_string + 'twelve' -elsif left == 3 - num_string = num_string + 'thirteen' -elsif left == 4 - num_string = num_string + 'fourteen' -elsif left == 5 - num_string = num_string + 'fifteen' -elsif left == 6 - num_string = num_string + 'sixteen' -elsif left == 7 - num_string = num_string + 'seventeen' -elsif left == 8 - -num_string = num_string + 'eighteen' - -elsif left == 9 - -num_string = num_string + 'nineteen' - -end - -# Since we took care of the digit in the - -# ones place already, we have nothing left to write. - -left = 0 - -elsif write == 2 - -num_string = num_string + 'twenty' - -elsif write == 3 - -num_string = num_string + 'thirty' - -elsif write == 4 - -num_string = num_string + 'forty' - -elsif write == 5 - -num_string = num_string + 'fifty' - -elsif write == 6 - -num_string = num_string + 'sixty' - -elsif write == 7 - -num_string = num_string + 'seventy' - -elsif write == 8 - -num_string = num_string + 'eighty' - -elsif write == 9 - -num_string = num_string + 'ninety' - + if number < 0 + return "Oops! Please use a positive integer!" + 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], + ['sexdecillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novemdecillion', 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 + # Now here's the recursion: + 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 - -if left > 0 - -num_string = num_string + '-' - -end - -end - -write = left # How many ones left to write out? - -left = 0 # Subtract off those ones. - -if write > 0 - -if write == 1 - -num_string = num_string + 'one' - -elsif write == 2 - -num_string = num_string + 'two' - - - -elsif write == 3 - -num_string = num_string + 'three' - -elsif write == 4 - -num_string = num_string + 'four' - -elsif write == 5 - -num_string = num_string + 'five' - -elsif write == 6 - -num_string = num_string + 'six' - -elsif write == 7 - -num_string = num_string + 'seven' - -elsif write == 8 - -num_string = num_string + 'eight' - -elsif write == 9 - -num_string = num_string + 'nine' - -end - -end - -if num_string == '' - -# The only way "num_string" could be empty - -# is if "number" is 0. - -return 'zero' - -end - -# If we got this far, then we had a number - -# somewhere in between 0 and 100, so we need - -# to return "num_string". - -num_string - -end - -puts english_number( 0) - -puts english_number( 9) - -puts english_number( 10) - -puts english_number( 11) - -puts english_number( 17) - -puts english_number( 32) - -puts english_number( 88) - -puts english_number( 99) - -puts english_number(100) \ No newline at end of file From a0dc6cbcb54a3ec92fa0ad968afdb06696bf6455 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 22:05:51 +0100 Subject: [PATCH 26/27] Updated even better profiling --- .../even_better_profiling.rb | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index dff7504f8..082dcb66e 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,14 +1,12 @@ - $OPT_PROFILING_ON = false - - def profile block_description, &block - # your code here - if $OPT_PROFILING_ON - start_time = Time.new - block[] - duration = Time.new - start_time - puts "#{block_description}: #{duration} seconds" +$OPT_PROFILING_ON = false + +def profile block_description, &block + if $OPT_PROFILING_ON + start_time = Time.new + block[] + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" else - - block[] + block[] end -end \ No newline at end of file +end From 031be966c2ad27652b44e3f36455d035d9809492 Mon Sep 17 00:00:00 2001 From: Katy McCann Date: Sun, 11 Sep 2016 22:09:02 +0100 Subject: [PATCH 27/27] Update extend_built_in_classes.rb --- .../extend_built_in_classes.rb | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index 6ea9c3aa1..dd50a0dd2 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,10 +1,27 @@ -class Integer # in the class Integer add a new method - def factorial # factorial method - if self <= 1 # self refers to the object. Which is in this case an integer - 1 - else - self * (self-1).factorial # it then takes the object and multiplies it by - end # by it's self-1 and then calls the function again repeating the process with - end +class Integer + + def factorial + + raise 'Must not use negative integer' if self < 0 + + (self <= 1) ? 1 : self * (self-1).factorial + end + + + def to_roman + + raise 'Must use positive integer' if self <= 0 + + roman = '' + + roman << 'M' * (self / 1000) + roman << 'D' * (self % 1000 / 500) + roman << 'C' * (self % 500 / 100) + roman << 'L' * (self % 100 / 50) + roman << 'X' * (self % 50 / 10) + roman << 'V' * (self % 10 / 5) + roman << 'I' * (self % 5 / 1) + + roman + end end -