diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..bc8515457 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,30 @@ 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 + +puts "Hello, and thank you for..." +puts +ask "Do you like eating tacos?" # Ignore this return value +ask "Do you like eating burritos?" # And this one +wets_bed = ask "Do you wet the bed?" # Save this return value +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..7390aaa18 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,20 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + # reference: + # I = 1, V = 5, X = 10, L = 50 + # C = 100, D = 500, M = 1000 + + 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) / 1 )) + + #puts "The number #{num}, is converted to:\n#{roman} in roman numerals" + roman +end + +puts old_roman_numeral 555 diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..7cdc9c366 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,54 @@ def roman_numeral num - # your code here -end \ No newline at end of file + # reference: + # I = 1, V = 5, X = 10, L = 50 + # C = 100, D = 500, M = 1000 + + thous = (num / 1000) + hunds = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10 ) + + roman = 'M' * thous # start building the roman number + # starting with the largest (thousands) + + + # conversion code for integer to roman is the same + # as the old_school_roman_numerals + + # 9 takes 1 before 10 + # 4 takes 1 before 5 + # giving the code instructions for case of 9 and 4 for each group + # respectively (hundreds, tens, ones) + + + 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 + diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..b383f90a8 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,6 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + arr.sort_by { |word| word.downcase } +end + +#words = ['Can','feel','singing.','like','A','can',"b","B"] +#puts dictionary_sort words \ No newline at end of file diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..482a871aa 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,82 @@ def english_number number - # your code here + if number < 0 # No negative numbers. + return "Please enter a number that isn't negative." + elsif number == 0 + return "zero" + end + + num_string = "" # start with empy number 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 = number + + while zillions.length > 0 + zil_pair = zillions.pop #removes last array pair until reaches correct + zil_name = zil_pair[0] # takes the name of number + + # how many + zil_base = 10 ** zil_pair[1] + write = left_number/zil_base + left_number = left_number - write*zil_base + + if write > 0 + prefix = english_number write # call method + num_string = num_string + prefix + " " + zil_name + if left_number > 0 + + num_string = num_string + " " # building the number + end + end + + end + + write = left_number/10 + left_number = left_number - write*10 + if write > 0 + if ((write == 1) and (left_number > 0)) + num_string = num_string + teenagers[left_number-1] + left_number = 0 + else + num_string = num_string + tens_place[write-1] + end + if left_number > 0 + num_string = num_string + "-" + end + end + write = left_number + left_number = 0 + if write > 0 + num_string = num_string + ones_place[write-1] + end + + num_string end + +#puts english_number(101) +#puts english_number(234) +#puts english_number(3211) +#puts english_number(999999) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..b62502a52 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,99 @@ -# 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." + elsif number == 0 + return "zero" + end + + num_string = "" # start with empy number 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 = number + + while zillions.length > 0 + zil_pair = zillions.pop #removes last array pair until reaches correct + zil_name = zil_pair[0] # takes the name of number + + # how many + zil_base = 10 ** zil_pair[1] + write = left_number/zil_base + left_number = left_number - write*zil_base + + if write > 0 + prefix = english_number write # call method + num_string = num_string + prefix + " " + zil_name + if left_number > 0 + + num_string = num_string + " " # building the number + end + end + + end + + write = left_number/10 + left_number = left_number - write*10 + if write > 0 + if ((write == 1) and (left_number > 0)) + num_string = num_string + teenagers[left_number-1] + left_number = 0 + else + num_string = num_string + tens_place[write-1] + end + if left_number > 0 + num_string = num_string + "-" + end + end + write = left_number + left_number = 0 + if write > 0 + num_string = num_string + ones_place[write-1] + end + + num_string +end + +# use any number below to set the lyrics of the song +num_at_start = 99 # 99 beers! +num_at_start = 5 # 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!" + +num_now = num_at_start + +# using a while loop to call the enlish_number function +# converting number to word_number for a given number +# up to 3 +# then change text for last 2 bottles + +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..da197f90d 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 +def shuffle(array) + array.shuffle +end + + +#test_array = ["hi",5,"6",["hey","you"],"Einstein"] +#puts shuffle(test_array) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..a58e9e6f2 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,28 @@ def sort arr - # your code here -end \ No newline at end of file + recursive_sort(arr, []) +end + +def recursive_sort(unsorted_arr, sorted_arr) + if unsorted_arr.length < 1 + return sorted_arr + end + + last_unsorted_word = unsorted_arr.pop + new_arr = [] + + unsorted_arr.each do |word| + if word < last_unsorted_word + new_arr << last_unsorted_word + last_unsorted_word = word + else + new_arr << word + end + end + + sorted_arr << last_unsorted_word + recursive_sort(new_arr, sorted_arr) +end + +#test_array = ["orange","apple","banana","cherry","orange","watermelon"] +#puts test_array.length +#puts sort(test_array) \ 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..1b9a6288c 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,38 @@ def music_shuffle filenames - # your code here + + filenames = filenames.sort + len = filenames.length + + # shuffle twice. + 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 diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..0cfe8b797 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,47 @@ -# your code here \ No newline at end of file +# shuffle method + +def shuffle arr +shuf = [] # bulding array +while arr.length > 0 + +# pick one random element from the array. +rand_index = rand(arr.length) +# Now go through each item in the array, +# putting them all into new_arr except for the +# randomly chosen one, which goes into shuf. +curr_index = 0 +new_arr = [] + +arr.each do |item| + if curr_index == rand_index + shuf.push item # a random file is pushed + else + new_arr.push item + end + curr_index = curr_index + 1 +end +# Replace the original array with the new, +# smaller array. + +arr = new_arr +end +shuf +end + +############################ +# creates a "playlist" file +# that lists in random order +# the flac music files in the +# current directory +# run this .rb file from the folder +# you want the playlist to be made + + +all_flac = shuffle(Dir['**/*.flac']) + +File.open 'playlist.m3u', 'w' do |f| + all_flac.each do |flac_files| + f.write flac_files+"\n" + end +end +puts "Done! See current working folder to see the playlist file" \ No newline at end of file diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..b6e33df3b 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,93 @@ -# your code here \ No newline at end of file +### Download pictures from camera card + +require "win32ole" + +STDOUT.sync = true +Thread.abort_on_exception = true + +# output dir +Dir.chdir 'C:\Users\Dionysis\Ruby\AA' + +# source files +pic_names = Dir['E:/**/*.{jpg,mp4}'] +thm_names = Dir['E:/**/*.{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,mp4}'] + thm_names += Dir[x.DriveLetter+':/**/*.{thm}'] + end +end + +months = %w(jan feb mar apr may jun jul aug sept oct nov dec) + +encountered_error = false + +print "Downloading #{pic_names.size} files: " + +pic_names.each do |name| + print "." + is_movie = (name[-3..-1].downcase == "mp4" ) + + if is_movie + orientation = 0 + new_name = File.open(name) do |f| + f.seek(0x144, IO::SEEK_SET) + f.read(20) + end + + mew_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) |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.mp4" : ".jpg") + # do not overwrite files + while FileTest.exists? save_name + new_name += "a" + save_name = new_name + (is_movie ? ".orig.mp4" : ".jpg") + end + + case orientation + when 6 + 'convert "#{name}" -rotate "90>" "#{save_name}"' + when 8 + 'convert "#{name}" -rotate -"90>" "#{save_name}"' + File.delete name + else + File.rename name, save_name + 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 +# the error message before the window closes. + +if encountered_error + puts + puts + "Press [Enter] to finish." + puts + gets +end diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..f0c9269bb 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,24 @@ -# 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 += 1 + end + + name = line[0..(first_comma - 1)] + date = line[-12..-1] + + birth_dates[name] = date +end + +puts "Type a name to see their Birthday date" +name = gets.chomp +date = birth_dates[name] + +if date == nil + puts "Unknown" +else + puts date[0..5] +end \ No newline at end of file diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..ec1ecbd62 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 "Hi, please answer the following giving the numerical format of Calendars" +puts "What year were you born?" +year = gets.chomp +puts "And what month?" +month = gets.chomp +puts "And on what day?" +day = gets.chomp +birthday = Time.gm(year,month,day) +time_now =Time.new +seconds_old = time_now - birthday +puts "You are #{seconds_old} seconds old" +puts + +years_old = 1 +while Time.gm(year.to_i + years_old, month, day) <= time_now + puts "SPANK!" + years_old += 1 +end \ 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..66e739fde 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_born = Time.gm(1993,3,22) +puts time_born +time_born_billion_seconds = time_born + (1 * 10 ** 9) +puts time_born_billion_seconds \ 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..bf7092a6b 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,31 @@ -def roman_to_integer roman - # your code here +def roman_to_integer(roman) + rom_int = {"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 -= 1 + val = rom_int[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 \ 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..b6a60fa21 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,28 @@ +class Array + def shuffle + sort_by{rand} + end +end + 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 diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..ad6ad35dc 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,119 @@ -# your code here \ No newline at end of file +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 + +puts "Name your baby dragon?" +name = gets.chomp +pet = Dragon.new name + +while true +puts "commands: feed, toss, walk, rock, put to bed, exit" +command = gets.chomp + 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 "Please type one of the commands given above." + end +end \ No newline at end of file diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..6312e869b 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,72 @@ class OrangeTree - # your code here + def initialize + @age = 0 # initial age of tree + @height = 0 # initial height of tree + @orange_count = 0 # initially no oranges on tree + end + + def one_year_passes + @age = @age + 1 # annual age growth + @height = (@height + 0.4).round(1) # annual year growth + + if @age <= 5 + @orange_count = 0 + elsif @age <= 25 + @orange_count = (@height * 15 - 25) # orange production + "This year your tree grew to #{@height}m tall, and produced #{@orange_count.round} oranges." + elsif @age == 26 + "Oh, no! The tree is too old, and has died. :(" + else + "A year later, the tree is still dead. :(" + end + end + + + def height + if @age <= 25 + @height + else + "A dead tree is not very tall. :(" + end + end + + def count_the_oranges + if @age <= 25 + @orange_count.round + else + "A dead tree has no oranges. :(" + end + end + + def pick_an_orange + if @age <=25 + @orange_count = @orange_count - 1 + "You picked up an orange!" + else + "A dead tree has nothing to pick. :(" + end + end end + +=begin + +ot = OrangeTree.new +20.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) + +=end diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..4dd68bb5c 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 +$intend_line = 0 + +def better_log desc, &block + intend = " " * $intend_line + puts intend + 'Beginning "' + desc + '"...' + $intend_line = $intend_line + 1 + result = block.call + $intend_line = $intend_line - 1 + puts intend + '..."' + desc + '" finished, returning: ' + result.to_s +end + +better_log "outer blog" do + better_log "addition" do + 1+2+3+4+5 + end + +better_log "names start with a capital letter!" do + "My name is " +"dionysis".capitalize! + end + + "2" != 2 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..7708b7687 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,16 @@ -def profile block_description, &block - # your code here -end \ No newline at end of file +def profile block_description, &block +# To turn profiling on/off, set this +# to true/false. + + profiling_on = true # true for on, false for off + + if profiling_on + start_time = Time.new # time block is called + block.call # call the block liner + duration = Time.new - start_time # time taken for block to execute + puts "#{block_description}: #{duration} seconds" + else + block.call # call block without calculating the time taken + end +end + diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..ec8c52688 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,20 @@ def grandfather_clock &block - # your code here + hour = Time.new.hour # current hour + + #convert hour format to 12 hours + if hour >= 13 + hour = hour - 12 + end + + if hour == 0 || hour == 24 # at 0 and 24, the hour is 12 + hour = 12 + end + + hour.times do # how many times the clock should sound + block.call + 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..21c142dfb 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 +def program_log desc, &block + puts 'Beginning "' + desc + '"...' + result = block.call + puts '..."' + desc + '" finished, returning: ' + result.to_s +end + +program_log "outer blog" do + program_log "addition" do + 1+2+3+4+5 + end + +program_log "names start with a capital letter!" do + "My name is " +"dionysis".capitalize! + end + + "2" != 2 end \ No newline at end of file diff --git a/note_to_makers.txt b/note_to_makers.txt new file mode 100644 index 000000000..767fce6c6 --- /dev/null +++ b/note_to_makers.txt @@ -0,0 +1,5 @@ +For the rspec tests for Chapter 14, the pre-defined method's names (in the original file) +had to be changed in order to match the expected methods in the spec file. + +program_logger: "log" to "program_log" +better_program_logger: "log" to "better_log" \ No newline at end of file diff --git a/testFile b/testFile new file mode 100644 index 000000000..e69de29bb