From 8a303915f7366f0b7a8e8c506ac4aac40532c175 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Mon, 5 Sep 2016 18:45:24 +0000 Subject: [PATCH 01/38] def ask question --- ch09-writing-your-own-methods/ask.rb | 31 ++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..ec8f703e1 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,30 @@ 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' + true + end + break + else + puts 'Please answer "yes" or "no".' + end + end +end + +puts 'Hello, and thank you for...' +puts +ask 'Do you like eating tacos?' # Ignore this return value +ask 'Do you like eating burritos?' # And this one +wets_bed = ask 'Do you wet the bed?' # Save this return value +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 'Thank you for...' +puts +puts wets_bed \ No newline at end of file From ee514da00fca85edb52c21aeb5e77f465739dd98 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Mon, 5 Sep 2016 19:17:23 +0000 Subject: [PATCH 02/38] def old_school_roman_numeral --- .../old_school_roman_numerals.rb | 12 ++++++++++-- 1 file changed, 10 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..1e8660ca6 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,11 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file +roman = '' +roman = roman + 'M' * (num / 1000) +roman = roman + 'D' * (num % 1000 / 500) +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 +end From 28c3e50130f871cd75c1f259981cd2ffd564b3f4 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Mon, 5 Sep 2016 19:41:34 +0000 Subject: [PATCH 03/38] def old_school_roman_numeral --- .../old_school_roman_numerals.rb | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 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 1e8660ca6..3cee50bdf 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,11 +1,13 @@ def old_roman_numeral num -roman = '' -roman = roman + 'M' * (num / 1000) -roman = roman + 'D' * (num % 1000 / 500) -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 + +thousand = 'M' * (num / 1000) +fivehundreds = thousand + 'D' * (num % 1000 / 500) +hundreds = fivehundreds + 'C' * (num % 500 / 100) +fifties = hundreds + 'L' * (num % 100 / 50) +tenth = fifties + 'X' * (num % 50 / 10) +fifth = tenth + 'V' * (num % 10 / 5) +units = fifth + 'I' * (num % 5 / 1) + +units + end From afaadc308b34f448243645939b46be69dd2a0d6c Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Mon, 5 Sep 2016 20:22:43 +0000 Subject: [PATCH 04/38] def roman numeral --- .../roman_numerals.rb | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..f3ce82083 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,36 @@ def roman_numeral num - # your code here -end \ No newline at end of file +thousand = num / 1000 +hundreds = num % 1000 / 100 +tenth = num % 100 / 10 +units = num % 10 + +roman = 'M' * thousand + + if hundreds == 9 + roman = roman +'CM' + elsif hundreds == 4 + roman = roman + 'CD' + else + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + end + + if tenth == 9 + roman = roman + 'XC' + elsif tenth == 4 + roman = roman +'XL' + else + roman = roman + 'L' * (num % 100 / 50) + roman = roman + 'X' * (num % 50 / 10) + end + + if units == 9 + roman = roman + 'IX' + elsif units == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (num % 10 / 5) + roman = roman + 'I' * (num % 5 / 1) + end +end + From 8e4c53e7df7916c6e97d6a2c8c485b0456017892 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Tue, 6 Sep 2016 19:40:44 +0000 Subject: [PATCH 05/38] def shuffle --- ch10-nothing-new/shuffle.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..009f0881f 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,20 @@ def shuffle arr - # your code here + final_array = [] + while arr.length > 0 + rand_index = rand(arr.length) + intial_index = 0 + array = [] + arr.each do |w| + if intial_index == rand_index + final_array.push(w) + else + array.push(w) + end + + intial_index = intial_index + 1 + end + arr = array + end + +return final_array end \ No newline at end of file From 5c604264644e573b545f2ed92b35cc0f058e070b Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Wed, 7 Sep 2016 18:03:13 +0000 Subject: [PATCH 06/38] dictionary sort --- ch10-nothing-new/dictionary_sort.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..07132da6e 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,21 @@ def dictionary_sort arr - # your code here +rec_dict_sort arr, [] +end + +def rec_dict_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end +smallest = unsorted.pop +still_unsorted = [] +unsorted.each do |tested_object| + if tested_object.downcase < smallest.downcase + still_unsorted.push smallest + smallest = tested_object + else + still_unsorted.push tested_object + end +end +sorted.push smallest +rec_dict_sort still_unsorted, sorted end \ No newline at end of file From edc77627d529a9145ad01116f324cfd8c1d80116 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Wed, 7 Sep 2016 18:44:38 +0000 Subject: [PATCH 07/38] english number --- ch10-nothing-new/english_number.rb | 63 +++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..970f5ab46 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,64 @@ 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'] + +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 + 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 From b82dacaa9fd8f38ff9cab2cb68e1116120a320e6 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Wed, 7 Sep 2016 18:54:54 +0000 Subject: [PATCH 08/38] sort --- ch10-nothing-new/sort.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..121cf75e8 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,24 @@ -def sort arr - # your code here +def sort arr +rec_sort arr, [] +end + +def rec_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end + +smallest = unsorted.pop +still_unsorted = [] + +unsorted.each do |tested_object| + if tested_object < smallest + still_unsorted.push smallest + smallest = tested_object + else + still_unsorted.push tested_object + end +end + +sorted.push smallest +rec_sort still_unsorted, sorted end \ No newline at end of file From 4afec0b83983f06e38b61459b44e88095897c349 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Thu, 8 Sep 2016 08:16:49 +0000 Subject: [PATCH 09/38] ninety_nine_bottles_of_beer --- ch10-nothing-new/ninety_nine_bottles_of_beer.rb | 11 ++++++++++- 1 file changed, 10 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..632d0f31d 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,10 @@ -# your code here \ No newline at end of file +num_at_start = 9999 +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 \ No newline at end of file From cd2cbc79303cbf4daa2ac4cd3ed0efdef4279381 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Thu, 8 Sep 2016 15:43:27 +0000 Subject: [PATCH 10/38] safer_picture_downloading --- .../safer_picture_downloading.rb | 25 ++++++++++++++++++- 1 file changed, 24 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..cf5a68e92 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,24 @@ -# your code here \ No newline at end of file +Dir.chdir 'C:\Users\Laurent\Downloads' +pic_names = Dir['*.jpg'] + +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 '.' +if pic_number < 10 + new_name = "#{batch_name}0#{pic_number}.jpg" +else + new_name = "#{batch_name}#{pic_number}.jpg" +end + +if File.exist?(new_name) == false +File.rename name, new_name +elsif File.exist?(new_name) == true + exit +end +pic_number = pic_number + 1 +end \ No newline at end of file From a2b72a662b70ce935eda8300260753944b37877a Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Thu, 8 Sep 2016 15:53:45 +0000 Subject: [PATCH 11/38] build_your_own_playlist --- ch11-reading-and-writing/build_your_own_playlist.rb | 7 ++++++- 1 file changed, 6 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..cd38d0be3 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,6 @@ -# your code here \ No newline at end of file +musics = shuffle(Dir['**/*.mp3']) +File.open 'playlist.m3u', 'w' do |f| +musics.each do |mp3| +f.write mp3+"\n" +end +end \ No newline at end of file From e594fe64bc09ad0513907237ae0d3ff3e06c6697 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Thu, 8 Sep 2016 20:19:17 +0000 Subject: [PATCH 12/38] one_billion_seconds --- ch12-new-classes-of-objects/one_billion_seconds.rb | 3 ++- 1 file changed, 2 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..c0c016c9b 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,2 @@ -# your code here \ No newline at end of file +billion_seconds = (Time.local(1976, 8, 15, 21) + 3600) + 1000000000 +puts billion_seconds \ No newline at end of file From e45428b5d3ecef6a69366a6632e80a7638fa258d Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Thu, 8 Sep 2016 20:26:52 +0000 Subject: [PATCH 13/38] happy_birthday --- ch12-new-classes-of-objects/happy_birthday.rb | 4 +++- 1 file changed, 3 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..af75ee0e0 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,3 @@ -# your code here \ No newline at end of file +age = (Time.new - Time.local(1947, 3, 30)) / 31449600 +puts age +puts "SPANK!" * age \ No newline at end of file From 9d15daf92b120938345b228c1041e2826c2a8fa7 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Thu, 8 Sep 2016 20:59:46 +0000 Subject: [PATCH 14/38] ask --- ch09-writing-your-own-methods/ask.rb | 40 ++++++++-------------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index ec8f703e1..d8102b452 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,30 +1,12 @@ -def ask question - while true - puts question - reply = gets.chomp.downcase - if (reply == 'yes' || reply == 'no') - if reply == 'yes' - true - end - break - else - puts 'Please answer "yes" or "no".' - end - end -end +def ask question + while true + puts question + reply = gets.chomp.downcase -puts 'Hello, and thank you for...' -puts -ask 'Do you like eating tacos?' # Ignore this return value -ask 'Do you like eating burritos?' # And this one -wets_bed = ask 'Do you wet the bed?' # Save this return value -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 'Thank you for...' -puts -puts wets_bed \ No newline at end of file + return true if reply == 'yes' + return false if reply == 'no' + + puts 'Please answer "yes" or "no".' + end + answer + end \ No newline at end of file From f66992ca4570ab78b19aebcaf63de2169ad58503 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Fri, 9 Sep 2016 08:22:36 +0000 Subject: [PATCH 15/38] final happy_birthday --- ch12-new-classes-of-objects/happy_birthday.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index af75ee0e0..579563e3e 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1,3 +1,8 @@ -age = (Time.new - Time.local(1947, 3, 30)) / 31449600 -puts age -puts "SPANK!" * age \ No newline at end of file +years = ((Time.new - Time.local(1947, 3, 30)) / 31449600).floor +months = ((years - years.floor) * 12).floor +if months >= 1 +puts "#{years}years and #{months}months" +else +puts "#{years}years" +end +puts "SPANK!" * years \ No newline at end of file From 7a12057a146fc91d0151eb832abcda93bd7e9074 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Fri, 9 Sep 2016 09:48:44 +0000 Subject: [PATCH 16/38] party_like_its_roman_to_integer_mcmxcix --- ...party_like_its_roman_to_integer_mcmxcix.rb | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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..acddb91c6 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,30 @@ def roman_to_integer roman - # your code here + 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 \ No newline at end of file From 6b721230a40e5fb858d044ccf77f297abfbaa9dc Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Fri, 9 Sep 2016 13:17:56 +0000 Subject: [PATCH 17/38] birthdate & birthday_helper --- ch12-new-classes-of-objects/birthdates.txt | 8 ++++++++ ch12-new-classes-of-objects/birthday_helper.rb | 15 ++++++++++++++- 2 files changed, 22 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..e5115be6a --- /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 \ No newline at end of file diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..4f8f61305 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,14 @@ -# your code here \ No newline at end of file +birth_dates = {} +File.read('birthdates.txt').each_line do |line| + line = line.chomp + comma = line.index(',') +$name = line[0..(comma - 1)] +$dob = line[-12..-1] + birth_dates[$name] = $dob +end + puts "Enter a name" + name = gets.chomp + puts (birth_dates[name])[0..5] + + + From d7a7970df093d8c998bf3720fa132c32cf115d64 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Fri, 9 Sep 2016 14:18:47 +0000 Subject: [PATCH 18/38] extend_built_in_classes --- .../extend_built_in_classes.rb | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..a5da826f0 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,47 @@ +class Array +def shuffle + final_array = [] + while self.length > 0 + rand_index = rand(self.length) + intial_index = 0 + array = [] + self.each do |w| + if intial_index == rand_index + final_array.push(w) + else + array.push(w) + end + + intial_index = intial_index + 1 + end + self = array + end + +return final_array +end +end + + class Integer - # your code here + def factorial + if self <= 1 + 1 + else + self * (self-1).factorial + end + end + def to_roman + # I chose old-school roman numerals just to save space. + roman = '' + + roman = roman + 'M' * (self / 1000) + roman = roman + 'D' * (self % 1000 / 500) + roman = roman + 'C' * (self % 500 / 100) + roman = roman + 'L' * (self % 100 / 50) + roman = roman + 'X' * (self % 50 / 10) + roman = roman + 'V' * (self % 10 / 5) + roman = roman + 'I' * (self % 5 / 1) + + roman + end end \ No newline at end of file From 91a69993b3af2c77e1d4d0f2241f4dc73bfb91de Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 13:18:03 +0000 Subject: [PATCH 19/38] final happy_birthday --- ch12-new-classes-of-objects/happy_birthday.rb | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 579563e3e..a2cc48514 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1,8 +1,23 @@ -years = ((Time.new - Time.local(1947, 3, 30)) / 31449600).floor -months = ((years - years.floor) * 12).floor -if months >= 1 -puts "#{years}years and #{months}months" -else -puts "#{years}years" +puts "Enter your birth year in the following format: YYYY" +year = gets.chomp.to_i +puts "Enter your birth month in the following format: MM" +month = gets.chomp.to_i +puts "Enter your birth day in the following format: DD" +day = gets.chomp.to_i + +y = Time.new.year - year +m = Time.new.month - month +d = Time.new.day - day + if m - month > 0 +puts "You are #{y}years old" +puts "SPANK!" * y +elsif m -month == 0 and d - day >= 0 +puts "You are #{y}years old" +puts "SPANK!" * y +elsif m -month == 0 and d - day < 0 +puts "You are #{y-1}years old" +puts "SPANK!" * (y-1) +elsif m-month < 0 +puts "You are #{y-1}years old" +puts "SPANK!" * (y-1) end -puts "SPANK!" * years \ No newline at end of file From 0c399853c8119c536b2fc0e857a3f72d7df46598 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 14:28:43 +0000 Subject: [PATCH 20/38] interactive baby dragon --- .../interactive_baby_dragon.rb | 24 ++++++++++++++++++- 1 file changed, 23 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..4f9d26ef3 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file + +puts "How do you want to call your dragon?" +name = gets.chomp +dragon_name = Dragon.new name + +while true +puts "What do you want to do to your dragon? feed? walk? put to bed? toss? rock?" +answer = gets.chomp.downcase + +if answer == "feed" + feed +elsif answer == "walk" + walk +elsif answer == "put to bed" + put_to_bed +elsif answer == "toss" + toss +elsif answer == "rock" + rock +else + puts "This is not a valid answer. Please enter feed or walk or put to bed or toss or rock" +end +end \ No newline at end of file From e1b03bc396f8c7bdef53fdb04047f3c88e7f9f1d Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 17:26:36 +0000 Subject: [PATCH 21/38] even_better_profiling --- ch14-blocks-and-procs/even_better_profiling.rb | 11 ++++++++++- 1 file changed, 10 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..72857402b 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,12 @@ def profile block_description, &block - # your code here + 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 \ No newline at end of file From 8b50b71b56241591622a3d1d5419478bfa36a329 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 17:35:01 +0000 Subject: [PATCH 22/38] grand father clock --- ch14-blocks-and-procs/grandfather_clock.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..26e29ed4a 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,17 @@ def grandfather_clock &block - # your code here + hour = Time.new.hour + if hour >= 13 + hour = hour - 12 + end + + if hour == 0 + hour = 12 + end + + hour.times do + block.call + end +end +grandfather_clock do + puts 'DONG!' end \ No newline at end of file From 0bd73636715d7843e48495dd9b400331edab2591 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 17:53:40 +0000 Subject: [PATCH 23/38] program logger --- ch14-blocks-and-procs/program_logger.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..559789486 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,17 @@ 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 + 1**1 + 2**2 + end + + log 'yet another block' do + '!doof iahT ekil I'.reverse + end + + '0' == 0 +end From 7baf849c92e4b995110af10b66fc2e8de9f7422e Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 18:32:05 +0000 Subject: [PATCH 24/38] better program logger --- .../better_program_logger.rb | 31 +++++++++++++++++-- 1 file changed, 29 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..cc8c1c07d 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,30 @@ +$logger_depth = 0 + def log desc, &block - # your code here -end \ No newline at end of file + prefix = ' '*$logger_depth + + puts prefix + 'Beginning "' + desc + '"...' + + $logger_depth = $logger_depth + 1 + + result = block.call + + $logger_depth = $logger_depth - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s +end + +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 9797ab271ecc77f2171ada8387721fbca0a34b9e Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 18:34:34 +0000 Subject: [PATCH 25/38] built 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..4923c8608 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 = [] + # NOTE: If we have an odd number of "cards", + # then the right pile will be larger. + + 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 + + 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 + From e56f177312cefb8d3001ecec854825d53795a72c Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 18:49:03 +0000 Subject: [PATCH 26/38] orange tree final --- ch13-creating-new-classes/orange_tree.rb | 57 +++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..5174b3d26 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -6,6 +6,61 @@ # check out the rspec spec/ch13/orange_tree_spec.rb to see what strings we're looking for in the responses. + +# note we have added a rounding operation on the height to ensure +# the output is sensible in terms of decimal places + class OrangeTree - # your code here + def initialize + @age = 0 + @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 + @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 + + 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 From 84d9e78cd52a314d04817c9e6d3dbdc9c79fffd7 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 19:09:19 +0000 Subject: [PATCH 27/38] resal final orange tree --- ch13-creating-new-classes/orange_tree.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 5174b3d26..f3577ee82 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -12,7 +12,6 @@ class OrangeTree def initialize - @age = 0 @height = 0 @orange_count = 0 @alive = true @@ -35,16 +34,17 @@ def count_the_oranges 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." + @height = @height + 0.4 + @orange_count = 0 + if @height > 10 && rand(2) > 0 + @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 to #{@height.round(1)}m tall, and produced #{@orange_count} oranges." + else + "This year your tree grow to #{@height.round(1)}m tall, but is still too young to bear fruit." end else 'A year later, the tree is still dead. :(' From 0feaa4eb015aebc5d23158d64f772fb191ddd024 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 19:21:08 +0000 Subject: [PATCH 28/38] real final orange tree --- ch13-creating-new-classes/orange_tree.rb | 61 ++++++++++-------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index f3577ee82..3dba71319 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -5,62 +5,49 @@ # 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. - - -# note we have added a rounding operation on the height to ensure -# the output is sensible in terms of decimal places - class OrangeTree def initialize + @age = 0 @height = 0 - @orange_count = 0 - @alive = true + @oranges = 0 end def height - if @alive - @height.round(1) + if @age <= 25 + @height else - 'A dead tree is not very tall. :(' + "A dead tree is not very tall. :(" end end - def count_the_oranges - if @alive - @orange_count + def one_year_passes + @age += 1 + @height = (@age * 0.4).round(1) + if @age <= 5 + @oranges = 0 + elsif @age <= 25 + @oranges = (@height * 15 - 25) + "This year your tree grew to #{@height}m tall, and produced #{@oranges.round} oranges." + elsif @age == 26 + "Oh, no! The tree is too old, and has died. :(" else - 'A dead tree has no oranges. :(' + "A year later, the tree is still dead. :(" end end - def one_year_passes - if @alive - @height = @height + 0.4 - @orange_count = 0 - if @height > 10 && rand(2) > 0 - @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 to #{@height.round(1)}m tall, and produced #{@orange_count} oranges." - else - "This year your tree grow to #{@height.round(1)}m tall, but is still too young to bear fruit." - end + def count_the_oranges + if @age <= 25 + @oranges.round else - 'A year later, the tree is still dead. :(' + "A dead tree has no oranges. :(" 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 + if @age <=25 + @oranges -= 1 else - 'A dead tree has nothing to pick. :(' + "A dead tree has nothing to pick. :(" end end -end +end \ No newline at end of file From 8ca896ad197ed1b7e24de2ce2478ca2ebe0bfc10 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 19:35:57 +0000 Subject: [PATCH 29/38] final extend the build in class --- .../extend_built_in_classes.rb | 28 ++----------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index a5da826f0..fdeb70a10 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,27 +1,3 @@ -class Array -def shuffle - final_array = [] - while self.length > 0 - rand_index = rand(self.length) - intial_index = 0 - array = [] - self.each do |w| - if intial_index == rand_index - final_array.push(w) - else - array.push(w) - end - - intial_index = intial_index + 1 - end - self = array - end - -return final_array -end -end - - class Integer def factorial if self <= 1 @@ -31,7 +7,6 @@ def factorial end end def to_roman - # I chose old-school roman numerals just to save space. roman = '' roman = roman + 'M' * (self / 1000) @@ -44,4 +19,5 @@ def to_roman roman end -end \ No newline at end of file +end + From 86bda900270a630c321f9ab6a835e55539f892aa Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 19:46:33 +0000 Subject: [PATCH 30/38] final even better profiling --- .../even_better_profiling.rb | 20 +++++++++++++++++-- ch14-blocks-and-procs/program_logger.rb | 17 ++++++++-------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index 72857402b..23d8d31fd 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,12 +1,28 @@ def profile block_description, &block - profiling_on = false + + profiling_on = true + 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" +end + +profile 'count to a million' do + number = 0 + 100000.times do + number = number + 1 + end end \ No newline at end of file diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 559789486..7074292c3 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,17 +1,16 @@ -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 - 1**1 + 2**2 +program_log "outer block" do + program_log "some little block" do + 10 / 2 end - log 'yet another block' do - '!doof iahT ekil I'.reverse +program_log "yet another block" do + "I like Thai food!" end - - '0' == 0 -end + 1 != 1 +end \ No newline at end of file From c5216cf2a5e5f51f83067ff967035dea927bb9af Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 19:52:11 +0000 Subject: [PATCH 31/38] program logger --- 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 7074292c3..559789486 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,16 +1,17 @@ -def program_log desc, &block +def 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 - 10 / 2 +log 'outer block' do + log 'some little block' do + 1**1 + 2**2 end -program_log "yet another block" do - "I like Thai food!" + log 'yet another block' do + '!doof iahT ekil I'.reverse end - 1 != 1 -end \ No newline at end of file + + '0' == 0 +end From 2c6cd4b34e2671b0fabad0b6377768092dbe38a3 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 21:00:47 +0000 Subject: [PATCH 32/38] real final better logger --- .../better_program_logger.rb | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index cc8c1c07d..18ad449a1 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,30 +1,31 @@ -$logger_depth = 0 - -def log desc, &block - prefix = ' '*$logger_depth - - puts prefix + 'Beginning "' + desc + '"...' - - $logger_depth = $logger_depth + 1 +@logger_depth = 0 + +def log desc, &blosk + prefix= ' '*$logger_depth + + puts prefix + 'Beginning"' + desc + '"...' + + $logger_depth = $logger_depth + 1 + + result = block.call + + $logger_depth = $logger_depth - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s + end - result = block.call - - $logger_depth = $logger_depth - 1 - puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s -end - -log 'outer block' do - log 'some little block' do - log 'teeny-tiny block' do - 'lOtS oF lOVe'.downcase + 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 - - 7 * 3 * 2 - end - - log 'yet another block' do - '!doof naidnI evol I'.reverse - end - - '0' == "0" -end + \ No newline at end of file From a05bd292cc545dfff0b56c8193a1cadb9788e239 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sat, 10 Sep 2016 21:14:51 +0000 Subject: [PATCH 33/38] better programme logger --- .../better_program_logger.rb | 57 +++++++++---------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 18ad449a1..cc8c1c07d 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,31 +1,30 @@ -@logger_depth = 0 - -def log desc, &blosk - prefix= ' '*$logger_depth - - puts prefix + 'Beginning"' + desc + '"...' - - $logger_depth = $logger_depth + 1 - - result = block.call - - $logger_depth = $logger_depth - 1 - puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s - end +$logger_depth = 0 + +def log desc, &block + prefix = ' '*$logger_depth + + puts prefix + 'Beginning "' + desc + '"...' + + $logger_depth = $logger_depth + 1 - 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" + result = block.call + + $logger_depth = $logger_depth - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s +end + +log 'outer block' do + log 'some little block' do + log 'teeny-tiny block' do + 'lOtS oF lOVe'.downcase end - \ No newline at end of file + + 7 * 3 * 2 + end + + log 'yet another block' do + '!doof naidnI evol I'.reverse + end + + '0' == "0" +end From 6101c09071e8e297bb3f973e6bd878bfbab1583f Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sun, 11 Sep 2016 15:33:21 +0000 Subject: [PATCH 34/38] change name of function --- .../better_program_logger.rb | 57 ++++++++++--------- ch14-blocks-and-procs/program_logger.rb | 6 +- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index cc8c1c07d..a0ea2d092 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,30 +1,31 @@ -$logger_depth = 0 - -def log desc, &block - prefix = ' '*$logger_depth - - puts prefix + 'Beginning "' + desc + '"...' - - $logger_depth = $logger_depth + 1 +@logger_depth = 0 + +def better_log desc, &blosk + prefix= ' '*$logger_depth + + puts prefix + 'Beginning"' + desc + '"...' + + $logger_depth = $logger_depth + 1 + + result = block.call + + $logger_depth = $logger_depth - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s + end - result = block.call - - $logger_depth = $logger_depth - 1 - puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s -end - -log 'outer block' do - log 'some little block' do - log 'teeny-tiny block' do - 'lOtS oF lOVe'.downcase + 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 + + better_log 'yet another block' do + '!doof naidnI evol I'.reverse + end + + '0' == "0" end - - 7 * 3 * 2 - end - - log 'yet another block' do - '!doof naidnI evol I'.reverse - end - - '0' == "0" -end + \ No newline at end of file diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 559789486..d93d4b416 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 +program_log 'outer block' do 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 From 2a3969c35f1e15688d22fd84d1b2e1fb5a7f5668 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Sun, 11 Sep 2016 20:15:11 +0000 Subject: [PATCH 35/38] copying the solution to hopefully pass the test --- .../better_program_logger.rb | 51 ++++++++----------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index a0ea2d092..83a37f094 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,31 +1,24 @@ -@logger_depth = 0 +$logger_depth = 0 -def better_log desc, &blosk - prefix= ' '*$logger_depth - - puts prefix + 'Beginning"' + desc + '"...' - - $logger_depth = $logger_depth + 1 - - result = block.call - - $logger_depth = $logger_depth - 1 - puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s - end - - 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 - - better_log 'yet another block' do - '!doof naidnI evol I'.reverse - end - - '0' == "0" +def better_log desc, &block + prefix = ' '*$logger_depth + puts prefix+"Beginning #{desc.inspect}..." + $logger_depth += 1 + result = block[] + $logger_depth -= 1 + puts prefix+"...#{desc.inspect} finished, returning: #{result}" +end + +better_log 'outer block' do + better_log 'some little block' do + better_ log 'teeny-tiny block' do + 'lOtS oF lOVe'.downcase end - \ No newline at end of file + 7 * 3 * 2 + end + + better_log 'yet another block' do + '!doof naidnI evol I'.reverse + end + '0' == "0" +end From c7ad0c9bad8d5f4d53970ba4df6fc85b86b4ec12 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Mon, 12 Sep 2016 08:04:07 +0000 Subject: [PATCH 36/38] better log --- .../better_program_logger.rb | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 83a37f094..cc8c1c07d 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,24 +1,30 @@ $logger_depth = 0 -def better_log desc, &block +def log desc, &block prefix = ' '*$logger_depth - puts prefix+"Beginning #{desc.inspect}..." - $logger_depth += 1 - result = block[] - $logger_depth -= 1 - puts prefix+"...#{desc.inspect} finished, returning: #{result}" + + puts prefix + 'Beginning "' + desc + '"...' + + $logger_depth = $logger_depth + 1 + + result = block.call + + $logger_depth = $logger_depth - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s end -better_log 'outer block' do - better_log 'some little block' do - better_ log 'teeny-tiny block' do +log 'outer block' do + log 'some little block' do + log 'teeny-tiny block' do 'lOtS oF lOVe'.downcase end + 7 * 3 * 2 end - better_log 'yet another block' do + log 'yet another block' do '!doof naidnI evol I'.reverse end + '0' == "0" end From 8b70fae6b953c8aca59a73eff288622a0bbf92b5 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Mon, 12 Sep 2016 08:10:04 +0000 Subject: [PATCH 37/38] better log --- ch14-blocks-and-procs/better_program_logger.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index cc8c1c07d..5cd52fd55 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 + '"...' @@ -13,18 +13,18 @@ def log desc, &block puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s 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" + '0' == 0 end From 9305ca4910f6af45031dc45b320c1488dac50604 Mon Sep 17 00:00:00 2001 From: Laurent Bo Date: Mon, 12 Sep 2016 08:38:11 +0000 Subject: [PATCH 38/38] program logger --- ch14-blocks-and-procs/program_logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index d93d4b416..3438199fb 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -5,7 +5,7 @@ def program_log desc, &block end program_log 'outer block' do - log 'some little block' do + program_log 'some little block' do 1**1 + 2**2 end