diff --git a/.gitignore b/.gitignore index 9a2f2e013..8c0732b7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /**/.DS_Store +my_examples diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..7dad4947d 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,17 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + + return true if reply == 'yes' + return false if reply =='no' + + puts 'Please answer yes or no.' + end + answer +end + +#puts(ask ('Do you like chocolate?')) + + + 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..4b8d0c221 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 << 'M' * (num / 1000) + roman << 'D' * (num % 1000 / 500) + roman << 'C' * (num % 500 / 100) + roman << 'L' * (num % 100 / 50) + roman << 'X' * (num % 50 / 10) + roman << 'V' * (num % 10 / 5) + roman << 'I' * (num % 5) + +end + + +#puts (old_roman_numeral(2679)) + diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..afba9ba5a 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,39 @@ def roman_numeral num - # your code here -end \ No newline at end of file + thous = (num / 1000) + hunds = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10) + + roman = 'M' * thous + + if hunds == 9 + roman = roman + 'CM' + elsif hunds == 4 + roman = roman + 'CD' + else + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + end + + if tens == 9 + roman = roman + 'XC' + elsif tens ==4 + roman = roman + 'XL' + else + roman = roman + 'L' * (num % 100 / 50) + roman = roman + 'X' * (num % 50 / 10) + end + + if ones == 9 + roman = roman + 'IX' + elsif ones == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (num % 10 / 5) + roman = roman + 'I' * (num % 5 / 1) + end + roman +end + + +puts (roman_numeral(2999)) \ No newline at end of file diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..3963fabe3 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,30 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + 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 + +veg = ['carrot', 'apple', 'Pear', 'banana', 'Leek'] + +puts dictionary_sort(veg) \ No newline at end of file diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..c4ddc7082 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,96 @@ 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 + + numString = '' + + onesPlace = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] + tensPlace = ['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 + # So we don't write 'two billionfifty-one'... + num_string = num_string + ' ' + end + end + end + + write = left/10 + left = left-write*10 + + if write > 0 + if ((write == 1) and (left == 0)) + numString = numString + teenagers[left-1] + left = 0 + else + numString = numString + tensPlace[write-1] + end + if left > 0 + numString = numString + ' ' + end + end + + + write = left + left = 0 + + if write > 0 + numString = numString + onesPlace[write-1] + end + + numString end + + +puts english_number (12) +puts english_number (1) +puts english_number (0) +puts english_number (27) +puts english_number (100) +puts english_number (512) +puts english_number(3211) +puts english_number(999999) +puts english_number(1000000000000) +puts english_number(109238745102938560129834709285360238475982374561034) + diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..49ecb91ef 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,129 @@ -# your code here \ No newline at end of file +def english_number number + if number < 0 # No negative numbers. + return 'Please enter a number that isn\'t negative.' + end + if number == 0 + return 'zero' + end + + # No more special cases! No more returns! + + num_string = '' # This is the string we will return. + + ones_place = ['one', 'two', 'three', + 'four', 'five', 'six', + 'seven', 'eight', 'nine'] + + tens_place = ['ten', 'twenty', 'thirty', + 'forty', 'fifty', 'sixty', + 'seventy', 'eighty', 'ninety'] + + + teenagers = ['eleven', 'twelve', 'thirteen', + 'fourteen', 'fifteen', 'sixteen', + 'seventeen', 'eighteen', 'nineteen'] + + zillions = [['hundred', 2], + ['thousand', 3], + ['million', 6], + ['billion', 9], + ['trillion', 12], + ['quadrillion', 15], + ['quintillion', 18], + ['sextillion', 21], + ['septillion', 24], + ['octillion', 27], + ['nonillion', 30], + ['decillion', 33], + ['undecillion', 36], + ['duodecillion', 39], + ['tredecillion', 42], + ['quattuordecillion', 45], + ['quindecillion', 48], + ['sexdecillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novemdecillion', 60], + ['vigintillion', 63], + ['googol', 100]] + + # "left" is how much of the number + # we still have left to write out. + # "write" is the part we are + # writing out right now. + # write and left...get it? :) + left = number + + while zillions.length > 0 + zil_pair = zillions.pop + zil_name = zil_pair[0] + zil_base = 10 ** zil_pair[1] + write = left/zil_base # How many zillions left? + left = left - write*zil_base # Subtract off those zillions. + + if write > 0 + # Now here's the recursion: + prefix = english_number write + num_string = num_string + prefix + ' ' + zil_name + + if left > 0 + # So we don't write 'two billionfifty-one'... + num_string = num_string + ' ' + end + end + end + + write = left/10 # How many tens left? + left = left - write*10 # Subtract off those tens. + + if write > 0 + if ((write == 1) and (left > 0)) + # Since we can't write "tenty-two" instead of + # "twelve", we have to make a special exception + # for these. + num_string = num_string + teenagers[left-1] + # The "-1" is because teenagers[3] is + # 'fourteen', not 'thirteen'. + + # Since we took care of the digit in the + # ones place already, we have nothing left to write. + left = 0 + else + num_string = num_string + tens_place[write-1] + # The "-1" is because tens_place[3] is + # 'forty', not 'thirty'. + end + + if left > 0 + # So we don't write 'sixtyfour'... + num_string = num_string + '-' + end + end + + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + num_string = num_string + ones_place[write-1] + # The "-1" is because ones_place[3] is + # 'four', not 'three'. + end + + # Now we just return "num_string"... + num_string +end + +num_at_start = 9999 # change to 9999 if you want +num_now = num_at_start +while num_now > 2 + puts english_number(num_now).capitalize + ' bottles of beer on the wall, ' + + english_number(num_now) + ' bottles of beer!' + num_now = num_now - 1 + puts 'Take one down, pass it around, ' + + english_number(num_now) + ' bottles of beer on the wall!' +end + +puts "Two bottles of beer on the wall, two bottles of beer!" +puts "Take one down, pass it around, one bottle of beer on the wall!" +puts "One bottle of beer on the wall, one bottle of beer!" +puts "Take one down, pass it around, no more bottles of beer on the wall!" diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..2144b3cfc 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,7 @@ def shuffle arr - # your code here -end \ No newline at end of file + arr.shuffle +end + +veg = ['apple', 'banana', 'carrot', 'Leek', 'Pear',] + +puts(shuffle(veg)) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..ca89cfcce 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,16 @@ def sort arr - # your code here -end \ No newline at end of file + return arr if arr.length <= 1 + + middle = arr.pop + less = arr.select{|x| x < middle} + more = arr.select{|x| x >= middle} + + sort(less) + [middle] + sort(more) +end + +fruit = ['pear', 'apple', 'grape', 'banana'] +veg = ['carrot'] + +p sort(fruit) +p sort([]) +p sort(veg) \ No newline at end of file diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..1970037e0 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,34 @@ def music_shuffle filenames - # your code here + filenames = filenames.sort + len = filenames.length + + 2.times do + l_idx = 0 + r_idx = len/2 + shuf = [] + + while shuf.length < len + if shuf.length % 2 == 0 + shuf.push(filenames[r_idx]) + r_idx = r_idx + 1 + else + shuf.push(filenames[l_idx]) + l_idx = l_idx + 1 + end + end + filenames = shuf + end + arr = [] + cut = rand(len) + idx = 0 + + while idx < len + arr.push(filenames[(idx+cut)%len]) + idx = idx + 1 end + arr +end + +music = Dir['/home/ubuntu/workspace/week2/music/**/*.mp3'] + +puts (music_shuffle(music)) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..09e514d88 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,9 @@ -# your code here \ No newline at end of file +music = Dir['/home/ubuntu/workspace/week2/music/**/*.{mp3,MP3}'].shuffle + +File.open '/home/ubuntu/workspace/week2/music/playlist.m3u', 'w' do |f| + music.each do |song| + f.write song+"\n" + end +end + +puts "Playlist created" \ No newline at end of file diff --git a/ch11-reading-and-writing/playlist.m3u b/ch11-reading-and-writing/playlist.m3u new file mode 100644 index 000000000..e69de29bb diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..188730b87 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,35 @@ -# your code here \ No newline at end of file +# your code here +Dir.chdir '/home/ubuntu/workspace/week2' + +pic_names = Dir['/home/ubuntu/workspace/week2/hedgehogs/**/*.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 '.' + + new_name = if pic_number < 10 + "#{batch_name}0#{pic_number}.jpg" + else + "#{batch_name}#{pic_number}.jpg" + end + + if File.exists?(new_name) + exit + else + File.rename name, new_name + end + +pic_number = pic_number + 1 +end + +puts +puts "All done!" + diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..db7253677 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,19 @@ -# your code here \ No newline at end of file +#read in file and convert to hash +@chrises = {} +file = File.open("../../chris_bday.txt", "r") +file.readlines.each do |line| +name, date, year = line.chomp.split(',') +@chrises [name.to_s] = date.to_s + year.to_s +end +file.close + + +#get input from user +puts "Whose birthday are you looking for?" +chris = gets.chomp + +if !@chrises.has_key?(chris) + puts "I'm sorry but I don't know that person's date of birth" +else + puts "#{chris}'s birthday will be on#{@chrises[chris][0..-6]}." +end diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..786f13426 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file +puts "What year were you born? (e.g. 1982)" +year = gets.chomp +puts "What month were you born? (e.g. if you were born in February, write '2')" +month = gets.chomp +puts "What day were you born?(e.g. if you were born on 19th of month, write '19')" +day = gets.chomp + +birthday = Time.gm(year, month, day) + +age_seconds = Time.new - birthday + +age_years = age_seconds/60/60/24/365 + +spank = age_years.to_i + +puts "Happy birthday! You are #{spank} years old!" + +spank.times do + puts "SPANK!" +end + +#puts birthday +#puts Time.new \ No newline at end of file diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..779bace44 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,6 @@ -# your code here \ No newline at end of file +# 19/06/1986, 03:30:21, GMT +birthday = Time.gm(1986,06,19, 3, 00, 22) + +one_billion = birthday + 1000000000 + +puts one_billion \ No newline at end of file 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..9c4879ee0 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 -end \ No newline at end of file + roman.upcase! + digits = {'M' => 1000, + 'D' => 500, + 'C' => 100, + 'L' => 50, + 'X' => 10, + 'V' => 5, + 'I' => 1} + + total = 0 + prev = 0 + roman.reverse.each_char do |char| + if !digits.has_key?(char) + puts "Please enter a valid roman numeral" + else + val = digits[char] + if val < prev + val *= -1 + else + prev = val + end + end + total += val + end + total +end + +puts roman_to_integer("mcmxcix") +puts roman_to_integer('MMXiv') \ No newline at end of file diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..864ba4114 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,42 @@ class Integer - # your code here -end \ No newline at end of file + def to_roman_numeral + thous = (self / 1000) + hunds = (self % 1000 / 100) + tens = (self % 100 / 10) + ones = (self % 10) + + roman = 'M' * thous + + if hunds == 9 + roman = roman + 'CM' + elsif hunds == 4 + roman = roman + 'CD' + else + roman = roman + 'D' * (self % 1000 / 500) + roman = roman + 'C' * (self % 500 / 100) + end + + if tens == 9 + roman = roman + 'XC' + elsif tens ==4 + roman = roman + 'XL' + else + roman = roman + 'L' * (self % 100 / 50) + roman = roman + 'X' * (self % 50 / 10) + end + + if ones == 9 + roman = roman + 'IX' + elsif ones == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (self % 10 / 5) + roman = roman + 'I' * (self % 5 / 1) + end + roman +end +end + +puts 12.to_roman_numeral +puts 2016.to_roman_numeral +puts 735.to_roman_numeral \ No newline at end of file diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..3a2d9a450 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,121 @@ -# your code here \ No newline at end of file +class Dragon + + def initialize name + @name = name + @asleep = false + @stuff_in_belly = 10 + @stuff_in_intestine = 0 + + puts "#{@name} is born." + end + + def feed + puts "You feed #{@name}." + @stuff_in_belly = 10 + passage_of_time + end + + def walk + puts "You walk #{@name}." + @stuff_in_intestine = 0 + passage_of_time + end + + def put_to_bed + puts "You put #{@name} to bed." + @asleep = true + 3.times do + if @asleep + passage_of_time + end + if @asleep + puts "#{@name} snores, filling the room with smoke." + end + end + if @asleep + @asleep = false + puts "#{@name} wakes up slowly." + end + end + + def toss + puts "You toss #{@name} up into the air." + puts "He giggles, which singes your eyebrows." + passage_of_time + end + + def rock + puts "You rock #{@name} gently." + @asleep = true + puts "He briefly dozes off..." + passage_of_time + if @asleep + @asleep = false + puts "...but wakes when you stop." + end + end + + private + + 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 + +puts "What would you like to call your baby dragon?" +name = gets.chomp +pet = Dragon.new name + +while true + puts "commands: feed, walk, rock, toss, put to bed, exit" + command = gets.chomp + + case command + when "feed" then pet.feed + when "walk" then pet.walk + when "rock" then pet.rock + when "toss" then pet.toss + when "put to bed" then pet.put_to_bed + when "exit" then exit +else puts "Please type a valid command" + end +end diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..a7f25d326 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,78 @@ class OrangeTree - # your code here + def initialize + @height = 0 + @orange_count = 0 + @alive = true + end + + def height + if @alive + @height.round(1) + else + 'A dead tree is not very tall. :(' + end + end + + def count_the_oranges + if @alive + @orange_count + else + 'A dead tree has no oranges. :(' + end + end + + def one_year_passes + if @alive + @height = @height + 0.4 + @orange_count = 0 + 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 grew to #{@height.round(1)}, tall, but is still to young to bear fruit." + end + else + "A year later, the tree is still dead. :(" + end + end + + def pick_an_orange + if @alive + if @orange_count > 0 + @orange_count = @orange_count - 1 + 'You pick a juicy, delicious orange!' + else + 'You search every branch but find no oranges' + end + else + 'A dead tree has nothing to pick. :(' + end + end + end + + +ot = OrangeTree.new +23.times do +ot.one_year_passes +end + +puts(ot.one_year_passes) +puts(ot.count_the_oranges) +puts(ot.pick_an_orange) +puts(ot.height) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.height) +puts(ot.count_the_oranges) +puts(ot.pick_an_orange) + + diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..c6e9b5b0a 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,24 @@ +$log_depth = 0 + def log desc, &block - # your code here + prefix = ' ' * $log_depth + puts prefix + "Beginning '#{desc}'..." + $log_depth = $log_depth + 1 + output = block.call + $log_depth = $log_depth - 1 + puts prefix + "...'#{desc}' finished, returning: #{output}" + +end + +log "outer block" do + log "some little block" do + log "teeny-tiny block" do + "lots of love" + end + 42 + end + log "yet another block" do + "I like Thai food!" + end + false end \ No newline at end of file diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..e9b9c3a1d 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,26 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + profiling = false #set to true to do profiling + if profiling + 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 + 1000000.times do + number = number + 1 + end +end diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..cebcd73d8 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 + hours = Time.new.hour + if hours <= 12 + hours.times do + block.call + end + else + hours = hours - 12 + hours.times do + block.call + end + end +end + +grandfather_clock do + puts "DONG!" 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 0e2e18d57..92381134d 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,16 @@ def log desc, &block - # your code here + puts "Beginning '#{desc}'" + output = block.call + puts "...'#{desc}' finished, returning: \n#{output}" + +end + +log "outer block" do + log "some little block" do + 5 + end + log "yet another block" do + "I like Thai food!" + end + false end \ No newline at end of file