diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..b92a4d625 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,15 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + + if reply == "yes" + return true + elsif reply == "no" + return false + end + + puts "Please answer \"yes\" or \"no\"." + end + answer +end 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..23e270c90 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,19 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + + x = "" + + x = x + "M" * (num / 1000) + x = x + "D" * (num % 1000 / 500) + x = x + "C" * (num % 500 / 100) + x = x + "L" * (num % 100 / 50) + x = x + "X" * (num % 50 / 10) + x = x + "V" * (num % 10 / 5) + x = x + "I" * (num % 5 / 1) + + x + +end + +puts old_roman_numeral 141 +puts old_roman_numeral 3000 +puts old_roman_numeral 6 diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..333adbd51 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,44 @@ def roman_numeral num - # your code here -end \ No newline at end of file + + thou = (num / 1000) + hund = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10) + + x = "M" * thou + + if hund == 4 + x = x + "CD" + elsif hund == 9 + x = x + "CM" + else + x = x + "D" * (num % 1000 / 500) + x = x + "C" * (num % 500 / 100) + end + + if tens == 4 + x = x + "XL" + elsif tens == 9 + x = x + "XC" + else + x = x + "L" * (num % 100 / 50) + x = x + "X" * (num % 50 / 10) + end + + if ones == 4 + x = x + "IV" + elsif ones == 9 + x = x + "IX" + else + x = x + "V" * (num % 10 / 5) + x = x + "I" * (num % 5 / 1) + end + + x # returns roman numeral +end + +# test criteria below +puts (roman_numeral 4) +puts (roman_numeral 1949) +puts (roman_numeral 2654) +puts (roman_numeral 9) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..85b45ff55 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,27 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + recursive_sort arr, [] +end + +def recursive_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end + + small = unsorted.pop + + remain_unsorted = [] + + unsorted.each do | x | + if x.downcase < small.downcase + remain_unsorted.push small + small = x + else + remain_unsorted.push x + end + end + + sorted.push small + recursive_sort remain_unsorted, sorted +end + +puts(dictionary_sort(["beans","Eggs","Brunch","apple","Acorn","badger"])) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..103f2e88e 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,117 @@ 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 = '' # String to be returned. + + 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. + 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 + # 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] + # 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] + 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] + end + + # Now we just return "num_string"... + num_string end + +puts english_number( 0) +puts english_number( 9) +puts english_number( 10) +puts english_number( 11) +puts english_number( 17) +puts english_number( 32) +puts english_number( 88) +puts english_number( 99) +puts english_number(100) +puts english_number(101) +puts english_number(234) +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..4f622a271 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,109 @@ -# your code here \ No newline at end of file +# English number: +def english_number number + 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 + +# 99 Bottles Starts Here: +start_num = 9999 +current_num = start_num + +while current_num > 2 + puts english_number(current_num).capitalize + " bottles of beer on the wall" + + english_number(current_num) + " bottles of beer!" + current_num = current_num - 1 + puts "Take one down, pass it around, " + english_number(current_num) + + " 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..566576ab1 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,26 @@ def shuffle arr - # your code here -end \ No newline at end of file + shuffled = [] + + while arr.length > 0 + rand_index = rand(arr.length) + index = 0 + new_arr = [] + + arr.each do |x| + if index == rand_index + shuffled.push x + else + new_arr.push x + end + + index = index + 1 + end + + arr = new_arr + end + + shuffled + +end + +puts(shuffle([1,2,3,4,5,6,7,8,9])) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..6571b1a1e 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,27 @@ def sort arr - # your code here -end \ No newline at end of file + recursive_sort arr, [] +end + +def recursive_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end + + small = unsorted.pop + + remain_unsorted = [] + + unsorted.each do | x | + if x < small + remain_unsorted.push small + small = x + else + remain_unsorted.push x + end + end + + sorted.push small + recursive_sort remain_unsorted, sorted +end + +puts(sort(["beans","Eggs","Brunch","apple","Acorn","badger"])) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..edfcb8baf 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,51 @@ +# solution visited - fully understood + def music_shuffle filenames - # your code here + # We don't want a perfectly random shuffle, so let's + # instead do a shuffle like card-shuffling. Let's + # shuffle the "deck" twice, then cut it once. That's + # not enough times to make a perfect shuffle, but it + # does mix things up a bit. + # Before we do anything, let's actually *sort* the + # input, since we don't know how shuffled it might + # already be, and we don't want it to be *too* random. + filenames = filenames.sort + len = filenames.length + + # Now we shuffle twice. + 2.times do + l_idx = 0 # index of next card in left pile + r_idx = len/2 # index of next card in right pile + shuf = [] + # 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 + # And cut the deck. + arr = [] + cut = rand(len) # index of card to cut at + idx = 0 + + while idx < len + arr.push(filenames[(idx+cut)%len]) + idx = idx + 1 + end + + arr end +# songs = ['aa/bbb', 'aa/ccc', 'aa/ddd', +# 'AAA/xxxx', 'AAA/yyyy', 'AAA/zzzz', 'foo/bar'] +# puts(music_shuffle(songs)) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..1d158f17b 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,36 @@ -# your code here \ No newline at end of file +# Shuffle method: +def shuffle arr + shuffled = [] + + while arr.length > 0 + rand_index = rand(arr.length) + index = 0 + new_arr = [] + + arr.each do |x| + if index == rand_index + shuffled.push x + else + new_arr.push x + end + + index = index + 1 + end + + arr = new_arr + end + + shuffled + +end + +# Playlist: +all_mp3s = shuffle(Dir['**/*.mp3']) + +File.open 'playlist.m3u', 'w' do |f| + all_mp3s.each do |mp3| + f.write mp3+"\n" + end +end + +puts "Complete!" diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..e4cc32333 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,53 @@ -# your code here \ No newline at end of file +# This is where she stores her pictures before +# she gets her YAML on and moves them to the server. +# Just for my own convenience, I'll go there now. + +Dir.chdir '/home/stevie/Test2' + +# First we find all of the pictures to be moved. +pic_names = Dir['/home/stevie/Test1/*.jpg'] + +puts 'What would you like to call this batch?' +batch_name = gets.chomp + +puts +print "Downloading #{pic_names.length} files: " + +# This will be our counter. We'll start at 1 today, +# though normally I like to count from 0. +pic_number = 1 + +pic_names.each do |name| + print '.' # This is our "progress bar". + + new_name = if pic_number < 10 + "#{batch_name}0#{pic_number}" + else + "#{batch_name}#{pic_number}" + end + + save_name = new_name + '.jpg' + while FileTest.exist? save_name + new_name += 'a' + save_name = new_name + '.jpg' + end + + # This renames the picture, but since "name" + # has a big long path on it, and "new_name" + # doesn't, it also moves the file to the + # current working directory, which is now + # Katy's PictureInbox folder. + # Since it's a *move*, this effectively + # downloads and deletes the originals. + # And since this is a memory card, not a + # hard drive, each of these takes a second + # or so; hence, the little dots let her + # know that my program didn't hose her machine. + + File.rename name, save_name + # Finally, we increment the counter. + pic_number = pic_number + 1 +end + +puts # This is so we aren't on progress bar line. +puts 'Done, cutie!' diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..16bcbcae3 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file +birth_dates = {} +File.read('birthdates.txt').each_line do |line| + line = line.chomp + first_comma = 0 + while line[first_comma] != ',' && + first_comma < line.length + first_comma = first_comma + 1 + end + + name = line[0..(first_comma - 1)] + date = line[-12..-1] + birth_dates[name] = date +end + +puts 'Whose birthday would you like to know?' +name = gets.chomp +date = birth_dates[name] + +if date == nil + puts "Oooh, I don't know that one..." +else + puts date[0..5] +end diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..ce6b560b5 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,15 @@ -# your code here \ No newline at end of file +puts "What year were you born?" +year = gets.chomp.to_i + +puts "What month were you born (1-12)?" +month = gets.chomp.to_i + +puts "And finally, on what day of that month?" +day = gets.chomp.to_i + +age = (Time.new - Time.local(year,month,day))/(60*60*24*365.25).to_i + +while age > 1 + 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..301d9f1d6 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,5 @@ -# your code here \ No newline at end of file +# I am this many seconds old: +puts((Time.new) - Time.gm(1988,9,28,13,58)) + +# I will be a billion seconds old on: +puts(Time.gm(1988,9,28,13,58) + 10**9) 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..0531696e6 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 + 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 diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..c4cef265b 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,23 @@ class Integer - # your code here -end \ No newline at end of file + + def to_roman + x = "" + x = x + "M" * (self / 1000) + x = x + "D" * (self % 1000 / 500) + x = x + "C" * (self % 500 / 100) + x = x + "L" * (self % 100 / 50) + x = x + "X" * (self % 50 / 10) + x = x + "V" * (self % 10 / 5) + x = x + "I" * (self % 5 / 1) + x + end + + def factorial + 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..90cc9e59d 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,134 @@ -# your code here \ No newline at end of file +# Dragon Class from Chapter 13: +class Dragon + + def initialize name + @name = name + @asleep = false + @stuff_in_belly = 10 # He's full. + @stuff_in_intestine = 0 # He doesn't need to go. + + 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 + # "private" means that the methods defined here are + # methods internal to the object. (You can feed your + # dragon, but you can't ask him whether he's hungry.) + + def hungry? + # Method names can end with "?". + # Usually, we do this only if the method + # returns true or false, like this: + @stuff_in_belly <= 2 + end + + def poopy? + @stuff_in_intestine >= 8 + end + + def passage_of_time + if @stuff_in_belly > 0 + # Move food from belly to intestine. + @stuff_in_belly = @stuff_in_belly - 1 + @stuff_in_intestine = @stuff_in_intestine + 1 + else # Our dragon is starving! + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name} is starving! In desperation, he ate YOU!" + exit # This quits the program. + end + if @stuff_in_intestine >= 10 + @stuff_in_intestine = 0 + puts "Whoops! #{@name} had an accident..." + end + if hungry? + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name}'s stomach grumbles..." + end + if poopy? + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name} does the potty dance..." + end + end +end + +#Interactive Baby Dragon +puts 'What would you like to name your baby dragon?' +name = gets.chomp +pet = Dragon.new name + +while true + puts + puts 'commands: feed, toss, walk, rock, put to bed, exit' + command = gets.chomp.downcase # to eliminate case sensitivity + + if command == 'exit' + exit + elsif command == 'feed' + pet.feed + elsif command == 'toss' + pet.toss + elsif command == 'walk' + pet.walk + elsif command == 'rock' + pet.rock + elsif command == 'put to bed' + pet.put_to_bed + else + puts 'Huh? Please type one of the commands.' + end +end diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..92372fbdd 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -5,7 +5,78 @@ # 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 + @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 # old oranges fall off + if @height > 10 && rand(2) > 0 + # tree dies + @alive = false + 'Oh, no! The tree is too old, and has died. :(' + elsif @height > 2 + # new oranges grow + @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)}m tall," + + " but is still too young to bear fruit." + end + else + 'A year later, the tree is still dead. :(' + end + end + + def pick_an_orange + if @alive + if @orange_count > 0 + @orange_count = @orange_count - 1 + 'You pick a juicy, delicious orange!' + else + 'You search every branch, but find no oranges.' + end + else + 'A dead tree has nothing to pick. :(' + end + end end + +# ot = OrangeTree.new +# 23.times do +# ot.one_year_passes +# end + +# puts(ot.one_year_passes) +# puts(ot.count_the_oranges) +# puts(ot.height) +# puts(ot.one_year_passes) +# puts(ot.one_year_passes) +# puts(ot.one_year_passes) +# puts(ot.one_year_passes) +# puts(ot.one_year_passes) +# puts(ot.height) +# puts(ot.count_the_oranges) +# 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..219532e28 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,28 @@ -def log desc, &block - # your code here -end \ No newline at end of file +$nesting_depth = 0 + +def better_log desc, &block + + indent = ' ' * $nesting_depth + + puts indent + 'Beginning "' + desc + '"...' + + $nesting_depth = $nesting_depth + 1 + x = block.call + + $nesting_depth = $nesting_depth - 1 + puts indent + '..."' + desc + '" finished, returning: ' + x.to_s +end + +better_log 'outer block' do + better_log 'some little block' do + better_log 'teeny-tiny block' do + 'lots of love' + end + 42 +end + + better_log 'yet another block' do + 'I love Indian food!' + end + false +end diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..125c68ece 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 + on = true + if 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..f2da114c5 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,9 @@ def grandfather_clock &block - # your code here -end \ No newline at end of file + hr = (Time.new.hour + 11) % 12 + 1 + hr.times(&block) +end + +grandfather_clock { puts 'DONG!' } +grandfather_clock { puts 'BEEP!'} +grandfather_clock { puts 'AH-HA!' } +grandfather_clock { puts 'BOOM!' } diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..d1bccc320 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 +def program_log desc, &block + puts 'Beginning "' + desc + '"...' + x = block.call + puts '..."' + desc + '" finished, returning: ' + x.to_s +end + +program_log 'outer block' do + program_log 'some little block' do + 5 + end + program_log 'yet another block' do + 'I like Thai food!' + end + false +end