From e0440da1c27df4d65a49ad4ce602ed12e1ff7106 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Thu, 8 Sep 2016 11:19:54 +0100 Subject: [PATCH 01/24] initial push --- push | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 push diff --git a/push b/push new file mode 100644 index 000000000..e69de29bb From 1f19f7ac147a556c818e4c6526ab1e9bffe6988b Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Thu, 8 Sep 2016 15:26:05 +0100 Subject: [PATCH 02/24] ask.rb modified --- ch09-writing-your-own-methods/ask.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..56b116edd 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,13 @@ 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 + else + puts 'Please answer "yes" or "no".' + end +end +end From 4fa9da7fb0193a250ebe0cf6fa6cd67269d025ef Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Fri, 9 Sep 2016 10:03:15 +0100 Subject: [PATCH 03/24] old school roman modified --- .../old_school_roman_numerals.rb | 14 ++++++++++++-- 1 file changed, 12 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..9cee784bf 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,13 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + +oldrom = "" + +oldrom = oldrom + 'M' * (num / 1000) +oldrom = oldrom + 'D' * (num % 1000 / 500) +oldrom = oldrom + 'C' * (num % 500 / 100) +oldrom = oldrom + 'L' * (num % 100 / 50) +oldrom = oldrom +'X' * (num % 50 / 10) +oldrom = oldrom + 'V' * (num % 10 / 5) +oldrom = oldrom + 'I' * (num % 5 / 1) +oldrom +end From 475eb631f49109b9d772ba00d777638db3ef7cdc Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Fri, 9 Sep 2016 11:33:04 +0100 Subject: [PATCH 04/24] new roman numerals modified --- .../roman_numerals.rb | 43 ++++++++++++++++++- 1 file changed, 41 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..a297cf836 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,42 @@ def roman_numeral num - # your code here -end \ No newline at end of file + #changes only to 4 and 9 in each tier + + newrom = "" + + thou = (num /1000) + hund = (num %1000 / 100) + ten = (num % 100 /10) + one = (num % 10) + + newrom = 'M' * thou + + if hund == 9 + newrom = newrom + 'CM' + elsif hund == 4 + newrom = newrom + 'CD' + else + newrom = newrom + 'D' * (num % 1000 / 500) + newrom = newrom + 'C' * (num % 500/ 100) + end + + if ten == 9 + newrom = newrom + 'XC' + elsif ten == 4 + newrom = newrom + 'XL' + else + newrom = newrom + 'L' * (num % 100 / 50) + newrom = newrom + 'X' * (num % 50 / 10) + end + + if one == 9 + newrom = newrom + 'IX' + elsif one == 4 + newrom = newrom + 'IV' + else + newrom = newrom + 'V' * (num % 10 / 5) + newrom = newrom + 'I' * (num % 5 / 1) + end + newrom +end + +puts roman_numeral 1992 From 0fb703f81d8a6f4c99d9eb7696f5e41f3e0546ba Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Fri, 9 Sep 2016 16:03:12 +0100 Subject: [PATCH 05/24] shuffle CP --- ch10-nothing-new/shuffle.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..076d0f212 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,25 @@ def shuffle arr - # your code here -end \ No newline at end of file + shuf = [] + while arr.length > 0 + rand_index = rand (arr.length) + + curr_index = 0 + new_arr = [] + + arr.each do |item| + if curr_index == rand_index + shuf.push item + else + new_arr.push item + end + + curr_index = curr_index + 1 + end + + arr = new_arr + end + + shuf +end + +puts shuffle [10,11,12,13,14,15,16,17,18,19,20] From 225865a43301455a2a7adede7afc730c7450f62a Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Fri, 9 Sep 2016 18:03:36 +0100 Subject: [PATCH 06/24] dictionary_sort --- ch10-nothing-new/dictionary_sort.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..d5b2f4920 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,11 @@ def dictionary_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.downcase < middle.downcase} + more = arr.select{|x| x.downcase >= middle.downcase} + + dictionary_sort(less) + [middle] + dictionary_sort(more) +end + +puts dictionary_sort ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'] From 210d5f14017190b16c82fa7b24e5fc99c119e3cc Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sat, 10 Sep 2016 11:21:04 +0100 Subject: [PATCH 07/24] first sort --- ch10-nothing-new/sort.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..14af7b5f1 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,11 @@ 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 + +puts(sort(['i', 'feel', 'like', 'dancing']).join(' ')) From 18d1a0955b8cb9174b851df19070b8abf8743527 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sat, 10 Sep 2016 12:01:01 +0100 Subject: [PATCH 08/24] english_number --- ch10-nothing-new/english_number.rb | 86 +++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..6b52579c1 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,87 @@ def english_number number - # your code here + if number < 0 + return 'Please enter a number that isn\'t negative.' + end + if number == 0 + return 'zero' + end + + num_string = '' + + ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] + tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + + zillions = [['hundred', 2], + ['thousand', 3], + ['million', 6], + ['billion', 9], + ['trillion', 12], + ['quadrillion', 15], + ['quintillion', 18], + ['sextillion', 21], + ['septillion', 24], + ['octillion', 27], + ['nonillion', 30], + ['decillion', 33], + ['undecillion', 36], + ['duodecillion', 39], + ['tredecillion', 42], + ['quattuordecillion', 45], + ['quindecillion', 48], + ['sexdecillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novemdecillion', 60], + ['vigintillion', 63], + ['googol', 100]] + + left = number + + while zillions.length > 0 + zil_pair = zillions.pop + zil_name = zil_pair[0] + zil_base = 10 ** zil_pair[1] + write = left/zil_base + left = left - write*zil_base + + if write > 0 + prefix = english_number write + num_string = num_string + prefix + ' ' + zil_name + + if left > 0 + num_string = num_string + ' ' + end + end + end + + write = left/10 + left = left - write*10 + + if write > 0 + if ((write == 1) and (left > 0)) + num_string = num_string + teenagers[left-1] + left = 0 + else + num_string = num_string + tens_place[write-1] + end + + if left > 0 + num_string = num_string + '-' + end end + +write = left +left = 0 + +if write > 0 + num_string = num_string + ones_place[write-1] +end + +num_string +end + +puts english_number 25 +puts english_number 0 +puts english_number 1000000000 +puts english_number 55986745241657 From c29dbf4bd652a962857fb8b451ac0035ed1d4862 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sat, 10 Sep 2016 12:41:59 +0100 Subject: [PATCH 09/24] ninety_nine_bottles --- .../ninety_nine_bottles_of_beer.rb | 97 ++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..98905ccce 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,96 @@ -# your code here \ No newline at end of file +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 + +num_at_start = 10 +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!" From fa97b0428bd51445cff0b3faad76577532fc7905 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sat, 10 Sep 2016 15:34:37 +0100 Subject: [PATCH 10/24] safer_picture_downloading_solution --- .../safer_picture_downloading.rb | 99 ++++++++++++++++++- 1 file changed, 98 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..ddebd5379 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,98 @@ -# your code here \ No newline at end of file +# For Katy, with love. + +### Download pictures from camera card. + +require 'win32ole' + +STDOUT.sync = true +Thread.abort_on_exception = true + +Dir.chdir 'C:\Documents and Settings\Chris\Desktop\pictureinbox' + +# Always look here for pics. + +pic_names = Dir['!undated/**/*.{jpg,avi}'] +thm_names = Dir['!undated/**/*.{thm}' ] + +# Scan for memory cards in the card reader. +WIN32OLE.new("Scripting.FileSystemObject").Drives.each() do |x| + #driveType 1 is removable disk + if x.DriveType == 1 && x.IsReady + pic_names += Dir[x.DriveLetter+':/**/*.{jpg,avi}'] + thm_names += Dir[x.DriveLetter+':/**/*.{thm}' ] + end +end + +months = %w(jan feb mar apr may jun jul aug sep oct nov dec) + +encountered_error = false + +print "Downloading #{pic_names.size} files: " + +pic_names.each do |name| + print '.' + is_movie = (name[-3..-1].downcase == 'avi') + + if is_movie + orientation = 0 + new_name = File.open(name) do |f| + f.seek(0x144,IO::SEEK_SET) + f.read(20) + end + + new_name[0...3] = '%.2d' % (1 + months.index(new_name[0...3].downcase)) + new_name = new_name[-4..-1] + ' ' + new_name[0...-5] + else + new_name, orientation = File.open(name) do |f| + f.seek(0x36, IO::SEEK_SET) + orientation_ = f.read(1)[0] + f.seek(0xbc, IO::SEEK_SET) + new_name_ = f.read(19) + [new_name_, orientation_] + end + end + + [4,7,10,13,16].each {|n| new_name[n] = '.'} + if new_name[0] != '2'[0] + encountered_error = true + puts "\n"+'ERROR: Could not process "'+name+ + '" because it\'s not in the proper format!' + next + end + + save_name = new_name + (is_movie ? '.orig.avi' : '.jpg') + # Make sure we don't save over another file!! + while FileTest.exist? save_name + new_name += 'a' + save_name = new_name + (is_movie ? '.orig.avi' : '.jpg') + end + + + case orientation + when 6 + `convert "#{name}" -rotate "90>" "#{save_name}"` + File.delete name + when 8 + `convert "#{name}" -rotate "-90>" "#{save_name}"` + File.delete name + else + File.rename name, save_name + end +end + +print "\nDeleting #{thm_names.size} THM files: " +thm_names.each do |name| + print '.' + File.delete name +end + +# If something bad happened, make sure she + +# sees the error message before the window closes. + +if encountered_error + puts + puts "Press [Enter] to finish." + puts + gets +end From 4b20bf5f090e87bfb91135fbfff902503d7296aa Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sat, 10 Sep 2016 16:28:28 +0100 Subject: [PATCH 11/24] build_your_own_playlist --- .../build_your_own_playlist.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..ac0fbae6c 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,13 @@ -# your code here \ No newline at end of file +def shuffle arr + arr.shuffle +end + +all_oggs = shuffle(Dir['**/*.ogg']) + +File.open 'playlist.m3u', 'w' do |f| + all_oggs.each do |ogg| + f.write ogg+"\n" + end +end + +puts 'Done!' From d1797b209b9784bf31a6143a6dcf10af7d2f7d51 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sat, 10 Sep 2016 17:21:46 +0100 Subject: [PATCH 12/24] build_a_better_playlist --- .../build_a_better_playlist.rb | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..f52485078 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,36 @@ def music_shuffle filenames - # your code here -end + filenames = filenames.sort + len = filenames.length + + 2.times do + l_index = 0 + r_index = len/2 + shuf = [] + + while shuf.length < len + if shuf.length%2 == 0 + shuf.push(filenames[r_index]) + r_index = r_index + 1 + else + shuf.push(filenames[l_index]) + l_index = l_index + 1 + end + end + + filenames = shuf + end + + arr = [] + cut = rand(len) + index = 0 + + while index < len + arr.push(filenames[(index+cut)%len]) + index = index + 1 + end + + arr + end + songs = ['RayCharles/HitTheRoadJack', 'RayCharles/GeorgiaOnMyMind', 'RayCharles/IGotAWoman', 'ArethaFranklin/Respect', 'ArethaFranklin/NaturalWoman', 'ArethaFranklin/ISayALittlePrayer'] + + puts(music_shuffle(songs)) From d0b08c12e33871008ac0a87e09b8f6cdbe123a9f Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 10:21:25 +0100 Subject: [PATCH 13/24] one_billion_seconds --- ch12-new-classes-of-objects/one_billion_seconds.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..784780eeb 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,4 @@ -# your code here \ No newline at end of file +time = Time.gm(1992, 8, 11, 11, 32, 36) +time2 = time + 1000000000 + +puts time2 From 9c25d64a9ec515f06559fa28f372321a2b076846 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 11:33:26 +0100 Subject: [PATCH 14/24] happy_birthday --- ch12-new-classes-of-objects/happy_birthday.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..7c784891e 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,18 @@ -# your code here \ No newline at end of file +puts "What year were you born in?" +year = gets.chomp.to_i + +puts "What month were you born in? (Jan = 1, Feb = 2, etc.)" +month = gets.chomp.to_i + +puts "On what day were you born?" +day = gets.chomp.to_i + +bdaytime = Time.local(year, month, day) +now = Time.new + +age = 1 + +while Time.local(year + age, month, day) <= now + puts "SPANK!" + age = age + 1 +end From 3ff9b16acce66d8f5fe6736189ed0cf2520507af Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 12:06:42 +0100 Subject: [PATCH 15/24] party_like_its_roman --- ...party_like_its_roman_to_integer_mcmxcix.rb | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) 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..8f113d512 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,33 @@ 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 + +puts(roman_to_integer('mcmlxxxv')) From 432439d431b0aa5dc1b1dfe55e55d6ffd5728542 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 13:15:42 +0100 Subject: [PATCH 16/24] birthday_helper --- .../birthday_helper.rb | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..8ff9d2525 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,25 @@ -# 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 "I don't know that one..." + else + puts date [0..5] + end + From f09f5830f91a1b7d01fb33ebe48c1192ada93f27 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 16:57:24 +0100 Subject: [PATCH 17/24] extend_built_in_classes --- .../extend_built_in_classes.rb | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 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..57029f1b7 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,22 @@ class Integer - # your code here -end \ No newline at end of file + def factorial + if self <=1 + 1 + else + self * (self-1).factorial + end + end + 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 / 1) + + roman + end +end From 5c20330ec976d53aa05e5f85ddb1f4de44804512 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 17:35:38 +0100 Subject: [PATCH 18/24] orange_tree --- ch13-creating-new-classes/orange_tree.rb | 55 +++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..6d449d484 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,58 @@ 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)}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 From 700ed61167ec95afe8811e2e68ca9def1c892d28 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 18:05:21 +0100 Subject: [PATCH 19/24] interactive_baby_dragon --- .../interactive_baby_dragon.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..2e7cfcc64 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,17 @@ -# your code here \ No newline at end of file +puts 'What would you like to name your baby dragon?' +name = gets.chomp +pet = Dragon.new name +obj = Object.new + +while true + puts + puts 'commands: feed, toss, walk, rock, put_to_bed, exit' + command = gets.chomp + if command == 'exit' + exit + elsif pet.respond_to?(command) && !obj.respond_to?(command) + pet.send command + else + puts 'Huh? Please type one of the commands.' + end +end From 884a6567e14c52d0600d74125f84313af8026a10 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 20:10:26 +0100 Subject: [PATCH 20/24] even_better_profiling --- ch14-blocks-and-procs/even_better_profiling.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..d7215cf24 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 = 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 From e6098fdb81bd3aa21af6d91c53c28563b9f9b559 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 20:30:41 +0100 Subject: [PATCH 21/24] grandfather_clock --- ch14-blocks-and-procs/grandfather_clock.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..705584a4b 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,19 @@ def grandfather_clock &block - # your code here -end \ No newline at end of file + hour = Time.new.hour + + if hour >= 13 + hour = hour - 12 + end + + if hour == 0 + hour = 12 + end + + hour.times do + block.call + end +end + +grandfather_clock do + puts 'Dong!' +end From 8627a01d51a2e5bc24ddfc75f88169c32eae372f Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 20:51:52 +0100 Subject: [PATCH 22/24] program_logger --- ch14-blocks-and-procs/program_logger.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..b9003e363 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,17 @@ -def log desc, &block - # your code here -end \ No newline at end of file +def program_log desc, &block + puts "Beginning #{desc.inspect}..." + result = block[] + puts "...#{desc.inspect} finished, returning: #{result}" +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 From 86f8007ec78472388ed5026dd50dbc6a20df6cd8 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 21:01:48 +0100 Subject: [PATCH 23/24] better_program_logger --- .../better_program_logger.rb | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..ed0b74364 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,25 @@ -def log desc, &block - # your code here -end \ No newline at end of file +$logger_depth = 0 + +def better_log desc, &block + + prefix = ' '*$logger_depth + puts prefix+"Beginning #{desc.inspect}..." + $logger_depth += 1 + result = block[] + $logger_depth -= 1 + puts prefix+"...#{desc.inspect} finished, returning: #{result}" +end + + better_log 'outer block' do + better_log 'some little block' do + better_log 'teeny-tiny block' do + 'lOtS oF lOVe'.downcase + end + 7 * 3 * 2 + end + + better_log 'yet another block' do + '!doof naidnI evol I'.reverse + end + '0' == "0" + end From 8f430da792a05165701c4667d56d95bc1fc968a7 Mon Sep 17 00:00:00 2001 From: Anna Lucking Date: Sun, 11 Sep 2016 21:08:56 +0100 Subject: [PATCH 24/24] updated_even_better_profiling --- ch14-blocks-and-procs/even_better_profiling.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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