diff --git a/Gemfile b/Gemfile index 2dac66f..6c7d0f5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,5 @@ source 'https://rubygems.org' -ruby '2.0.0' +ruby '2.2.1' -gem 'rspec', '~> 3.0.0.beta2' \ No newline at end of file +gem 'rspec' +gem 'pry' diff --git a/Gemfile.lock b/Gemfile.lock index dfd2ba5..998deac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,12 @@ GEM remote: https://rubygems.org/ specs: + coderay (1.1.2) diff-lcs (1.2.5) + method_source (0.9.0) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) rspec (3.0.0) rspec-core (~> 3.0.0) rspec-expectations (~> 3.0.0) @@ -19,4 +24,11 @@ PLATFORMS ruby DEPENDENCIES - rspec (~> 3.0.0.beta2) + pry + rspec + +RUBY VERSION + ruby 2.2.1p85 + +BUNDLED WITH + 1.16.1 diff --git a/lib/deaf_grandma.rb b/lib/deaf_grandma.rb index 83ad52a..2565f02 100644 --- a/lib/deaf_grandma.rb +++ b/lib/deaf_grandma.rb @@ -1,40 +1,67 @@ -#This script is different than FizzBuzz. It should accept user input from the terminal if done correctly. Run it to see what it does then complete the speak method so that it returns & prints the correct thing. +#This script is different than FizzBuzz. It should accept user input from the terminal if done correctly. +#Run it to see what it does then complete the speak method so that it returns & prints the correct thing. -#CAREFUL! This script will not exit. Do you know why? You may have to close it with `Ctrl-C` (Mac) if you do not insert an `exit` into your speak method. +#CAREFUL! This script will not exit. Do you know why? You may have to close it with `Ctrl-C` (Mac) +#if you do not insert an `exit` into your speak method. class DeafGrandma + attr_accessor :bye_counter def initialize - @bye_counter = 0 + @bye_counter = 0 end def run! print_welcome loop do - user_input = get_user_input - p speak(user_input) - end + user_input = get_user_input + process_user_input(user_input) + end end + def process_user_input(input) + if input == "BYE" + @bye_counter += 1 + if @bye_counter == 3 + abort("SEE YOU LATER SONNY!") + end + else + speak(input) + end + end def speak(input) + @input = input + if speak_softly? + "SPEAK UP SONNY!" + elsif yell? + "NOT SINCE 1964!" + end + end - #Implement your code here <<<<<<<<< + private + def get_user_input + print "> " + gets.chomp.gsub(/\s+/, "") end - private + def input_scanner + @input.scan(/[A-Z]/).length + end def print_welcome puts "\nSpeak to your Grandmother: " end - def get_user_input - print "> " - gets.chomp + def speak_softly? + input_scanner == 0 end + def yell? + input_scanner == @input.length + end end #Uncomment this next line to run your script but BE SURE to comment it, before you try and run your tests. diff --git a/lib/fizzbuzz.rb b/lib/fizzbuzz.rb index 86f9def..d379fe0 100644 --- a/lib/fizzbuzz.rb +++ b/lib/fizzbuzz.rb @@ -1,9 +1,15 @@ class SuperFizzBuzz def run(input) - - #Implement your code here - + if (input % 3 == 0) and (input % 5 == 0) + "FizzBuzz" + elsif (input % 3 == 0) + "Fizz" + elsif (input % 5 == 0) + "Buzz" + else + input + end end end diff --git a/spec/deaf_grandma_spec.rb b/spec/deaf_grandma_spec.rb index cf09766..aa01ff0 100644 --- a/spec/deaf_grandma_spec.rb +++ b/spec/deaf_grandma_spec.rb @@ -1,18 +1,20 @@ require 'rspec' require 'deaf_grandma' +require 'pry' describe 'DeafGrandma' do let(:script) { DeafGrandma.new } it "says 'SPEAK UP SONNY!' when we speak regularly" do - expect(script.speak("Hi Grandma")).to eq "SPEAK UP SONNY!" + expect(script.speak("speak")).to eq "SPEAK UP SONNY!" end - + it "says 'NOT SINCE 1964!' when we yell" do - #implement your test here + expect(script.speak("YELL")).to eq "NOT SINCE 1964!" end - it "EXTRA CREDIT: How would you test yelling BYE?" do - #implement your test here + it "says 'SEE YOU LATER SONNY!' when we say 'BYE' 3 times" do + script.bye_counter = 2 + expect(script.process_user_input("BYE")).to eq "SEE YOU LATER SONNY!" end end diff --git a/spec/fizzbuzz_spec.rb b/spec/fizzbuzz_spec.rb index cfda46b..538fadd 100644 --- a/spec/fizzbuzz_spec.rb +++ b/spec/fizzbuzz_spec.rb @@ -4,19 +4,20 @@ describe 'SuperFizzBuzz' do let(:script) { SuperFizzBuzz.new } + it "returns 'Fizz' when my input is divisible by 3" do + expect(script.run(3)).to eq "Fizz" + end + it "returns 'Fizz' when my input is divisible by 3" do expect(script.run(3)).to eq "Fizz" end - + it "returns 'Buzz' when my input is divisible by 5" do - #implement your test here + expect(script.run(5)).to eq "Buzz" end it "returns 'FizzBuzz' when input is divisible by 3 & 5" do - #implement your test here - end - - it "returns the input number when input isn't divisible by 3, 5, or both" do - #implement your test here + expect(script.run(15)).to eq "FizzBuzz" end + end