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 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 diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..63be22484 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 + +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 +roman_numeral 101 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"])) 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!" 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" diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..75ed62592 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,45 @@ +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 + class Integer - # your code here -end \ No newline at end of file + +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 + + +def factorial +if self < 0 + return "no" +end + +if self <=1 + 1 +else + self * (self-1).factorial +end +end +end 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..e0f0d95e2 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -1,11 +1,70 @@ -# 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 - # 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 + return @height + else + return "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 > 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) + 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 + return "A year later, the tree is still dead. :(" + end + +end + +def count_the_oranges + if @life_status == true + if @oranges_on_tree > 0 + return @oranges_on_tree + else + return "There are no oranges on the tree." + end + else + return "A dead tree has no oranges. :(" + end +end + +def pick_an_orange + if @life_status == true + if @oranges_on_tree > 0 + @oranges_on_tree = @oranges_on_tree - 1 + return "Mmmm, tasty!" + else + return "There are no oranges to pick!" + end + else + return "A dead tree has nothing to pick. :(" + end +end +end + +ot = OrangeTree.new + +23.times do + ot.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..544b24d58 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,22 @@ -def log desc, &block - # your code here -end \ No newline at end of file +$indent = 0 +def program_logger desc, &block + prefix = ' '*$indent + puts prefix + 'Beginning "' + desc + '"...' + $indent = $indent + 1 + result = block.call + $indent = $indent - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + "#{result}" +end + +program_logger 'outer block' do + program_logger 'some little block' do + program_logger 'teen-tiny block' do + 'lots of love' + end + 42 + end + program_logger '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 b01b78fd8..133aa67d5 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,11 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + 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 diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..0f8d5b664 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..04cbf40ad 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,14 @@ -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