From f5caedba9e1ef5a94e2283e62bbd737586e19cd5 Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Sun, 4 Sep 2016 21:37:22 +0100 Subject: [PATCH 01/11] First commit: ask.rb script streamlined --- ch09-writing-your-own-methods/ask.rb | 33 ++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..1b2f8635c 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,32 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + if (reply == 'yes' || reply == 'no') + if reply == 'yes' + return true + else + return false + end + break + else + puts 'Please answer "yes" or "no".' + end + end +end + +puts 'Hello and thank you blah blah' +ask 'Do you like eating tacos?' +ask 'Do you like eating burritos?' +wets_bed = ask 'Do you wet the bed?' +ask 'Do you like eating chimichangas?' +ask 'Do you like eating sopapillas?' +puts 'Just a few more questions...' +ask 'Do you like drinking horchata?' +ask 'Do you like eating flautas?' + +puts +puts 'DEBRIEFING:' +puts 'Thank you for...' +puts +puts wets_bed From c60b9cb2d683058baa65528583312454b95bc2ff Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Mon, 5 Sep 2016 00:18:39 +0100 Subject: [PATCH 02/11] Second commit: old_school_roman_numerals.rb script created --- .../old_school_roman_numerals.rb | 18 ++++++++++++++++-- 1 file changed, 16 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..456dd7c26 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,17 @@ 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 ) +roman +end + +puts old_roman_numeral 17 +puts old_roman_numeral 1567 +puts old_roman_numeral 222 +puts old_roman_numeral 865 +puts old_roman_numeral 4000 From 5ee06c02664836cf4f9a79b51c6d45acc64bcd8f Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Sun, 11 Sep 2016 11:46:17 +0100 Subject: [PATCH 03/11] Third commit: roman_numerals.rb script edited --- .../roman_numerals.rb | 53 ++++++++++++++++++- 1 file changed, 51 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..dd2296f35 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,52 @@ def roman_numeral num - # your code here -end \ No newline at end of file + thousands_to_convert = (num / 1000) + hundreds_to_convert = (num % 1000 / 100) + tens_to_convert = (num % 100 / 10) + units_to_convert = num % 10 + +if thousands_to_convert <= 3 + converted_thous = "M" * thousands_to_convert +end + +if hundreds_to_convert <= 3 + converted_huns = "C" * hundreds_to_convert +elsif hundreds_to_convert == 4 + converted_huns = 'C' + 'D' +elsif hundreds_to_convert == 5 + converted_huns = 'D' +elsif hundreds_to_convert > 5 && hundreds_to_convert < 9 + converted_huns = 'D' + "C" * (hundreds_to_convert - 5) +elsif hundreds_to_convert == 9 + converted_huns = 'C' + "M" +end + +if tens_to_convert <= 3 + converted_tens = "X" * tens_to_convert +elsif tens_to_convert == 4 + converted_tens = 'X' + 'L' +elsif tens_to_convert == 5 + converted_tens = 'L' +elsif tens_to_convert > 5 && tens_to_convert < 9 + converted_tens = 'L' + "X" * (tens_to_convert - 5) +elsif tens_to_convert == 9 + converted_tens = 'X' + "C" +end +ruby + +if units_to_convert <= 3 + converted_units = "I" * units_to_convert +elsif units_to_convert == 4 + converted_units = 'I' + 'V' +elsif units_to_convert == 5 + converted_units = 'V' +elsif units_to_convert > 5 && units_to_convert < 9 + converted_units = 'V' + "I" * (units_to_convert - 5) +elsif units_to_convert == 9 + converted_units = 'I' + "X" +end + +converted_num = converted_thous + converted_huns + converted_tens + converted_units +puts converted_num +converted_num +end +roman_numeral 119 From 27cd88b0e31a7564a041f805fe4675086df8e9f7 Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Sun, 11 Sep 2016 17:56:50 +0100 Subject: [PATCH 04/11] CH10 exercises completed --- ch10-nothing-new/dictionary_sort.rb | 37 ++++++- ch10-nothing-new/english_number.rb | 72 +++++++++++++- .../ninety_nine_bottles_of_beer.rb | 99 ++++++++++++++++++- ch10-nothing-new/shuffle.rb | 13 ++- ch10-nothing-new/sort.rb | 28 +++++- 5 files changed, 239 insertions(+), 10 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..1b00fd95a 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,34 @@ -def dictionary_sort arr - # your code here -end \ No newline at end of file +def dictionary_sort some_array +recursive_sort some_array, [] +end + +def recursive_sort unsorted_array, sorted_array +final_array = [] +sorting_hash = Hash.new 0 +lookup_value = 0 +sorting_array = [] +unsorted_array.each do |x| + sorting_hash[x.downcase] = x +end +unsorted_array.each do |x| + sorting_array.push(x.downcase) +end +while sorting_array.length > 0 + smallest_word = sorting_array.pop + sorting_array.each do |x| + if x == smallest_word + sorted_array.push(x) + elsif x < smallest_word + sorting_array.push(smallest_word) + smallest_word = x + end + end + sorting_array.delete(smallest_word) + sorted_array.push(smallest_word) + end +sorted_array.each do |x| + final_array.push(sorting_hash[x]) +end +puts final_array +return final_array +end diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..281a1ea17 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,73 @@ def english_number number - # your code here + if number < 0 + return 'Please enter a numer that isnt negative' + end + if number == 0 + return 'zero' + end + num_string = '' + ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'] + tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + left = number + + write = left / 1000000000 + left = left - write*1000000000 + if write > 0 + billions = english_number write + num_string = num_string + billions + ' billion' + if left > 0 + num_string = num_string + ' ' + end + end + + write = left / 1000000 + left = left - write*1000000 + if write > 0 + millions = english_number write + num_string = num_string + millions + ' million' + if left > 0 + num_string = num_string + ' ' + end + end + + write = left / 1000 + left = left - write*1000 + if write > 0 + thousands = english_number write + num_string = num_string + thousands + ' thousand' + if left > 0 + num_string = num_string + ' ' + end + end + write = left / 100 + left = left - write*100 + if write > 0 + hundreds = english_number write + num_string = num_string + hundreds + ' hundred' + if left > 0 + num_string = num_string + ' ' + end end +write = left / 10 +left = left - write*10 +if write > 0 + if ((write == 1) and (left >0)) + num_string = num_string + teenagers[left-1] + left = 0 + else + num_string = num_string + tens_place[write-1] + end + if left > 0 + num_string = num_string + '-' +end +end +write = left +left = 0 +if write > 0 + num_string = num_string + ones_place[write-1] +end +num_string +end + +puts english_number 5436000000 diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..6be93a9af 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,98 @@ -# your code here \ No newline at end of file +def english_number number + if number < 0 + return 'Please enter a numer that isnt negative' + end + if number == 0 + return 'zero' + end + num_string = '' + ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'] + tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + left = number + + write = left / 1000000000 + left = left - write*1000000000 + if write > 0 + billions = english_number write + num_string = num_string + billions + ' billion' + if left > 0 + num_string = num_string + ' ' + end + end + + write = left / 1000000 + left = left - write*1000000 + if write > 0 + millions = english_number write + num_string = num_string + millions + ' million' + if left > 0 + num_string = num_string + ' ' + end + end + + write = left / 1000 + left = left - write*1000 + if write > 0 + thousands = english_number write + num_string = num_string + thousands + ' thousand' + if left > 0 + num_string = num_string + ' ' + end + end + write = left / 100 + left = left - write*100 + if write > 0 + hundreds = english_number write + num_string = num_string + hundreds + ' hundred' + if left > 0 + num_string = num_string + ' and ' + end +end +write = left / 10 +left = left - write*10 +if write > 0 + if ((write == 1) and (left >0)) + num_string = num_string + teenagers[left-1] + left = 0 + else + num_string = num_string + tens_place[write-1] + end + if left > 0 + num_string = num_string + '-' +end +end +write = left +left = 0 +if write > 0 + num_string = num_string + ones_place[write-1] +end +num_string +end + +def ninety_nine_bottles start_num + + + while start_num > 0 + start_num_english = english_number start_num + start_num_caps = start_num_english.capitalize + next_num = start_num - 1 + next_num_english = english_number next_num + + if start_num == 1 + puts "#{start_num_caps} bottle of beer on the wall, #{start_num_english}" + puts "bottle of beer. Take one down, pass it around, #{next_num_english}" + puts "bottles of beer on the wall." + puts + else + puts "#{start_num_caps} bottles of beer on the wall, #{start_num_english}" + puts "bottles of beer. Take it down, pass it around, #{next_num_english}" + puts "bottles of beer on the wall." + puts + end +start_num = start_num - 1 +next_num = next_num - 1 +end +end + +ninety_nine_bottles 10 diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..4825e6c24 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,12 @@ def shuffle arr - # your code here -end \ No newline at end of file + shuffled_arr = [] + while arr.length > 0 + shuffle_entry = rand(arr.length) + entry_to_push = arr[shuffle_entry] + shuffled_arr.push(entry_to_push) + arr.delete_at(shuffle_entry) +end +arr = shuffled_arr +puts arr +arr +end diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..22d2d6365 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,25 @@ -def sort arr - # your code here -end \ No newline at end of file +def sort some_array +recursive_sort some_array, [] +end + +def recursive_sort unsorted_array, sorted_array + +if unsorted_array.length <= 0 + return sorted_array + puts sorted_array +end +smallest_word = unsorted_array.pop +still_unsorted = [] +unsorted_array.each do |x| + if x < smallest_word + still_unsorted.push(smallest_word) + smallest_word = x + else + still_unsorted.push(x) + end +end +sorted_array.push(smallest_word) +recursive_sort still_unsorted, sorted_array +end + +puts (sort(["fucking", "hell", "bollocks"])) From 473919307fe1027da132e997832e22501b924280 Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Sun, 11 Sep 2016 18:04:16 +0100 Subject: [PATCH 05/11] Ch11 exercise completed --- .../safer_picture_downloading.rb | 27 ++++++++++++++++++- 1 file changed, 26 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..6592c94bb 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,26 @@ -# your code here \ No newline at end of file +Dir.chdir '/Users/liam.taylor/Projects/Dummy/Destination' +pic_names = Dir['/Users/liam.taylor/Projects/Dummy/Start/**/*.txt'] + +puts "What would you like to call this batch?" +batch_name = gets.chomp + +puts +print "Downloading #{pic_names.length} files:" + +pic_number = 1 + +pic_names.each do |name| + print ' . ' + new_name = if pic_number < 10 + "#{batch_name}0#{pic_number}.jpg" + else "#{batch_name}#{pic_number}.jpg" + end + pic_number = pic_number + 1 + if File.exist? new_name + exit + else + File.rename name, new_name +end +end +puts +puts "Done!" From 90c392034d6ad001723c3ffe43dbe49408cb2ef8 Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Sun, 11 Sep 2016 18:38:38 +0100 Subject: [PATCH 06/11] Ch12 exercises added. --- .../birthday_helper.rb | 27 ++++++- ch12-new-classes-of-objects/happy_birthday.rb | 17 ++++- .../one_billion_seconds.rb | 3 +- ...party_like_its_roman_to_integer_mcmxcix.rb | 70 ++++++++++++++++++- 4 files changed, 112 insertions(+), 5 deletions(-) diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..715bd7bf7 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,26 @@ -# your code here \ No newline at end of file +Dir.chdir '/Users/liam.taylor/Projects/learn_to_program/ch12-new-classes-of-objects' +birthday_array = [] +list_of_birthdays = File.read 'birthdays.txt' +birthday_hash = {} +lookup_value = 0 +lookup_value_cells = 0 +list_of_birthdays.each_line do |line| +birthday_array.push line +end +birthday_array.each do |push| +string_to_search = push + while lookup_value < 50 + comma_perhaps = string_to_search[lookup_value] + if comma_perhaps == "," + name_to_push = string_to_search[0..lookup_value - 1] + birthday_to_push = string_to_search[lookup_value + 2..-1] + birthday_hash[name_to_push] = birthday_to_push + lookup_value = lookup_value + 1 + break + else lookup_value = lookup_value + 1 + end + end +end +puts 'Whose birthday? Do you want to know?' +sought_person = gets.chomp +puts "So, you want to know #{sought_person}'s birthday? Well it's #{birthday_hash[sought_person]}." diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..651c557dc 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,16 @@ -# your code here \ No newline at end of file +puts "What year were you born in?" +year_of_birth = gets.chomp +puts "What month were you born in?" +month_of_birth = gets.chomp +puts "What day were you born on?" +day_of_birth = gets.chomp +puts Time.local(year_of_birth, month_of_birth, day_of_birth) +birthday = Time.local(year_of_birth, month_of_birth, day_of_birth) +age_in_seconds = Time.new - birthday +age_in_years = age_in_seconds / 60 / 60 / 24 /365 +age = age_in_years.floor +puts "Then you're #{age} years old." +while age > 0 + puts "SPANK!" + age = age - 1 +end diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..9ca4971ec 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 +time_of_birth = Time.local(1988, 04, 18, 16, 30) +puts time_of_birth + 1000000000 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..a6efcc062 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,69 @@ def roman_to_integer roman - # your code here -end \ No newline at end of file +roman = roman.downcase +length_of_numeral = roman.length +lookup_value = 0 +running_total = 0 + +while lookup_value != length_of_numeral + numeral_to_measure = roman[lookup_value] + further_check = roman[lookup_value + 1] + output_number = 0 + if numeral_to_measure == "i" + if further_check == "v" + output_number = -1 + elsif further_check == "x" + output_number = -1 + else + output_number = 1 + end + end + if numeral_to_measure == "x" + if further_check == "l" + output_number = -10 + elsif further_check == "c" + output_number = -10 + else + output_number = 10 + end + end + if numeral_to_measure == "c" + if further_check == "d" + output_number = -100 + elsif further_check == "m" + output_number = -100 + else + output_number = 100 + end + end + if numeral_to_measure == "v" + output_number = 5 + end + if numeral_to_measure == "l" + output_number = 50 + end + if numeral_to_measure == "d" + output_number = 500 + end + if numeral_to_measure == "m" + output_number = 1000 + end + +numeral_to_measure = numeral_to_measure.to_i + 1 +running_total = running_total + output_number +lookup_value = lookup_value + 1 +end +if running_total == 0 + puts "Not a valid roman numeral" +else + puts running_total + return running_total +end +return running_total +end + +roman_to_integer "IX" +roman_to_integer "DLV" +roman_to_integer "MMXIV" +roman_to_integer "CXI" +roman_to_integer "CDXCIX" +roman_to_integer "mcx" From 76ed2537b4e33087abc2515780aa3b27ec8c61a3 Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Sun, 11 Sep 2016 20:58:38 +0100 Subject: [PATCH 07/11] Ch13 and ch14 exercises completed --- .../extend_built_in_classes.rb | 23 ++- .../interactive_baby_dragon.rb | 132 +++++++++++++++++- ch13-creating-new-classes/orange_tree.rb | 67 ++++++++- .../better_program_logger.rb | 25 +++- .../even_better_profiling.rb | 14 +- ch14-blocks-and-procs/grandfather_clock.rb | 14 +- ch14-blocks-and-procs/program_logger.rb | 16 ++- 7 files changed, 278 insertions(+), 13 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..28d0fd637 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,20 @@ -class Integer - # your code here -end \ No newline at end of file +class Array + def shuffle + shuffled_arr = [] + length_of_array = self.length + while length_of_array > 0 + length_of_array = self.length + shuffle_entry = rand(length_of_array) + entry_to_push = self[shuffle_entry] + shuffled_arr.push(entry_to_push) + self.delete_at(shuffle_entry) + end + while shuffled_arr.length > 0 + self.push(shuffled_arr.pop) + end + puts self + end +end + +array_to_shuffle = ["Harry", "Meg", "Lucy", "John"] +array_to_shuffle.shuffle diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..495f465d5 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,131 @@ -# your code here \ No newline at end of file +# your code he +class Dragon + def initialize name + @name = name + @asleep = false + @stuff_in_belly = 10 + @stuff_in_intestine = 0 + + puts "#{@name} is born." + puts "What would you like to do to #{@name}?" + @request_to_action = gets.chomp + dispatch + end + + def request + puts "What would you like to do to #{@name}?" + @request_to_action = gets.chomp + dispatch + end + + def feed + puts "You feed #{@name}." + @stuff_in_belly = 10 + passage_of_time + end + + def walk + puts "You take #{@name} for a walk." + @stuff_in_intestine = 0 + passage_of_time + end + + def put_to_bed + puts "You put #{@name} to bed." + @asleep = true + 3.times do + if @asleep + passage_of_time + end + if @asleep + puts "#{@name} snores, filling the room with smoke." + end + end + if @asleep + @asleep = false + puts "#{@name} wakes up slowly." + end + end + + def toss + puts "You toss #{@name} up into the air." + puts "He giggles, which singes your eyebrows." + passage_of_time + end + + def rock + puts "You rock #{@name} gently." + @asleep = true + puts "He briefly dozes off." + passage_of_time + if @asleep + @asleep = false + puts "...but wakes when you stop." + end + end + + + private + +def dispatch + if @request_to_action == "toss" + toss + elsif @request_to_action == 'walk' + walk + elsif @request_to_action == 'feed' + feed + elsif @request_to_action == "put to bed" + put_to_bed + elsif @request_to_action == "toss" + toss + elsif @request_to_action == "rock" + rock + else + puts "Not a recognised request, please try again (commands are 'feed', 'walk' 'put to bed', 'toss' and 'rock'.)" + @request_to_action = gets.chomp + end +end + + + 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 + if @asleep + @asleep = false + puts "He wakes up suddenly!" + end + puts "#{@name} is starving! In desperation, he ate YOU!" + exit +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 + +pet = Dragon.new "Clurr" diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..36b551bb7 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,70 @@ class OrangeTree - # your code here + +def initialize + @height = 0 + @tree_age = 0 + @oranges_on_tree = 0 + @growth_per_year = 0.4 + @life_status = true +end + +def height + if @life_status == true + puts "The tree is #{@height} metres tall" + else + puts "A dead tree is not very tall. :(" + end +end + +def one_year_passes + if @life_status == true + @tree_age = @tree_age + 1 + @oranges_on_tree = 0 + @height = (@height + @growth_per_year).round(1) + if @tree_age >=6 + @oranges_on_tree = (@oranges_on_tree + (@height * 15 - 25)).round(0) + puts "This year your tree grew to #{@height}m tall, and produced #{@oranges_on_tree} oranges." + else + puts "The tree is now #{@tree_age} years old. This year your tree grew to #{@height}m tall but isn't old enough to bear fruit." + end + else + puts "The tree is still dead..." + end + if @tree_age == 25 + @life_status = false + puts 'Oh, no! The tree is too old, and has died. :(' + end +end + +def count_the_oranges + if @life_status == true + if @oranges_on_tree > 0 + puts "There are #{@oranges_on_tree.to_i} oranges on the tree" + else + puts "There are no oranges on the tree." + end + else + puts "A dead tree has no oranges. :(" + end +end + +def pick_orange + if @life_status == true + if @oranges_on_tree > 0 + @oranges_on_tree = @oranges_on_tree - 1 + puts "Mmmm, tasty!" + else puts "There are no oranges to pick!" + end + else + puts "A dead tree has nothing to pick. :(" + end +end +end + +clurr = OrangeTree.new + +23.times do + clurr.one_year_passes end diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..0a9a1ee26 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,24 @@ + $nesting_depth = 0 + def log desc, &block - # your code here -end \ No newline at end of file + + buffer = ' ' * $nesting_depth + puts buffer + "Beginning #{desc}..." + $nesting_depth += 1 + output = block.call + $nesting_depth -= 1 + puts buffer + "... #{desc} finished, returning: #{output}" + end + + log "'outer block'" do + log "'some little block'" do + log "'teeny tiny block'" do + "lots of love" + end + 6 * 7 + end + log "'yet anothr block'" do + puts "I love Indian food!" + end + 5== 5 + end diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..f7448f6af 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,13 @@ def profile block_description, &block - # your code here -end \ No newline at end of file +profiling_on = false + +if profiling_on + start_time = Time.new + block.call + + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" +else + block.call +end +end diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..1c5bc3498 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,13 @@ def grandfather_clock &block - # your code here -end \ No newline at end of file + twenty_hour_time = Time.new.hour + if twenty_hour_time > 12 + twelve_hour_time = twenty_hour_time - 12 + else + twelve_hour_time = twenty_hour_time + end + twelve_hour_time.times do +puts "Dong!" + end +end + +grandfather_clock diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..31022e1e1 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,15 @@ def log desc, &block - # your code here -end \ No newline at end of file + puts "Beginning #{desc}..." + output = block.call + puts "... #{desc} finished, returning: " + output.to_s +end + +log '"outer block"' do + log '"some little block"' do + 5 + end + log '"yet another block"' do + puts "I like Thai food!" + end + 3 > 4 +end From fa36c0ee3613099ff9ddc2608533d2c1393e819d Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Sun, 11 Sep 2016 23:47:37 +0100 Subject: [PATCH 08/11] Refactoring! --- .../roman_numerals.rb | 2 +- .../extend_built_in_classes.rb | 71 ++++++++++++++++++- .../better_program_logger.rb | 40 +++++------ .../even_better_profiling.rb | 20 +++--- ch14-blocks-and-procs/grandfather_clock.rb | 2 +- ch14-blocks-and-procs/program_logger.rb | 17 ++--- 6 files changed, 108 insertions(+), 44 deletions(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index dd2296f35..63be22484 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -31,7 +31,6 @@ def roman_numeral num elsif tens_to_convert == 9 converted_tens = 'X' + "C" end -ruby if units_to_convert <= 3 converted_units = "I" * units_to_convert @@ -50,3 +49,4 @@ def roman_numeral num converted_num end roman_numeral 119 +roman_numeral 101 diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index 28d0fd637..aa0f07a14 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -16,5 +16,72 @@ def shuffle end end -array_to_shuffle = ["Harry", "Meg", "Lucy", "John"] -array_to_shuffle.shuffle +class Integer +def to_roman + thousands_to_convert = (self / 1000) + hundreds_to_convert = (self % 1000 / 100) + tens_to_convert = (self % 100 / 10) + units_to_convert = self % 10 + +if thousands_to_convert <= 3 + converted_thous = "M" * thousands_to_convert +end + +if hundreds_to_convert <= 3 + converted_huns = "C" * hundreds_to_convert +elsif hundreds_to_convert == 4 + converted_huns = 'C' + 'D' +elsif hundreds_to_convert == 5 + converted_huns = 'D' +elsif hundreds_to_convert > 5 && hundreds_to_convert < 9 + converted_huns = 'D' + "C" * (hundreds_to_convert - 5) +elsif hundreds_to_convert == 9 + converted_huns = 'C' + "M" +end + +if tens_to_convert <= 3 + converted_tens = "X" * tens_to_convert +elsif tens_to_convert == 4 + converted_tens = 'X' + 'L' +elsif tens_to_convert == 5 + converted_tens = 'L' +elsif tens_to_convert > 5 && tens_to_convert < 9 + converted_tens = 'L' + "X" * (tens_to_convert - 5) +elsif tens_to_convert == 9 + converted_tens = 'X' + "C" +end + +if units_to_convert <= 3 + converted_units = "I" * units_to_convert +elsif units_to_convert == 4 + converted_units = 'I' + 'V' +elsif units_to_convert == 5 + converted_units = 'V' +elsif units_to_convert > 5 && units_to_convert < 9 + converted_units = 'V' + "I" * (units_to_convert - 5) +elsif units_to_convert == 9 + converted_units = 'I' + "X" +end + +converted_num = converted_thous + converted_huns + converted_tens + converted_units +puts converted_num +converted_num +end + +def factorial +if self < 0 + return "no" +end + +if self <=1 + 1 +else + self * (self-1).factorial +end +end + +end + + +114.to_roman +puts 114.factorial diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0a9a1ee26..5fbcbb560 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,24 +1,22 @@ - $nesting_depth = 0 +$indent = 0 +def better_log desc, &block + prefix = ' '*$indent + puts prefix + 'Beginning "' + desc + '"...' + $indent = $indent + 1 + result = block.call + $indent = $indent - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + "#{result}" +end -def log desc, &block - - buffer = ' ' * $nesting_depth - puts buffer + "Beginning #{desc}..." - $nesting_depth += 1 - output = block.call - $nesting_depth -= 1 - puts buffer + "... #{desc} finished, returning: #{output}" - end - - log "'outer block'" do - log "'some little block'" do - log "'teeny tiny block'" do - "lots of love" - end - 6 * 7 +better_log 'outer block' do + better_log 'some little block' do + better_log 'teen-tiny block' do + 'lots of love' + end + 42 end - log "'yet anothr block'" do - puts "I love Indian food!" - end - 5== 5 + better_log 'yet another block' do + 'I like Indian food!' end + "0" == 0 +end diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index f7448f6af..d7215cf24 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,13 +1,11 @@ def profile block_description, &block -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 + 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 diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 1c5bc3498..0f8d5b664 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -6,7 +6,7 @@ def grandfather_clock &block twelve_hour_time = twenty_hour_time end twelve_hour_time.times do -puts "Dong!" +puts "DONG!" end end diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 31022e1e1..3ec9aea62 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,15 +1,16 @@ -def log desc, &block - puts "Beginning #{desc}..." +def program_log desc, &block + puts 'Beginning "' + desc + '"...' output = block.call - puts "... #{desc} finished, returning: " + output.to_s + puts '..."' + desc + '" finished, returning: ' + output.to_s end -log '"outer block"' do - log '"some little block"' do +program_log 'outer block' do + program_log 'some little block' do 5 end - log '"yet another block"' do - puts "I like Thai food!" + + log 'yet another block' do + "I like Thai food!" end - 3 > 4 + 16 == 8*2 end From 8db35ea982797223c4fab8303e2a92c06318afa1 Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Mon, 12 Sep 2016 00:40:26 +0100 Subject: [PATCH 09/11] More refactoring... --- ch14-blocks-and-procs/better_program_logger.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 5fbcbb560..544b24d58 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,5 +1,5 @@ $indent = 0 -def better_log desc, &block +def program_logger desc, &block prefix = ' '*$indent puts prefix + 'Beginning "' + desc + '"...' $indent = $indent + 1 @@ -8,14 +8,14 @@ def better_log desc, &block puts prefix + '..."' + desc + '" finished, returning: ' + "#{result}" end -better_log 'outer block' do - better_log 'some little block' do - better_log 'teen-tiny block' do +program_logger 'outer block' do + program_logger 'some little block' do + program_logger 'teen-tiny block' do 'lots of love' end 42 end - better_log 'yet another block' do + program_logger 'yet another block' do 'I like Indian food!' end "0" == 0 From a81077610a99a602038e10d9321cfe1b142b42a5 Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Mon, 12 Sep 2016 07:52:02 +0100 Subject: [PATCH 10/11] Last minute tweaks in relation to rspec error messages --- ch14-blocks-and-procs/program_logger.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 3ec9aea62..04cbf40ad 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,16 +1,14 @@ def program_log desc, &block puts 'Beginning "' + desc + '"...' - output = block.call - puts '..."' + desc + '" finished, returning: ' + output.to_s + result = block.call + puts '..."' + desc + '" finished, returning: ' + result.to_s end - program_log 'outer block' do program_log 'some little block' do - 5 + 1**1 + 2**2 end - - log 'yet another block' do - "I like Thai food!" + program_log 'yet another block' do + '!doof iahT ekil I'.reverse end - 16 == 8*2 +'0' == 0 end From c34da983bc4667f1f15a3cec6dbdd71516157260 Mon Sep 17 00:00:00 2001 From: Liam Taylor Date: Mon, 12 Sep 2016 08:53:23 +0100 Subject: [PATCH 11/11] Penultimate(?) commit... --- .../extend_built_in_classes.rb | 62 +++---------------- ch13-creating-new-classes/orange_tree.rb | 48 +++++++------- .../even_better_profiling.rb | 2 +- 3 files changed, 32 insertions(+), 80 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index aa0f07a14..75ed62592 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -17,56 +17,19 @@ def shuffle end class Integer -def to_roman - thousands_to_convert = (self / 1000) - hundreds_to_convert = (self % 1000 / 100) - tens_to_convert = (self % 100 / 10) - units_to_convert = self % 10 - -if thousands_to_convert <= 3 - converted_thous = "M" * thousands_to_convert -end - -if hundreds_to_convert <= 3 - converted_huns = "C" * hundreds_to_convert -elsif hundreds_to_convert == 4 - converted_huns = 'C' + 'D' -elsif hundreds_to_convert == 5 - converted_huns = 'D' -elsif hundreds_to_convert > 5 && hundreds_to_convert < 9 - converted_huns = 'D' + "C" * (hundreds_to_convert - 5) -elsif hundreds_to_convert == 9 - converted_huns = 'C' + "M" -end - -if tens_to_convert <= 3 - converted_tens = "X" * tens_to_convert -elsif tens_to_convert == 4 - converted_tens = 'X' + 'L' -elsif tens_to_convert == 5 - converted_tens = 'L' -elsif tens_to_convert > 5 && tens_to_convert < 9 - converted_tens = 'L' + "X" * (tens_to_convert - 5) -elsif tens_to_convert == 9 - converted_tens = 'X' + "C" -end -if units_to_convert <= 3 - converted_units = "I" * units_to_convert -elsif units_to_convert == 4 - converted_units = 'I' + 'V' -elsif units_to_convert == 5 - converted_units = 'V' -elsif units_to_convert > 5 && units_to_convert < 9 - converted_units = 'V' + "I" * (units_to_convert - 5) -elsif units_to_convert == 9 - converted_units = 'I' + "X" +def to_roman + 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 ) + roman end -converted_num = converted_thous + converted_huns + converted_tens + converted_units -puts converted_num -converted_num -end def factorial if self < 0 @@ -79,9 +42,4 @@ def factorial self * (self-1).factorial end end - end - - -114.to_roman -puts 114.factorial diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 36b551bb7..e0f0d95e2 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -1,10 +1,3 @@ -# 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 @@ -18,9 +11,9 @@ def initialize def height if @life_status == true - puts "The tree is #{@height} metres tall" + return @height else - puts "A dead tree is not very tall. :(" + return "A dead tree is not very tall. :(" end end @@ -29,48 +22,49 @@ def one_year_passes @tree_age = @tree_age + 1 @oranges_on_tree = 0 @height = (@height + @growth_per_year).round(1) - if @tree_age >=6 + if @tree_age > 25 + @life_status = false + return 'Oh, no! The tree is too old, and has died. :(' + elsif @tree_age >=5 @oranges_on_tree = (@oranges_on_tree + (@height * 15 - 25)).round(0) - puts "This year your tree grew to #{@height}m tall, and produced #{@oranges_on_tree} oranges." - else - puts "The tree is now #{@tree_age} years old. This year your tree grew to #{@height}m tall but isn't old enough to bear fruit." + return "This year your tree grew to #{@height}m tall, and produced #{@oranges_on_tree} oranges." + else @tree_age <5 + return "This year your tree grew to #{@height}m tall but is still too young to bear fruit." end else - puts "The tree is still dead..." - end - if @tree_age == 25 - @life_status = false - puts 'Oh, no! The tree is too old, and has died. :(' + return "A year later, the tree is still dead. :(" end + end def count_the_oranges if @life_status == true if @oranges_on_tree > 0 - puts "There are #{@oranges_on_tree.to_i} oranges on the tree" + return @oranges_on_tree else - puts "There are no oranges on the tree." + return "There are no oranges on the tree." end else - puts "A dead tree has no oranges. :(" + return "A dead tree has no oranges. :(" end end -def pick_orange +def pick_an_orange if @life_status == true if @oranges_on_tree > 0 @oranges_on_tree = @oranges_on_tree - 1 - puts "Mmmm, tasty!" - else puts "There are no oranges to pick!" + return "Mmmm, tasty!" + else + return "There are no oranges to pick!" end else - puts "A dead tree has nothing to pick. :(" + return "A dead tree has nothing to pick. :(" end end end -clurr = OrangeTree.new +ot = OrangeTree.new 23.times do - clurr.one_year_passes + ot.one_year_passes end diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index d7215cf24..133aa67d5 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,5 +1,5 @@ def profile block_description, &block - profiling_on = false + profiling_on = true if profiling_on start_time = Time.new block.call