From ce4d8174c4a0adeb213fb12fe3f81c84ba7d9ba1 Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Thu, 14 Aug 2014 02:57:06 -0700 Subject: [PATCH 01/33] Create README.md --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 000000000..9d0fa5152 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +Test First Ruby +========== + +See [testfirst.org/learn_ruby](http://testfirst.org/learn_ruby#install) for more information about how this will work. + +Basically, you will fork this repository and then clone it onto your local computer. +For each of the exercises, you will run the specs from the command line to see +what tests you need to make pass (see the link above for more info about how +to do that). You will then write the Ruby code that makes +those tests pass and move on to the next exercise. + +When you're done, push your changes to your forked repo and then submit a pull +request to [the main repo](https://github.com/vikingeducation/learn_ruby/) so we +can see your solutions and verify that you've completed the exercises. From d1f0abffda3c0ad869f91d4aeb438fc08652ccfc Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Tue, 6 Jan 2015 21:13:16 -0800 Subject: [PATCH 02/33] fix rakefile --- Gemfile | 2 +- Gemfile.lock | 24 ++++++++++++++++++++++++ Rakefile | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile index 3821439e2..b7ec1f1f4 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source "https://rubygems.org" gem "rake" -gem "rspec", ">=2.0" +gem "rspec", "~>3.0" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 000000000..903def1b0 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,24 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.2.5) + rake (10.4.2) + rspec (3.1.0) + rspec-core (~> 3.1.0) + rspec-expectations (~> 3.1.0) + rspec-mocks (~> 3.1.0) + rspec-core (3.1.7) + rspec-support (~> 3.1.0) + rspec-expectations (3.1.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.1.0) + rspec-mocks (3.1.3) + rspec-support (~> 3.1.0) + rspec-support (3.1.2) + +PLATFORMS + ruby + +DEPENDENCIES + rake + rspec (~> 3.0) diff --git a/Rakefile b/Rakefile index 22162469d..89d716ec9 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,5 @@ # This Rakefile has all the right settings to run the tests inside each lab -gem 'rspec', '~>2' + require 'rspec/core/rake_task' task :default => :spec From f44c4679b85e0a9bdeef4b35ffa294b41bf4123a Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Tue, 6 Jan 2015 21:53:22 -0800 Subject: [PATCH 03/33] update README --- README.md | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9d0fa5152..69a4b7088 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,37 @@ Test First Ruby See [testfirst.org/learn_ruby](http://testfirst.org/learn_ruby#install) for more information about how this will work. -Basically, you will fork this repository and then clone it onto your local computer. -For each of the exercises, you will run the specs from the command line to see -what tests you need to make pass (see the link above for more info about how -to do that). You will then write the Ruby code that makes -those tests pass and move on to the next exercise. - -When you're done, push your changes to your forked repo and then submit a pull -request to [the main repo](https://github.com/vikingeducation/learn_ruby/) so we -can see your solutions and verify that you've completed the exercises. + +### On a High Level + +Basically, you will fork this repository and then clone it onto your local computer. For each of the exercises, you will run the specs from the command line to see what tests you need to make pass (see the link above for more info about how to do that). You will then write the Ruby code that makes those tests pass and move on to the next exercise. + + +### Getting Started + +To get started, jump into the `/00_hello` directory and view the `hello_spec.rb` file. The comments at the top will kick things off. + +Basically, this is "error-driven development"... you'll keep running tests, hitting error messages, fixing those messages, running more tests... It is meant to not only test your Ruby skills but also get you comfortable seeing big scary looking stack traces and error messages. Most of the development you do at first will be + + +### Finishing Up + +When you're done, push your changes to your forked repo and then submit a pull request to this repo so we can see your solutions and verify that you've completed the exercises. + + +### Troubleshooting + +* Don't name any of your directories with spaces in them! It will give you horribly frustrating error messages and code hates dealing with spaces. For instance: + + ```language-bash + # BAD: + /Documents/My Homework/ruby + + # GOOD: + /Documents/my_homework/ruby + ``` + + +### Credit + +This is forked from [https://github.com/alexch/learn_ruby](https://github.com/alexch/learn_ruby), its original creator. \ No newline at end of file From 2083f1fe1f9a98d3a2479a70e62e3eef7a43c23f Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Sat, 14 Mar 2015 09:03:15 -0700 Subject: [PATCH 04/33] update be_false to be false as per rspec 3 --- 11_dictionary/dictionary_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/11_dictionary/dictionary_spec.rb b/11_dictionary/dictionary_spec.rb index 85f147ccf..9cc40d0be 100644 --- a/11_dictionary/dictionary_spec.rb +++ b/11_dictionary/dictionary_spec.rb @@ -30,19 +30,19 @@ end it 'can check whether a given keyword exists' do - @d.include?('fish').should be_false + @d.include?('fish').should be false end it "doesn't cheat when checking whether a given keyword exists" do - @d.include?('fish').should be_false # if the method is empty, this test passes with nil returned + @d.include?('fish').should be false # if the method is empty, this test passes with nil returned @d.add('fish') @d.include?('fish').should be_true # confirms that it actually checks - @d.include?('bird').should be_false # confirms not always returning true after add + @d.include?('bird').should be false # confirms not always returning true after add end it "doesn't include a prefix that wasn't added as a word in and of itself" do @d.add('fish') - @d.include?('fi').should be_false + @d.include?('fi').should be false end it "doesn't find a word in empty dictionary" do From 63220036478452d7a2a04231bf38c28abec6eef6 Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Fri, 22 May 2015 12:48:44 -0700 Subject: [PATCH 05/33] Update Gemfile --- Gemfile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Gemfile b/Gemfile index b7ec1f1f4..343874771 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,8 @@ source "https://rubygems.org" gem "rake" gem "rspec", "~>3.0" + +# So you can use binding.pry anywhere in your +# script to freeze time. This happens when the +# specs run too +gem "pry-bybug" From 15fa856224c9bc692bd34a4be8f75af5dfb31df2 Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Fri, 22 May 2015 12:51:32 -0700 Subject: [PATCH 06/33] Update rspec_config.rb --- rspec_config.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rspec_config.rb b/rspec_config.rb index fbc53440c..d3b116831 100644 --- a/rspec_config.rb +++ b/rspec_config.rb @@ -1,3 +1,6 @@ +# Allow people to use binding.pry with the pry-byebug gem +require 'pry-byebug' + RSpec.configure do |c| c.fail_fast = true c.color = true From e4f39787ff7744a3bce6f2c6eb6669e55de3a868 Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Thu, 18 Jun 2015 17:36:01 -0700 Subject: [PATCH 07/33] Update dictionary_spec.rb --- 11_dictionary/dictionary_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/11_dictionary/dictionary_spec.rb b/11_dictionary/dictionary_spec.rb index 9cc40d0be..e2bf54636 100644 --- a/11_dictionary/dictionary_spec.rb +++ b/11_dictionary/dictionary_spec.rb @@ -36,7 +36,7 @@ it "doesn't cheat when checking whether a given keyword exists" do @d.include?('fish').should be false # if the method is empty, this test passes with nil returned @d.add('fish') - @d.include?('fish').should be_true # confirms that it actually checks + @d.include?('fish').should be true # confirms that it actually checks @d.include?('bird').should be false # confirms not always returning true after add end From eca1c5efbcf44c3249940d6ffe2363752dddbb12 Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Thu, 18 Jun 2015 17:36:54 -0700 Subject: [PATCH 08/33] fix typo --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 343874771..4831cf1c1 100644 --- a/Gemfile +++ b/Gemfile @@ -6,4 +6,4 @@ gem "rspec", "~>3.0" # So you can use binding.pry anywhere in your # script to freeze time. This happens when the # specs run too -gem "pry-bybug" +gem "pry-byebug" From 6736c4a5ac1fae7cd9c39de3c43d0e59791941f5 Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Fri, 19 Jun 2015 11:42:43 -0700 Subject: [PATCH 09/33] Fix hanging chad --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 69a4b7088..75514a2fc 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Basically, you will fork this repository and then clone it onto your local compu To get started, jump into the `/00_hello` directory and view the `hello_spec.rb` file. The comments at the top will kick things off. -Basically, this is "error-driven development"... you'll keep running tests, hitting error messages, fixing those messages, running more tests... It is meant to not only test your Ruby skills but also get you comfortable seeing big scary looking stack traces and error messages. Most of the development you do at first will be +Basically, this is "error-driven development"... you'll keep running tests, hitting error messages, fixing those messages, running more tests... It is meant to not only test your Ruby skills but also get you comfortable seeing big scary looking stack traces and error messages. Most of the development you do at first will be just like this. In fact, most of *all* development is error-driven. So get comfortable with it! ### Finishing Up @@ -36,4 +36,4 @@ When you're done, push your changes to your forked repo and then submit a pull r ### Credit -This is forked from [https://github.com/alexch/learn_ruby](https://github.com/alexch/learn_ruby), its original creator. \ No newline at end of file +This is forked from [https://github.com/alexch/learn_ruby](https://github.com/alexch/learn_ruby), its original creator. From 9faf2fb2194abae4f3cb17819664ba16fce5a5fb Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Mon, 13 Jul 2015 12:26:18 -0700 Subject: [PATCH 10/33] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 75514a2fc..8e1388608 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Basically, you will fork this repository and then clone it onto your local compu ### Getting Started -To get started, jump into the `/00_hello` directory and view the `hello_spec.rb` file. The comments at the top will kick things off. +To get started, jump into the `/00_hello` directory, run `bundle install` from the command line to install the required libraries, and then view the `hello_spec.rb` file in your text editor. The comments at the top will kick things off. Basically, this is "error-driven development"... you'll keep running tests, hitting error messages, fixing those messages, running more tests... It is meant to not only test your Ruby skills but also get you comfortable seeing big scary looking stack traces and error messages. Most of the development you do at first will be just like this. In fact, most of *all* development is error-driven. So get comfortable with it! From 1c26685940e7963948f3310a612a6a0a9037b753 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sat, 29 Aug 2015 17:15:45 -0400 Subject: [PATCH 11/33] Finished hello and temperature exercise --- 00_hello/hello.rb | 7 +++++++ 01_temperature/temperature.rb | 7 +++++++ Gemfile.lock | 17 +++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 00_hello/hello.rb create mode 100644 01_temperature/temperature.rb diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..3623e57f4 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(who) + "Hello, #{who}!" +end \ No newline at end of file diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..02be3953d --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f) + ((f - 32.0) * 5.0 / 9.0) +end + +def ctof(c) + ((9.0 / 5.0) * c + 32.0) +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 903def1b0..1f62661aa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,19 @@ GEM remote: https://rubygems.org/ specs: + byebug (5.0.0) + columnize (= 0.9.0) + coderay (1.1.0) + columnize (0.9.0) diff-lcs (1.2.5) + method_source (0.8.2) + pry (0.10.1) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) + pry-byebug (3.2.0) + byebug (~> 5.0) + pry (~> 0.10) rake (10.4.2) rspec (3.1.0) rspec-core (~> 3.1.0) @@ -15,10 +27,15 @@ GEM rspec-mocks (3.1.3) rspec-support (~> 3.1.0) rspec-support (3.1.2) + slop (3.6.0) PLATFORMS ruby DEPENDENCIES + pry-byebug rake rspec (~> 3.0) + +BUNDLED WITH + 1.10.5 From 4a2c36e12a8f55e9179e20c838e711adf204b2aa Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sat, 29 Aug 2015 17:33:11 -0400 Subject: [PATCH 12/33] finished calculator exercise --- 02_calculator/calculator.rb | 36 ++++++++++++++++++++++++++++++ 02_calculator/calculator_spec.rb | 38 +++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 02_calculator/calculator.rb diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..06338660b --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,36 @@ +def add(num1, num2) + num1 + num2 +end + +def subtract(num1, num2) + num1 - num2 +end + +def sum(numlist) + result = 0 + numlist.each do |x| + result += x + end + result +end + +def multiply(numlist) + result = 1 + numlist.each do |x| + result *= x + end + result +end + +def power(numlist) + numlist[0] ** numlist[1] +end + +def factorial(num) + return 1 if num == 0 + result = 1 + (1..num).each do |x| + result *= x + end + result +end \ No newline at end of file diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index 29c91f64e..41e334b99 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -78,22 +78,40 @@ # write tests and code for the following: describe "#multiply" do + it "multiplies two numbers" do + multiply([1,2]).should == 2 + end - it "multiplies two numbers" - - it "multiplies several numbers" - + it "multiplies several numbers" do + multiply([1,2,3,4]).should == 24 + end end describe "#power" do - it "raises one number to the power of another number" + it "raises one number to the power of another number" do + power([2,4]).should == 16 + end end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" - it "computes the factorial of 1" - it "computes the factorial of 2" - it "computes the factorial of 5" - it "computes the factorial of 10" + it "computes the factorial of 0" do + factorial(0).should == 1 + end + + it "computes the factorial of 1" do + factorial(1).should == 1 + end + + it "computes the factorial of 2" do + factorial(2).should == 2 + end + + it "computes the factorial of 5" do + factorial(5).should == 120 + end + + it "computes the factorial of 10" do + factorial(10).should == 3628800 + end end From fe04c592561840aaf64ee760e7ea49b692e12435 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sat, 29 Aug 2015 17:52:24 -0400 Subject: [PATCH 13/33] finished simon_says exercise --- 03_simon_says/simon_says.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 03_simon_says/simon_says.rb diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..a60b0ae52 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,31 @@ +def echo(word) + word +end + +def shout(word) + word.upcase +end + +def repeat(word, num = 2) + ((word + " ") * num).strip +end + +def start_of_word(word, length = 1) + word[0..(length-1)] +end + +def first_word(words) + words.split(" ")[0] +end + +def titleize(words) + result = words.split(" ").each_with_index do |word, index| + little_words = ["and", "a", "or", "the", "over"] + if little_words.include?(word) && index != 0 + next + else + word[0] = word[0].upcase + end + end + result.join(" ") +end \ No newline at end of file From ed0dc396b0c6bcccf3adb5e9736f30a0cc9c06a7 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sat, 29 Aug 2015 22:40:50 -0400 Subject: [PATCH 14/33] finished pig_latin exercise --- 04_pig_latin/pig_latin.rb | 52 ++++++++++++++++++++++++++++++++++ 04_pig_latin/pig_latin_spec.rb | 9 ++++++ 2 files changed, 61 insertions(+) create mode 100644 04_pig_latin/pig_latin.rb diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..2a54abe39 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,52 @@ +def translate(input) + vowel = "aeiouAEIOU" + output = "" + punct_check = /[[:punct:]]/.match(input) + words = [] + + if punct_check + letters = input.split("") + wbuild = "" + letters.each do |c| + if /\w/.match(c) + wbuild += c + else + words.push(wbuild) if wbuild != "" + words.push(c) if c != "" + wbuild = "" + end + end + else + words = input.split(" ") + end + + words.each do |term| + if /\w/.match(term) == nil + output += term + next + end + + cap = false + if /[[:upper:]]/.match(term[0]) + cap = true + end + term = term.downcase + term_letters = term.split("") + + while !vowel.include?(term_letters[0]) + if term_letters[0] + term_letters[1] == "qu" + term_letters.push(term_letters.shift) + end + term_letters.push(term_letters.shift) + end + + term_letters[0].upcase! if cap == true + if punct_check + output += term_letters.join("") + "ay" + else + output += term_letters.join("") + "ay" + " " + end + end + + return output.strip +end \ No newline at end of file diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index 86328f2a4..2bf7ed249 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -69,4 +69,13 @@ # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course) # * retain the punctuation from the original phrase + it "capitalized words are still capitalized" do + s = translate("hello World") + s.should == "ellohay Orldway" + end + + it "retain punctuation from original phrase" do + s = translate("Hello, World!") + s.should == "Ellohay, Orldway!" + end end From db0496d88c67125b0cba33c669ae7d4da8d5fe86 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sat, 29 Aug 2015 23:01:10 -0400 Subject: [PATCH 15/33] finished silly_blocks exercise --- 05_silly_blocks/silly_blocks.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 05_silly_blocks/silly_blocks.rb diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..e7c98c6e5 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,11 @@ +def reverser + yield.split(" ").map { |word| word.reverse }.join(" ") +end + +def adder(num = 1) + yield + num +end + +def repeater(num = 1) + num.times { yield } +end \ No newline at end of file From cd131084ae2e00feb1f706c247adf42466f856a7 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sat, 29 Aug 2015 23:52:13 -0400 Subject: [PATCH 16/33] finished performance_monitor exercise --- 06_performance_monitor/performance_monitor.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 06_performance_monitor/performance_monitor.rb diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..f5c65aea2 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,12 @@ +def measure(n = 1) + dt = 0 + n.times do + t1 = Time.now + #puts t1 + yield + t2 = Time.now + #puts t2 + dt += t2 - t1 + end + dt /= n +end \ No newline at end of file From 990bb102efd552c1b2c9d42d43ba0fa65a0db6c4 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 00:02:50 -0400 Subject: [PATCH 17/33] finished hello_friend exercise --- 07_hello_friend/friend.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 07_hello_friend/friend.rb diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..a75d2b40e --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(who = nil) + if who == nil + "Hello!" + else + "Hello, " + who + "!" + end + end +end \ No newline at end of file From b6f0f633809e0b190c236136e820cfd5bbbf0dbc Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 00:31:15 -0400 Subject: [PATCH 18/33] finished book_titles exercise --- 08_book_titles/book.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 08_book_titles/book.rb diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..1f2806a6e --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,16 @@ +class Book + def initialize(title = nil) + @title = title + end + + def title + @title + end + + def title=(title) + words = title.split(" ") + words = words.each { |word| word.capitalize! if !["and","the", "a", "an", "in", "of"].include?(word) } + words[0].capitalize! + @title = words.join(" ") + end +end \ No newline at end of file From fc1a6706331852cfb2e872792edbd5868411fe4a Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 01:11:19 -0400 Subject: [PATCH 19/33] finished timer exercise --- 09_timer/timer.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 09_timer/timer.rb diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..add2ae327 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,20 @@ +class Timer + def initialize + @seconds = 0 + end + + def seconds + @seconds = 0 + end + + def seconds=(time) + @seconds = time + end + + def time_string + hr = (@seconds / 3600).to_s + min = ((@seconds / 60) % 60).to_s + sec = (@seconds % 60).to_s + output = "#{hr.rjust(2, "0")}:#{min.rjust(2, "0")}:#{sec.rjust(2, "0")}" + end +end \ No newline at end of file From cd9d11172e56ff5505209035751417b272bc7917 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 01:16:29 -0400 Subject: [PATCH 20/33] refactored timer exercise --- 09_timer/timer.rb | 10 ++-------- 10_temperature_object/temperature.rb | 0 2 files changed, 2 insertions(+), 8 deletions(-) create mode 100644 10_temperature_object/temperature.rb diff --git a/09_timer/timer.rb b/09_timer/timer.rb index add2ae327..c4be12272 100644 --- a/09_timer/timer.rb +++ b/09_timer/timer.rb @@ -1,16 +1,10 @@ class Timer - def initialize - @seconds = 0 - end + attr_accessor :seconds - def seconds + def initialize @seconds = 0 end - def seconds=(time) - @seconds = time - end - def time_string hr = (@seconds / 3600).to_s min = ((@seconds / 60) % 60).to_s diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..e69de29bb From b4b3c180ffd888a8f2f1a5eb52ef3019a966a2bc Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 02:21:43 -0400 Subject: [PATCH 21/33] finished temperuture_object exercise --- 10_temperature_object/temperature.rb | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb index e69de29bb..0b472f72c 100644 --- a/10_temperature_object/temperature.rb +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,47 @@ +class Temperature + def initialize(opt = {}) + @temp = opt + if @temp.has_key?(:f) + @temp[:c] = ftoc(@temp[:f]) + elsif @temp.has_key?(:c) + @temp[:f] = ctof(@temp[:c]) + end + end + + def in_fahrenheit + @temp[:f] + end + + def in_celsius + @temp[:c] + end + + def self.from_celsius(c) + Temperature.new(c: c) + end + + def self.from_fahrenheit(f) + Temperature.new(f: f) + end + + def ftoc(f) + ((f - 32.0) * 5.0 / 9.0) + end + + def ctof(c) + ((9.0 / 5.0) * c + 32.0) + end + +end + +class Celsius < Temperature + def initialize(num) + super(c: num) + end +end + +class Fahrenheit < Temperature + def initialize(num) + super(f: num) + end +end \ No newline at end of file From 4634f228317daa5835808eed9c29c6d5b5d055f4 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 15:24:26 -0400 Subject: [PATCH 22/33] finished dictionary exercise --- 11_dictionary/dictionary.rb | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 11_dictionary/dictionary.rb diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..5740bab53 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,44 @@ +class Dictionary + attr_accessor :entries, :keywords + + def initialize + @entries = {} + @keywords = [] + @definition = [] + end + + def add(input) + if input.is_a?(String) + @keywords.push(input) + @entries[input] = nil + elsif input.is_a?(Hash) + input.each do |key, value| + @keywords.push(key) + @definition.push(value) + @entries[key] = value + end + end + end + + def include?(key) + @keywords.include?(key) + end + + def find(term) + found = {} + found = @entries.select{ |key, value| key.include?(term) } + end + + def keywords + @keywords.sort + end + + def printable + @entries = @entries.sort + result = "" + @entries.each do |key, value| + result += "[#{key}] \"#{value}\"\n" + end + result.chomp + end +end From b7a4ee78048c7d2512326114412b72b67eb1515d Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 17:02:40 -0400 Subject: [PATCH 23/33] finished rpn_calculator exercise --- 12_rpn_calculator/rpn_calculator.rb | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 12_rpn_calculator/rpn_calculator.rb diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..af8b48cb5 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,71 @@ +class RPNCalculator + def initialize + @stack = [] + end + + def push(num) + @stack.push(num) + end + + def plus + raise "calculator is empty" if @stack.length < 2 + @stack.push(@stack.pop + @stack.pop) + end + + def minus + raise "calculator is empty" if @stack.length < 2 + num1 = @stack.pop + num2 = @stack.pop + @stack.push(num2 - num1) + end + + def divide + raise "calculator is empty" if @stack.length < 2 + num1 = @stack.pop.to_f + num2 = @stack.pop.to_f + @stack.push(num2 / num1) + end + + def times + raise "calculator is empty" if @stack.length < 2 + num1 = @stack.pop.to_f + num2 = @stack.pop.to_f + @stack.push(num2 * num1) + end + + def value + @stack.last + end + + def tokens(string) + result = string.split(" ").map do |x| + if x.match(/[+-\/*]/) + x.to_sym + else + x.to_i + end + end + result + end + + def evaluate(string) + tokens(string).each do |token| + if token.is_a?(Integer) + push(token) + elsif token.is_a?(Symbol) + case token + when :+ + plus + when :- + minus + when :/ + divide + when :* + times + end + end + end + + value + end +end From 941554e1b370c745dc322e609b2a7813d0c8f970 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 17:26:35 -0400 Subject: [PATCH 24/33] will come back to xml exercise if time allows --- 13_xml_document/xml_document.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 13_xml_document/xml_document.rb diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb new file mode 100644 index 000000000..03a10aca4 --- /dev/null +++ b/13_xml_document/xml_document.rb @@ -0,0 +1,14 @@ +class XmlDocument + def initialize + end + + def hello(attr = {}) + key = attr.keys.first.to_s + value = attr[attr.keys.first] + if attr.size == 0 + "" + else + "" + end + end +end From daef751cee24a4ae4a9f3221d6e60b4488ad6a76 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 17:39:38 -0400 Subject: [PATCH 25/33] finished array_extensions exercise --- 14_array_extensions/array_extensions.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 14_array_extensions/array_extensions.rb diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..b33f52518 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,17 @@ +class Array + + def sum + return 0 if self.empty? + return self.inject { |sum, x| sum + x } + end + + def square + return self if self.empty? + return self.map { |x| x ** 2 } + end + + def square! + return self if self.empty? + return self.map! { |x| x ** 2 } + end +end From a22557965c0a692790c1a600edef8901bca14048 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 30 Aug 2015 19:29:17 -0400 Subject: [PATCH 26/33] finished in_words exercise --- 15_in_words/in_words.rb | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 15_in_words/in_words.rb diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb new file mode 100644 index 000000000..309448ff2 --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,85 @@ +class Fixnum + def in_words + wordnum = { + 0 => "zero", + 1 => "one", + 2 => "two", + 3 => "three", + 4 => "four", + 5 => "five", + 6 => "six", + 7 => "seven", + 8 => "eight", + 9 => "nine", + 10 => "ten", + 11 => "eleven", + 12 => "twelve", + 13 => "thirteen", + 14 => "fourteen", + 15 => "fifteen", + 16 => "sixteen", + 17 => "seventeen", + 18 => "eighteen", + 19 => "nineteen", + 20 => "twenty", + 30 => "thirty", + 40 => "forty", + 50 => "fifty", + 60 => "sixty", + 70 => "seventy", + 80 => "eighty", + 90 => "ninety", + 100 => "hundred", + 1000 => "thousand", + 1000000 => "million", + 1000000000 => "billion", + 1000000000000 => "trillion" + } + + case self + when 0..19 + wordnum[self] + when 20..99 + return wordnum[self] if wordnum.has_key?(self) + ones = self % 10 + tens = self % 100 - ones + "#{wordnum[tens]} #{wordnum[ones]}" + when 100..999 + tens = self % 100 + hundreds = wordnum[self/100] + " " + wordnum[100] + if self % 100 == 0 + return hundreds + else + "#{hundreds} #{(tens % 100).in_words}".strip + end + when 1000..999999 + thousands = "#{(self / 1000).in_words} #{wordnum[1000]}" + if self % 1000 == 0 + return thousands + else + return "#{thousands} #{(self % 1000).in_words}".strip + end + when 1000000..999999999 + millions = "#{(self / 1000000).in_words} #{wordnum[1000000]}" + if self % 1000000 == 0 + return millions + else + return "#{millions} #{(self % 1000000).in_words}".strip + end + when (10 ** 9)...(10 ** 12) + billions = "#{(self / (10 ** 9)).in_words} #{wordnum[10 ** 9]}" + if self % (10 ** 9) == 0 + return billions + else + return "#{billions} #{(self % (10 ** 9)).in_words}".strip + end + when (10 ** 12)...(10 ** 15) + trillions = "#{(self / (10 ** 12)).in_words} #{wordnum[10 ** 12]}" + if self % (10 ** 12) == 0 + return trillions + else + return "#{trillions} #{(self % (10 ** 12)).in_words}".strip + end + end + end +end From 465655b9c9fac620b027fcdd6eff9eaf8b00f71c Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sat, 14 Nov 2015 15:55:16 -0500 Subject: [PATCH 27/33] changed rspec syntax to 3.0+ and finished XML exercise --- 00_hello/hello_spec.rb | 6 +- 01_temperature/temperature_spec.rb | 16 ++-- 02_calculator/calculator_spec.rb | 32 +++---- 03_simon_says/simon_says_spec.rb | 34 +++---- 04_pig_latin/pig_latin_spec.rb | 22 ++--- 05_silly_blocks/silly_blocks_spec.rb | 18 ++-- .../performance_monitor_spec.rb | 14 +-- 07_hello_friend/hello_friend_spec.rb | 4 +- 08_book_titles/book_titles_spec.rb | 18 ++-- 09_timer/timer_spec.rb | 8 +- 10_temperature_object/temperature.rb | 10 +- .../temperature_object_spec.rb | 44 +++++---- 11_dictionary/dictionary_spec.rb | 32 +++---- 12_rpn_calculator/rpn_calculator_spec.rb | 35 +++---- 13_xml_document/xml_document.rb | 75 ++++++++++++++- 13_xml_document/xml_document_spec.rb | 24 ++--- 14_array_extensions/array_extensions_spec.rb | 12 +-- 15_in_words/in_words_spec.rb | 94 +++++++++---------- 18 files changed, 285 insertions(+), 213 deletions(-) diff --git a/00_hello/hello_spec.rb b/00_hello/hello_spec.rb index 3ffd5cce4..ab64fc9c4 100644 --- a/00_hello/hello_spec.rb +++ b/00_hello/hello_spec.rb @@ -117,16 +117,16 @@ describe "the hello function" do it "says hello" do - hello.should == "Hello!" + expect(hello).to eq("Hello!") end end describe "the greet function" do it "says hello to someone" do - greet("Alice").should == "Hello, Alice!" + expect(greet("Alice")).to eq("Hello, Alice!") end it "says hello to someone else" do - greet("Bob").should == "Hello, Bob!" + expect(greet("Bob")).to eq("Hello, Bob!") end end diff --git a/01_temperature/temperature_spec.rb b/01_temperature/temperature_spec.rb index 6cad3701c..7e92a54dc 100644 --- a/01_temperature/temperature_spec.rb +++ b/01_temperature/temperature_spec.rb @@ -22,19 +22,19 @@ describe "#ftoc" do it "converts freezing temperature" do - ftoc(32).should == 0 + expect(ftoc(32)).to eq(0) end it "converts boiling temperature" do - ftoc(212).should == 100 + expect(ftoc(212)).to eq(100) end it "converts body temperature" do - ftoc(98.6).should == 37 + expect(ftoc(98.6)).to eq(37) end it "converts arbitrary temperature" do - ftoc(68).should == 20 + expect(ftoc(68)).to eq(20) end end @@ -42,19 +42,19 @@ describe "#ctof" do it "converts freezing temperature" do - ctof(0).should == 32 + expect(ctof(0)).to eq(32) end it "converts boiling temperature" do - ctof(100).should == 212 + expect(ctof(100)).to eq(212) end it "converts arbitrary temperature" do - ctof(20).should == 68 + expect(ctof(20)).to eq(68) end it "converts body temperature" do - ctof(37).should be_within(0.1).of(98.6) + expect(ctof(37)).to be_within(0.1).of(98.6) # Why do we need to use be_within? # See http://www.ruby-forum.com/topic/169330 # and http://en.wikipedia.org/wiki/IEEE_754-2008 diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index 41e334b99..bcb7d0040 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -37,39 +37,39 @@ describe "add" do it "adds 0 and 0" do - add(0,0).should == 0 + expect(add(0,0)).to eq(0) end it "adds 2 and 2" do - add(2,2).should == 4 + expect(add(2,2)).to eq(4) end it "adds positive numbers" do - add(2,6).should == 8 + expect(add(2,6)).to eq(8) end end describe "subtract" do it "subtracts numbers" do - subtract(10,4).should == 6 + expect(subtract(10,4)).to eq(6) end end describe "sum" do it "computes the sum of an empty array" do - sum([]).should == 0 + expect(sum([])).to eq(0) end it "computes the sum of an array of one number" do - sum([7]).should == 7 + expect(sum([7])).to eq(7) end it "computes the sum of an array of two numbers" do - sum([7,11]).should == 18 + expect(sum([7,11])).to eq(18) end it "computes the sum of an array of many numbers" do - sum([1,3,5,7,9]).should == 25 + expect(sum([1,3,5,7,9])).to eq(25) end end @@ -79,39 +79,39 @@ describe "#multiply" do it "multiplies two numbers" do - multiply([1,2]).should == 2 + expect(multiply([1,2])).to eq(2) end it "multiplies several numbers" do - multiply([1,2,3,4]).should == 24 + expect(multiply([1,2,3,4])).to eq(24) end end describe "#power" do it "raises one number to the power of another number" do - power([2,4]).should == 16 + expect(power([2,4])).to eq(16) end end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do it "computes the factorial of 0" do - factorial(0).should == 1 + expect(factorial(0)).to eq(1) end it "computes the factorial of 1" do - factorial(1).should == 1 + expect(factorial(1)).to eq(1) end it "computes the factorial of 2" do - factorial(2).should == 2 + expect(factorial(2)).to eq(2) end it "computes the factorial of 5" do - factorial(5).should == 120 + expect(factorial(5)).to eq(120) end it "computes the factorial of 10" do - factorial(10).should == 3628800 + expect(factorial(10)).to eq(3628800) end end diff --git a/03_simon_says/simon_says_spec.rb b/03_simon_says/simon_says_spec.rb index 8dac0b9f0..2c62be76d 100644 --- a/03_simon_says/simon_says_spec.rb +++ b/03_simon_says/simon_says_spec.rb @@ -16,27 +16,27 @@ describe "Simon says" do describe "echo" do it "should echo hello" do - echo("hello").should == "hello" + expect(echo("hello")).to eq("hello") end it "should echo bye" do - echo("bye").should == "bye" + expect(echo("bye")).to eq("bye") end end describe "shout" do it "should shout hello" do - shout("hello").should == "HELLO" + expect(shout("hello")).to eq("HELLO") end it "should shout multiple words" do - shout("hello world").should == "HELLO WORLD" + expect(shout("hello world")).to eq("HELLO WORLD") end end describe "repeat" do it "should repeat" do - repeat("hello").should == "hello hello" + expect(repeat("hello")).to eq("hello hello") end # Wait a second! How can you make the "repeat" method @@ -44,52 +44,52 @@ # # Hint: *default values* it "should repeat a number of times" do - repeat("hello", 3).should == "hello hello hello" + expect(repeat("hello", 3)).to eq("hello hello hello") end end describe "start_of_word" do it "returns the first letter" do - start_of_word("hello", 1).should == "h" + expect(start_of_word("hello", 1)).to eq("h") end it "returns the first two letters" do - start_of_word("Bob", 2).should == "Bo" + expect(start_of_word("Bob", 2)).to eq("Bo") end it "returns the first several letters" do s = "abcdefg" - start_of_word(s, 1).should == "a" - start_of_word(s, 2).should == "ab" - start_of_word(s, 3).should == "abc" + expect(start_of_word(s, 1)).to eq("a") + expect(start_of_word(s, 2)).to eq("ab") + expect(start_of_word(s, 3)).to eq("abc") end end describe "first_word" do it "tells us the first word of 'Hello World' is 'Hello'" do - first_word("Hello World").should == "Hello" + expect(first_word("Hello World")).to eq("Hello") end it "tells us the first word of 'oh dear' is 'oh'" do - first_word("oh dear").should == "oh" + expect(first_word("oh dear")).to eq("oh") end end describe "titleize" do it "capitalizes a word" do - titleize("jaws").should == "Jaws" + expect(titleize("jaws")).to eq("Jaws") end it "capitalizes every word (aka title case)" do - titleize("david copperfield").should == "David Copperfield" + expect(titleize("david copperfield")).to eq("David Copperfield") end it "doesn't capitalize 'little words' in a title" do - titleize("war and peace").should == "War and Peace" + expect(titleize("war and peace")).to eq("War and Peace") end it "does capitalize 'little words' at the start of a title" do - titleize("the bridge over the river kwai").should == "The Bridge over the River Kwai" + expect(titleize("the bridge over the river kwai")).to eq("The Bridge over the River Kwai") end end diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index 2bf7ed249..6fa540e37 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -23,46 +23,46 @@ it "translates a word beginning with a vowel" do s = translate("apple") - s.should == "appleay" + expect(s).to eq("appleay") end it "translates a word beginning with a consonant" do s = translate("banana") - s.should == "ananabay" + expect(s).to eq("ananabay") end it "translates a word beginning with two consonants" do s = translate("cherry") - s.should == "errychay" + expect(s).to eq("errychay") end it "translates two words" do s = translate("eat pie") - s.should == "eatay iepay" + expect(s).to eq("eatay iepay") end it "translates a word beginning with three consonants" do - translate("three").should == "eethray" + expect(translate("three")).to eq("eethray") end it "counts 'sch' as a single phoneme" do s = translate("school") - s.should == "oolschay" + expect(s).to eq("oolschay") end it "counts 'qu' as a single phoneme" do s = translate("quiet") - s.should == "ietquay" + expect(s).to eq("ietquay") end it "counts 'qu' as a consonant even when it's preceded by a consonant" do s = translate("square") - s.should == "aresquay" + expect(s).to eq("aresquay") end it "translates many words" do s = translate("the quick brown fox") - s.should == "ethay ickquay ownbray oxfay" + expect(s).to eq("ethay ickquay ownbray oxfay") end # Test-driving bonus: @@ -71,11 +71,11 @@ it "capitalized words are still capitalized" do s = translate("hello World") - s.should == "ellohay Orldway" + expect(s).to eq("ellohay Orldway") end it "retain punctuation from original phrase" do s = translate("Hello, World!") - s.should == "Ellohay, Orldway!" + expect(s).to eq("Ellohay, Orldway!") end end diff --git a/05_silly_blocks/silly_blocks_spec.rb b/05_silly_blocks/silly_blocks_spec.rb index 6a34b2cff..c64e3ef70 100644 --- a/05_silly_blocks/silly_blocks_spec.rb +++ b/05_silly_blocks/silly_blocks_spec.rb @@ -15,28 +15,28 @@ result = reverser do "hello" end - result.should == "olleh" + expect(result).to eq("olleh") end it "reverses each word in the string returned by the default block" do result = reverser do "hello dolly" end - result.should == "olleh yllod" + expect(result).to eq("olleh yllod") end end describe "adder" do it "adds one to the value returned by the default block" do - adder do + expect(adder do 5 - end.should == 6 + end).to eq(6) end it "adds 3 to the value returned by the default block" do - adder(3) do + expect(adder(3) do 5 - end.should == 8 + end).to eq(8) end end @@ -46,7 +46,7 @@ repeater do block_was_executed = true end - block_was_executed.should == true + expect(block_was_executed).to eq(true) end it "executes the default block 3 times" do @@ -54,7 +54,7 @@ repeater(3) do n += 1 end - n.should == 3 + expect(n).to eq(3) end it "executes the default block 10 times" do @@ -62,7 +62,7 @@ repeater(10) do n += 1 end - n.should == 10 + expect(n).to eq(10) end end diff --git a/06_performance_monitor/performance_monitor_spec.rb b/06_performance_monitor/performance_monitor_spec.rb index 04820d4f7..83ca25db1 100644 --- a/06_performance_monitor/performance_monitor_spec.rb +++ b/06_performance_monitor/performance_monitor_spec.rb @@ -20,21 +20,21 @@ it "takes about 0 seconds to run an empty block" do elapsed_time = measure do end - elapsed_time.should be_within(0.1).of(0) + expect(elapsed_time).to be_within(0.1).of(0) end it "takes exactly 0 seconds to run an empty block (with stubs)" do Time.stub(:now) { @eleven_am } elapsed_time = measure do end - elapsed_time.should == 0 + expect(elapsed_time).to eq(0) end it "takes about 1 second to run a block that sleeps for 1 second" do elapsed_time = measure do sleep 1 end - elapsed_time.should be_within(0.1).of(1) + expect(elapsed_time).to be_within(0.1).of(1) end it "takes exactly 1 second to run a block that sleeps for 1 second (with stubs)" do @@ -43,7 +43,7 @@ elapsed_time = measure do fake_time += 60 # adds one minute to fake_time end - elapsed_time.should == 60 + expect(elapsed_time).to eq(60) end it "runs a block N times" do @@ -51,7 +51,7 @@ measure(4) do n += 1 end - n.should == 4 + expect(n).to eq(4) end it "returns the average time, not the total time, when running multiple times" do @@ -61,7 +61,7 @@ average_time = measure(4) do fake_time += run_times.pop end - average_time.should == 6.5 + expect(average_time).to eq(6.5) end it "returns the average time when running a random number of times for random lengths of time" do @@ -72,7 +72,7 @@ delay = rand(10) fake_time += delay end - average_time.should == (fake_time - @eleven_am).to_f/number_of_times + expect(average_time).to eq((fake_time - @eleven_am).to_f/number_of_times) end end diff --git a/07_hello_friend/hello_friend_spec.rb b/07_hello_friend/hello_friend_spec.rb index 29b25730f..de5d4962f 100644 --- a/07_hello_friend/hello_friend_spec.rb +++ b/07_hello_friend/hello_friend_spec.rb @@ -92,10 +92,10 @@ describe Friend do it "says hello" do - Friend.new.greeting.should == "Hello!" + expect(Friend.new.greeting).to eq("Hello!") end it "says hello to someone" do - Friend.new.greeting("Bob").should == "Hello, Bob!" + expect(Friend.new.greeting("Bob")).to eq("Hello, Bob!") end end diff --git a/08_book_titles/book_titles_spec.rb b/08_book_titles/book_titles_spec.rb index d7bc9a359..72822c6b8 100644 --- a/08_book_titles/book_titles_spec.rb +++ b/08_book_titles/book_titles_spec.rb @@ -23,52 +23,52 @@ describe 'title' do it 'should capitalize the first letter' do @book.title = "inferno" - @book.title.should == "Inferno" + expect(@book.title).to eq("Inferno") end it 'should capitalize every word' do @book.title = "stuart little" - @book.title.should == "Stuart Little" + expect(@book.title).to eq("Stuart Little") end describe 'should capitalize every word except...' do describe 'articles' do specify 'the' do @book.title = "alexander the great" - @book.title.should == "Alexander the Great" + expect(@book.title).to eq("Alexander the Great") end specify 'a' do @book.title = "to kill a mockingbird" - @book.title.should == "To Kill a Mockingbird" + expect(@book.title).to eq("To Kill a Mockingbird") end specify 'an' do @book.title = "to eat an apple a day" - @book.title.should == "To Eat an Apple a Day" + expect(@book.title).to eq("To Eat an Apple a Day") end end specify 'conjunctions' do @book.title = "war and peace" - @book.title.should == "War and Peace" + expect(@book.title).to eq("War and Peace") end specify 'prepositions' do @book.title = "love in the time of cholera" - @book.title.should == "Love in the Time of Cholera" + expect(@book.title).to eq("Love in the Time of Cholera") end end describe 'should always capitalize...' do specify 'I' do @book.title = "what i wish i knew when i was 20" - @book.title.should == "What I Wish I Knew When I Was 20" + expect(@book.title).to eq("What I Wish I Knew When I Was 20") end specify 'the first word' do @book.title = "the man in the iron mask" - @book.title.should == "The Man in the Iron Mask" + expect(@book.title).to eq("The Man in the Iron Mask") end end end diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index d1baf90b3..89cc59bf6 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -21,22 +21,22 @@ describe 'time_string' do it "should display 0 seconds as 00:00:00" do @timer.seconds = 0 - @timer.time_string.should == "00:00:00" + expect(@timer.time_string).to eq("00:00:00") end it "should display 12 seconds as 00:00:12" do @timer.seconds = 12 - @timer.time_string.should == "00:00:12" + expect(@timer.time_string).to eq("00:00:12") end it "should display 66 seconds as 00:01:06" do @timer.seconds = 66 - @timer.time_string.should == "00:01:06" + expect(@timer.time_string).to eq("00:01:06") end it "should display 4000 seconds as 01:06:40" do @timer.seconds = 4000 - @timer.time_string.should == "01:06:40" + expect(@timer.time_string).to eq("01:06:40") end end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb index 0b472f72c..c44cddcb8 100644 --- a/10_temperature_object/temperature.rb +++ b/10_temperature_object/temperature.rb @@ -2,9 +2,9 @@ class Temperature def initialize(opt = {}) @temp = opt if @temp.has_key?(:f) - @temp[:c] = ftoc(@temp[:f]) + @temp[:c] = Temperature.ftoc(@temp[:f]) elsif @temp.has_key?(:c) - @temp[:f] = ctof(@temp[:c]) + @temp[:f] = Temperature.ctof(@temp[:c]) end end @@ -24,11 +24,11 @@ def self.from_fahrenheit(f) Temperature.new(f: f) end - def ftoc(f) + def self.ftoc(f) ((f - 32.0) * 5.0 / 9.0) end - def ctof(c) + def self.ctof(c) ((9.0 / 5.0) * c + 32.0) end @@ -44,4 +44,4 @@ class Fahrenheit < Temperature def initialize(num) super(f: num) end -end \ No newline at end of file +end diff --git a/10_temperature_object/temperature_object_spec.rb b/10_temperature_object/temperature_object_spec.rb index fe21c1135..b95ed92e6 100644 --- a/10_temperature_object/temperature_object_spec.rb +++ b/10_temperature_object/temperature_object_spec.rb @@ -24,44 +24,44 @@ describe "can be constructed with an options hash" do describe "in degrees fahrenheit" do it "at 50 degrees" do - Temperature.new(:f => 50).in_fahrenheit.should == 50 + expect(Temperature.new(:f => 50).in_fahrenheit).to eq(50) end describe "and correctly convert to celsius" do it "at freezing" do - Temperature.new(:f => 32).in_celsius.should == 0 + expect(Temperature.new(:f => 32).in_celsius).to eq(0) end it "at boiling" do - Temperature.new(:f => 212).in_celsius.should == 100 + expect(Temperature.new(:f => 212).in_celsius).to eq(100) end it "at body temperature" do - Temperature.new(:f => 98.6).in_celsius.should == 37 + expect(Temperature.new(:f => 98.6).in_celsius).to eq(37) end it "at an arbitrary temperature" do - Temperature.new(:f => 68).in_celsius.should == 20 + expect(Temperature.new(:f => 68).in_celsius).to eq(20) end end end describe "in degrees celsius" do it "at 50 degrees" do - Temperature.new(:c => 50).in_celsius.should == 50 + expect(Temperature.new(:c => 50).in_celsius).to eq(50) end describe "and correctly convert to fahrenheit" do it "at freezing" do - Temperature.new(:c => 0).in_fahrenheit.should == 32 + expect(Temperature.new(:c => 0).in_fahrenheit).to eq(32) end it "at boiling" do - Temperature.new(:c => 100).in_fahrenheit.should == 212 + expect(Temperature.new(:c => 100).in_fahrenheit).to eq(212) end it "at body temperature" do - Temperature.new(:c => 37).in_fahrenheit.should be_within(0.1).of(98.6) + expect(Temperature.new(:c => 37).in_fahrenheit).to be_within(0.1).of(98.6) # Why do we need to use be_within here? # See http://www.ruby-forum.com/topic/169330 # and http://groups.google.com/group/rspec/browse_thread/thread/f3ebbe3c313202bb @@ -78,13 +78,13 @@ describe "can be constructed via factory methods" do it "in degrees celsius" do - Temperature.from_celsius(50).in_celsius.should == 50 - Temperature.from_celsius(50).in_fahrenheit.should == 122 + expect(Temperature.from_celsius(50).in_celsius).to eq(50) + expect(Temperature.from_celsius(50).in_fahrenheit).to eq(122) end it "in degrees fahrenheit" do - Temperature.from_fahrenheit(50).in_fahrenheit.should == 50 - Temperature.from_fahrenheit(50).in_celsius.should == 10 + expect(Temperature.from_fahrenheit(50).in_fahrenheit).to eq(50) + expect(Temperature.from_fahrenheit(50).in_celsius).to eq(10) end end @@ -97,30 +97,36 @@ # run *all* the tests during your refactoring, to make sure you did it right # describe "utility class methods" do + it "ftoc calls properly on class" do + expect(Temperature.ftoc(32)).to eq(0) + end + it "ctof calls properly on class" do + expect(Temperature.ctof(0)).to eq(32) + end end # Here's another way to solve the problem! describe "Temperature subclasses" do describe "Celsius subclass" do it "is constructed in degrees celsius" do - Celsius.new(50).in_celsius.should == 50 - Celsius.new(50).in_fahrenheit.should == 122 + expect(Celsius.new(50).in_celsius).to eq(50) + expect(Celsius.new(50).in_fahrenheit).to eq(122) end it "is a Temperature subclass" do - Celsius.new(0).should be_a(Temperature) + expect(Celsius.new(0)).to be_a(Temperature) end end describe "Fahrenheit subclass" do it "is constructed in degrees fahrenheit" do - Fahrenheit.new(50).in_fahrenheit.should == 50 - Fahrenheit.new(50).in_celsius.should == 10 + expect(Fahrenheit.new(50).in_fahrenheit).to eq(50) + expect(Fahrenheit.new(50).in_celsius).to eq(10) end it "is a Temperature subclass" do - Fahrenheit.new(0).should be_a(Temperature) + expect(Fahrenheit.new(0)).to be_a(Temperature) end end end diff --git a/11_dictionary/dictionary_spec.rb b/11_dictionary/dictionary_spec.rb index e2bf54636..f57bb3e9d 100644 --- a/11_dictionary/dictionary_spec.rb +++ b/11_dictionary/dictionary_spec.rb @@ -14,70 +14,70 @@ end it 'is empty when created' do - @d.entries.should == {} + expect(@d.entries).to eq({}) end it 'can add whole entries with keyword and definition' do @d.add('fish' => 'aquatic animal') - @d.entries.should == {'fish' => 'aquatic animal'} - @d.keywords.should == ['fish'] + expect(@d.entries).to eq({'fish' => 'aquatic animal'}) + expect(@d.keywords).to eq(['fish']) end it 'add keywords (without definition)' do @d.add('fish') - @d.entries.should == {'fish' => nil} - @d.keywords.should == ['fish'] + expect(@d.entries).to eq({'fish' => nil}) + expect(@d.keywords).to eq(['fish']) end it 'can check whether a given keyword exists' do - @d.include?('fish').should be false + expect(@d.include?('fish')).to be false end it "doesn't cheat when checking whether a given keyword exists" do - @d.include?('fish').should be false # if the method is empty, this test passes with nil returned + expect(@d.include?('fish')).to be false # if the method is empty, this test passes with nil returned @d.add('fish') - @d.include?('fish').should be true # confirms that it actually checks - @d.include?('bird').should be false # confirms not always returning true after add + expect(@d.include?('fish')).to be true # confirms that it actually checks + expect(@d.include?('bird')).to be false # confirms not always returning true after add end it "doesn't include a prefix that wasn't added as a word in and of itself" do @d.add('fish') - @d.include?('fi').should be false + expect(@d.include?('fi')).to be false end it "doesn't find a word in empty dictionary" do - @d.find('fi').should be_empty # {} + expect(@d.find('fi')).to be_empty # {} end it 'finds nothing if the prefix matches nothing' do @d.add('fiend') @d.add('great') - @d.find('nothing').should be_empty + expect(@d.find('nothing')).to be_empty end it "finds an entry" do @d.add('fish' => 'aquatic animal') - @d.find('fish').should == {'fish' => 'aquatic animal'} + expect(@d.find('fish')).to eq({'fish' => 'aquatic animal'}) end it 'finds multiple matches from a prefix and returns the entire entry (keyword + definition)' do @d.add('fish' => 'aquatic animal') @d.add('fiend' => 'wicked person') @d.add('great' => 'remarkable') - @d.find('fi').should == {'fish' => 'aquatic animal', 'fiend' => 'wicked person'} + expect(@d.find('fi')).to eq({'fish' => 'aquatic animal', 'fiend' => 'wicked person'}) end it 'lists keywords alphabetically' do @d.add('zebra' => 'African land animal with stripes') @d.add('fish' => 'aquatic animal') @d.add('apple' => 'fruit') - @d.keywords.should == %w(apple fish zebra) + expect(@d.keywords).to eq(%w(apple fish zebra)) end it 'can produce printable output like so: [keyword] "definition"' do @d.add('zebra' => 'African land animal with stripes') @d.add('fish' => 'aquatic animal') @d.add('apple' => 'fruit') - @d.printable.should == %Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"} + expect(@d.printable).to eq(%Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"}) end end diff --git a/12_rpn_calculator/rpn_calculator_spec.rb b/12_rpn_calculator/rpn_calculator_spec.rb index 0b57f136b..a79d0a369 100644 --- a/12_rpn_calculator/rpn_calculator_spec.rb +++ b/12_rpn_calculator/rpn_calculator_spec.rb @@ -47,7 +47,7 @@ calculator.push(2) calculator.push(3) calculator.plus - calculator.value.should == 5 + expect(calculator.value).to eq(5) end it "adds three numbers" do @@ -55,16 +55,16 @@ calculator.push(3) calculator.push(4) calculator.plus - calculator.value.should == 7 + expect(calculator.value).to eq(7) calculator.plus - calculator.value.should == 9 + expect(calculator.value).to eq(9) end it "subtracts the second number from the first number" do calculator.push(2) calculator.push(3) calculator.minus - calculator.value.should == -1 + expect(calculator.value).to eq(-1) end it "adds and subtracts" do @@ -72,9 +72,9 @@ calculator.push(3) calculator.push(4) calculator.minus - calculator.value.should == -1 + expect(calculator.value).to eq(-1) calculator.plus - calculator.value.should == 1 + expect(calculator.value).to eq(1) end it "multiplies and divides" do @@ -82,9 +82,9 @@ calculator.push(3) calculator.push(4) calculator.divide - calculator.value.should == (3.0 / 4.0) + expect(calculator.value).to eq(3.0 / 4.0) calculator.times - calculator.value.should == 2.0 * (3.0 / 4.0) + expect(calculator.value).to eq(2.0 * (3.0 / 4.0)) end it "resolves operator precedence unambiguously" do @@ -94,7 +94,7 @@ calculator.plus calculator.push(3) calculator.times - calculator.value.should == (1+2)*3 + expect(calculator.value).to eq((1+2)*3) # 1 2 3 * + => 1 + (2 * 3) calculator.push(1) @@ -102,7 +102,7 @@ calculator.push(3) calculator.times calculator.plus - calculator.value.should == 1+(2*3) + expect(calculator.value).to eq(1+(2*3)) end it "fails informatively when there's not enough values stacked away" do @@ -125,23 +125,18 @@ # extra credit it "tokenizes a string" do - calculator.tokens("1 2 3 * + 4 5 - /").should == - [1, 2, 3, :*, :+, 4, 5, :-, :/] + expect(calculator.tokens("1 2 3 * + 4 5 - /")).to eq([1, 2, 3, :*, :+, 4, 5, :-, :/]) end # extra credit it "evaluates a string" do - calculator.evaluate("1 2 3 * +").should == - ((2 * 3) + 1) + expect(calculator.evaluate("1 2 3 * +")).to eq((2 * 3) + 1) - calculator.evaluate("4 5 -").should == - (4 - 5) + expect(calculator.evaluate("4 5 -")).to eq(4 - 5) - calculator.evaluate("2 3 /").should == - (2.0 / 3.0) + expect(calculator.evaluate("2 3 /")).to eq(2.0 / 3.0) - calculator.evaluate("1 2 3 * + 4 5 - /").should == - (1.0 + (2 * 3)) / (4 - 5) + expect(calculator.evaluate("1 2 3 * + 4 5 - /")).to eq( (1.0 + (2 * 3)) / (4 - 5) ) end end diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb index 03a10aca4..cb81e7778 100644 --- a/13_xml_document/xml_document.rb +++ b/13_xml_document/xml_document.rb @@ -1,14 +1,85 @@ class XmlDocument - def initialize + def initialize(indent = false) + @indent = indent + @indent_count = 0 end + def method_missing(method, hash = {}, &block) + result = "" + + if hash.empty? && block.nil? + @indent ? (" " * @indent_count) + "<#{method}/>\n" : "<#{method}/>" + elsif !hash.empty? + key = hash.keys.first.to_s + val = hash[hash.keys.first] + @indent ? "<#{method} #{key}='#{val}'/>\n" : "<#{method} #{key}='#{val}'/>" + else + if @indent + result << "<#{method}>\n" + @indent_count += 1 + result << (" " * @indent_count) + "#{yield}" + @indent_count -= 1 + result << (" " * @indent_count) + "\n" + return result + else + "<#{method}>#{yield}" + end + end + end +=begin + Found a really cool way to do this using method_missing in ruby! + Source: http://stackoverflow.com/questions/29567635/what-distinguishes-new-from-newtrue-indent-xml-document-in-test-first-rub + + + Explanation of &block in ruby + Source: http://mixandgo.com/blog/mastering-ruby-blocks-in-less-than-5-minutes + + The code below were my previous efforts in doing this exercise without the knowledge of method_missing. Preserved for posterity. +=end + +=begin def hello(attr = {}) key = attr.keys.first.to_s value = attr[attr.keys.first] - if attr.size == 0 + if block_given? + return @indent ? "\n" + yield + "\n" : "" + yield + "" + elsif attr.size == 0 "" else "" end end + + def send(name) + "<#{name}/>" + end + + def goodbye + if block_given? + "" + yield + "" + else + "" + end + end + + def come_back + if block_given? + "" + yield + "" + else + "" + end + end + + def ok_fine(attr = {}) + key = attr.keys.first.to_s + value = attr[attr.keys.first] + if block_given? + "" + yield + "" + elsif attr.size == 0 + "" + else + "" + end + end +=end end diff --git a/13_xml_document/xml_document_spec.rb b/13_xml_document/xml_document_spec.rb index 88ce3ae69..fa1995818 100644 --- a/13_xml_document/xml_document_spec.rb +++ b/13_xml_document/xml_document_spec.rb @@ -22,56 +22,56 @@ end it "renders an empty tag" do - @xml.hello.should == "" + expect(@xml.hello).to eq("") end it "renders a tag with attributes" do - @xml.hello(:name => 'dolly').should == "" + expect(@xml.hello(:name => 'dolly')).to eq("") end it "renders a randomly named tag" do tag_name = (1..8).map{|i| ('a'..'z').to_a[rand(26)]}.join - @xml.send(tag_name).should == "<#{tag_name}/>" + expect(@xml.send(tag_name)).to eq("<#{tag_name}/>") end it "renders block with text inside" do - @xml.hello do + expect(@xml.hello do "dolly" - end.should == "dolly" + end).to eq("dolly") end it "nests one level" do - @xml.hello do + expect(@xml.hello do @xml.goodbye - end.should == "" + end).to eq("") end it "nests several levels" do xml = XmlDocument.new - xml.hello do + expect(xml.hello do xml.goodbye do xml.come_back do xml.ok_fine(:be => "that_way") end end - end.should == "" + end).to eq("") end it "indents" do @xml = XmlDocument.new(true) - @xml.hello do + expect(@xml.hello do @xml.goodbye do @xml.come_back do @xml.ok_fine(:be => "that_way") end end - end.should == + end).to eq( "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + - "\n" + "\n") end end diff --git a/14_array_extensions/array_extensions_spec.rb b/14_array_extensions/array_extensions_spec.rb index a755ac899..4c6b075a2 100755 --- a/14_array_extensions/array_extensions_spec.rb +++ b/14_array_extensions/array_extensions_spec.rb @@ -12,25 +12,25 @@ describe "#sum" do it "has a #sum method" do - [].should respond_to(:sum) + expect([]).to respond_to(:sum) end it "should be 0 for an empty array" do - [].sum.should == 0 + expect([].sum).to eq(0) end it "should add all of the elements" do - [1,2,4].sum.should == 7 + expect([1,2,4].sum).to eq(7) end end describe '#square' do it "does nothing to an empty array" do - [].square.should == [] + expect([].square).to eq([]) end it "returns a new array containing the squares of each element" do - [1,2,3].square.should == [1,4,9] + expect([1,2,3].square).to eq([1,4,9]) end end @@ -38,7 +38,7 @@ it "squares each element of the original array" do array = [1,2,3] array.square! - array.should == [1,4,9] + expect(array).to eq([1,4,9]) end end diff --git a/15_in_words/in_words_spec.rb b/15_in_words/in_words_spec.rb index d9872e767..1f5768575 100644 --- a/15_in_words/in_words_spec.rb +++ b/15_in_words/in_words_spec.rb @@ -23,80 +23,80 @@ describe Fixnum do it "reads 0 to 9" do - 0.in_words.should == 'zero' - 1.in_words.should == 'one' - 2.in_words.should == 'two' - 3.in_words.should == 'three' - 4.in_words.should == 'four' - 5.in_words.should == 'five' - 6.in_words.should == 'six' - 7.in_words.should == 'seven' - 8.in_words.should == 'eight' - 9.in_words.should == 'nine' + expect(0.in_words).to eq('zero') + expect(1.in_words).to eq('one') + expect(2.in_words).to eq('two') + expect(3.in_words).to eq('three') + expect(4.in_words).to eq('four') + expect(5.in_words).to eq('five') + expect(6.in_words).to eq('six') + expect(7.in_words).to eq('seven') + expect(8.in_words).to eq('eight') + expect(9.in_words).to eq('nine') end it "reads 10 to 12" do - 10.in_words.should == 'ten' - 11.in_words.should == 'eleven' - 12.in_words.should == 'twelve' + expect(10.in_words).to eq('ten') + expect(11.in_words).to eq('eleven') + expect(12.in_words).to eq ('twelve') end it "reads teens" do - 13.in_words.should == 'thirteen' - 14.in_words.should == 'fourteen' - 15.in_words.should == 'fifteen' - 16.in_words.should == 'sixteen' - 17.in_words.should == 'seventeen' - 18.in_words.should == 'eighteen' - 19.in_words.should == 'nineteen' + expect(13.in_words).to eq('thirteen') + expect(14.in_words).to eq('fourteen') + expect(15.in_words).to eq('fifteen') + expect(16.in_words).to eq('sixteen') + expect(17.in_words).to eq('seventeen') + expect(18.in_words).to eq('eighteen') + expect(19.in_words).to eq('nineteen') end it "reads tens" do - 20.in_words.should == 'twenty' - 30.in_words.should == 'thirty' - 40.in_words.should == 'forty' - 50.in_words.should == 'fifty' - 60.in_words.should == 'sixty' - 70.in_words.should == 'seventy' - 80.in_words.should == 'eighty' - 90.in_words.should == 'ninety' + expect(20.in_words).to eq('twenty') + expect(30.in_words).to eq('thirty') + expect(40.in_words).to eq('forty') + expect(50.in_words).to eq('fifty') + expect(60.in_words).to eq('sixty') + expect(70.in_words).to eq('seventy') + expect(80.in_words).to eq('eighty') + expect(90.in_words).to eq('ninety') end it "reads various other numbers less than 100" do - 20.in_words.should == 'twenty' - 77.in_words.should == 'seventy seven' - 99.in_words.should == 'ninety nine' + expect(20.in_words).to eq('twenty') + expect(77.in_words).to eq('seventy seven') + expect(99.in_words).to eq('ninety nine') end it "reads hundreds" do - 100.in_words.should == 'one hundred' - 200.in_words.should == 'two hundred' - 300.in_words.should == 'three hundred' - 123.in_words.should == 'one hundred twenty three' - 777.in_words.should == 'seven hundred seventy seven' - 818.in_words.should == 'eight hundred eighteen' - 512.in_words.should == 'five hundred twelve' - 999.in_words.should == 'nine hundred ninety nine' + expect(100.in_words).to eq('one hundred') + expect(200.in_words).to eq('two hundred') + expect(300.in_words).to eq('three hundred') + expect(123.in_words).to eq('one hundred twenty three') + expect(777.in_words).to eq('seven hundred seventy seven') + expect(818.in_words).to eq('eight hundred eighteen') + expect(512.in_words).to eq('five hundred twelve') + expect(999.in_words).to eq('nine hundred ninety nine') end it "reads thousands" do - 1000.in_words.should == 'one thousand' - 32767.in_words.should == 'thirty two thousand seven hundred sixty seven' - 32768.in_words.should == 'thirty two thousand seven hundred sixty eight' + expect(1000.in_words).to eq('one thousand') + expect(32767.in_words).to eq('thirty two thousand seven hundred sixty seven') + expect(32768.in_words).to eq('thirty two thousand seven hundred sixty eight') end it "reads millions" do - 10_000_001.in_words.should == 'ten million one' + expect(10_000_001.in_words).to eq('ten million one') end it "reads billions" do - 1_234_567_890.in_words.should == 'one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety' + expect(1_234_567_890.in_words).to eq('one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety') end it "reads trillions" do - 1_000_000_000_000.in_words.should == 'one trillion' - 1_000_000_000_001.in_words.should == 'one trillion one' - 1_888_259_040_036.in_words.should == 'one trillion eight hundred eighty eight billion two hundred fifty nine million forty thousand thirty six' + expect(1_000_000_000_000.in_words).to eq('one trillion') + expect(1_000_000_000_001.in_words).to eq('one trillion one') + expect(1_888_259_040_036.in_words).to eq('one trillion eight hundred eighty eight billion two hundred fifty nine million forty thousand thirty six') end end From c72208e0bdf8aa555621f210a3909a821ffc967d Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sat, 14 Nov 2015 16:33:52 -0500 Subject: [PATCH 28/33] removed my solutions to learn_ruby excercises --- 00_hello/hello.rb | 7 -- 01_temperature/temperature.rb | 7 -- 02_calculator/calculator.rb | 36 -------- 03_simon_says/simon_says.rb | 31 ------- 04_pig_latin/pig_latin.rb | 52 ------------ 05_silly_blocks/silly_blocks.rb | 11 --- 06_performance_monitor/performance_monitor.rb | 12 --- 07_hello_friend/friend.rb | 9 -- 08_book_titles/book.rb | 16 ---- 09_timer/timer.rb | 14 --- 10_temperature_object/temperature.rb | 47 ---------- 11_dictionary/dictionary.rb | 44 ---------- 12_rpn_calculator/rpn_calculator.rb | 71 ---------------- 13_xml_document/xml_document.rb | 85 ------------------- 14_array_extensions/array_extensions.rb | 17 ---- 15_in_words/in_words.rb | 85 ------------------- 16 files changed, 544 deletions(-) delete mode 100644 00_hello/hello.rb delete mode 100644 01_temperature/temperature.rb delete mode 100644 02_calculator/calculator.rb delete mode 100644 03_simon_says/simon_says.rb delete mode 100644 04_pig_latin/pig_latin.rb delete mode 100644 05_silly_blocks/silly_blocks.rb delete mode 100644 06_performance_monitor/performance_monitor.rb delete mode 100644 07_hello_friend/friend.rb delete mode 100644 08_book_titles/book.rb delete mode 100644 09_timer/timer.rb delete mode 100644 10_temperature_object/temperature.rb delete mode 100644 11_dictionary/dictionary.rb delete mode 100644 12_rpn_calculator/rpn_calculator.rb delete mode 100644 13_xml_document/xml_document.rb delete mode 100644 14_array_extensions/array_extensions.rb delete mode 100644 15_in_words/in_words.rb diff --git a/00_hello/hello.rb b/00_hello/hello.rb deleted file mode 100644 index 3623e57f4..000000000 --- a/00_hello/hello.rb +++ /dev/null @@ -1,7 +0,0 @@ -def hello - "Hello!" -end - -def greet(who) - "Hello, #{who}!" -end \ No newline at end of file diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb deleted file mode 100644 index 02be3953d..000000000 --- a/01_temperature/temperature.rb +++ /dev/null @@ -1,7 +0,0 @@ -def ftoc(f) - ((f - 32.0) * 5.0 / 9.0) -end - -def ctof(c) - ((9.0 / 5.0) * c + 32.0) -end \ No newline at end of file diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb deleted file mode 100644 index 06338660b..000000000 --- a/02_calculator/calculator.rb +++ /dev/null @@ -1,36 +0,0 @@ -def add(num1, num2) - num1 + num2 -end - -def subtract(num1, num2) - num1 - num2 -end - -def sum(numlist) - result = 0 - numlist.each do |x| - result += x - end - result -end - -def multiply(numlist) - result = 1 - numlist.each do |x| - result *= x - end - result -end - -def power(numlist) - numlist[0] ** numlist[1] -end - -def factorial(num) - return 1 if num == 0 - result = 1 - (1..num).each do |x| - result *= x - end - result -end \ No newline at end of file diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb deleted file mode 100644 index a60b0ae52..000000000 --- a/03_simon_says/simon_says.rb +++ /dev/null @@ -1,31 +0,0 @@ -def echo(word) - word -end - -def shout(word) - word.upcase -end - -def repeat(word, num = 2) - ((word + " ") * num).strip -end - -def start_of_word(word, length = 1) - word[0..(length-1)] -end - -def first_word(words) - words.split(" ")[0] -end - -def titleize(words) - result = words.split(" ").each_with_index do |word, index| - little_words = ["and", "a", "or", "the", "over"] - if little_words.include?(word) && index != 0 - next - else - word[0] = word[0].upcase - end - end - result.join(" ") -end \ No newline at end of file diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb deleted file mode 100644 index 2a54abe39..000000000 --- a/04_pig_latin/pig_latin.rb +++ /dev/null @@ -1,52 +0,0 @@ -def translate(input) - vowel = "aeiouAEIOU" - output = "" - punct_check = /[[:punct:]]/.match(input) - words = [] - - if punct_check - letters = input.split("") - wbuild = "" - letters.each do |c| - if /\w/.match(c) - wbuild += c - else - words.push(wbuild) if wbuild != "" - words.push(c) if c != "" - wbuild = "" - end - end - else - words = input.split(" ") - end - - words.each do |term| - if /\w/.match(term) == nil - output += term - next - end - - cap = false - if /[[:upper:]]/.match(term[0]) - cap = true - end - term = term.downcase - term_letters = term.split("") - - while !vowel.include?(term_letters[0]) - if term_letters[0] + term_letters[1] == "qu" - term_letters.push(term_letters.shift) - end - term_letters.push(term_letters.shift) - end - - term_letters[0].upcase! if cap == true - if punct_check - output += term_letters.join("") + "ay" - else - output += term_letters.join("") + "ay" + " " - end - end - - return output.strip -end \ No newline at end of file diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb deleted file mode 100644 index e7c98c6e5..000000000 --- a/05_silly_blocks/silly_blocks.rb +++ /dev/null @@ -1,11 +0,0 @@ -def reverser - yield.split(" ").map { |word| word.reverse }.join(" ") -end - -def adder(num = 1) - yield + num -end - -def repeater(num = 1) - num.times { yield } -end \ No newline at end of file diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb deleted file mode 100644 index f5c65aea2..000000000 --- a/06_performance_monitor/performance_monitor.rb +++ /dev/null @@ -1,12 +0,0 @@ -def measure(n = 1) - dt = 0 - n.times do - t1 = Time.now - #puts t1 - yield - t2 = Time.now - #puts t2 - dt += t2 - t1 - end - dt /= n -end \ No newline at end of file diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb deleted file mode 100644 index a75d2b40e..000000000 --- a/07_hello_friend/friend.rb +++ /dev/null @@ -1,9 +0,0 @@ -class Friend - def greeting(who = nil) - if who == nil - "Hello!" - else - "Hello, " + who + "!" - end - end -end \ No newline at end of file diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb deleted file mode 100644 index 1f2806a6e..000000000 --- a/08_book_titles/book.rb +++ /dev/null @@ -1,16 +0,0 @@ -class Book - def initialize(title = nil) - @title = title - end - - def title - @title - end - - def title=(title) - words = title.split(" ") - words = words.each { |word| word.capitalize! if !["and","the", "a", "an", "in", "of"].include?(word) } - words[0].capitalize! - @title = words.join(" ") - end -end \ No newline at end of file diff --git a/09_timer/timer.rb b/09_timer/timer.rb deleted file mode 100644 index c4be12272..000000000 --- a/09_timer/timer.rb +++ /dev/null @@ -1,14 +0,0 @@ -class Timer - attr_accessor :seconds - - def initialize - @seconds = 0 - end - - def time_string - hr = (@seconds / 3600).to_s - min = ((@seconds / 60) % 60).to_s - sec = (@seconds % 60).to_s - output = "#{hr.rjust(2, "0")}:#{min.rjust(2, "0")}:#{sec.rjust(2, "0")}" - end -end \ No newline at end of file diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb deleted file mode 100644 index c44cddcb8..000000000 --- a/10_temperature_object/temperature.rb +++ /dev/null @@ -1,47 +0,0 @@ -class Temperature - def initialize(opt = {}) - @temp = opt - if @temp.has_key?(:f) - @temp[:c] = Temperature.ftoc(@temp[:f]) - elsif @temp.has_key?(:c) - @temp[:f] = Temperature.ctof(@temp[:c]) - end - end - - def in_fahrenheit - @temp[:f] - end - - def in_celsius - @temp[:c] - end - - def self.from_celsius(c) - Temperature.new(c: c) - end - - def self.from_fahrenheit(f) - Temperature.new(f: f) - end - - def self.ftoc(f) - ((f - 32.0) * 5.0 / 9.0) - end - - def self.ctof(c) - ((9.0 / 5.0) * c + 32.0) - end - -end - -class Celsius < Temperature - def initialize(num) - super(c: num) - end -end - -class Fahrenheit < Temperature - def initialize(num) - super(f: num) - end -end diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb deleted file mode 100644 index 5740bab53..000000000 --- a/11_dictionary/dictionary.rb +++ /dev/null @@ -1,44 +0,0 @@ -class Dictionary - attr_accessor :entries, :keywords - - def initialize - @entries = {} - @keywords = [] - @definition = [] - end - - def add(input) - if input.is_a?(String) - @keywords.push(input) - @entries[input] = nil - elsif input.is_a?(Hash) - input.each do |key, value| - @keywords.push(key) - @definition.push(value) - @entries[key] = value - end - end - end - - def include?(key) - @keywords.include?(key) - end - - def find(term) - found = {} - found = @entries.select{ |key, value| key.include?(term) } - end - - def keywords - @keywords.sort - end - - def printable - @entries = @entries.sort - result = "" - @entries.each do |key, value| - result += "[#{key}] \"#{value}\"\n" - end - result.chomp - end -end diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb deleted file mode 100644 index af8b48cb5..000000000 --- a/12_rpn_calculator/rpn_calculator.rb +++ /dev/null @@ -1,71 +0,0 @@ -class RPNCalculator - def initialize - @stack = [] - end - - def push(num) - @stack.push(num) - end - - def plus - raise "calculator is empty" if @stack.length < 2 - @stack.push(@stack.pop + @stack.pop) - end - - def minus - raise "calculator is empty" if @stack.length < 2 - num1 = @stack.pop - num2 = @stack.pop - @stack.push(num2 - num1) - end - - def divide - raise "calculator is empty" if @stack.length < 2 - num1 = @stack.pop.to_f - num2 = @stack.pop.to_f - @stack.push(num2 / num1) - end - - def times - raise "calculator is empty" if @stack.length < 2 - num1 = @stack.pop.to_f - num2 = @stack.pop.to_f - @stack.push(num2 * num1) - end - - def value - @stack.last - end - - def tokens(string) - result = string.split(" ").map do |x| - if x.match(/[+-\/*]/) - x.to_sym - else - x.to_i - end - end - result - end - - def evaluate(string) - tokens(string).each do |token| - if token.is_a?(Integer) - push(token) - elsif token.is_a?(Symbol) - case token - when :+ - plus - when :- - minus - when :/ - divide - when :* - times - end - end - end - - value - end -end diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb deleted file mode 100644 index cb81e7778..000000000 --- a/13_xml_document/xml_document.rb +++ /dev/null @@ -1,85 +0,0 @@ -class XmlDocument - def initialize(indent = false) - @indent = indent - @indent_count = 0 - end - - def method_missing(method, hash = {}, &block) - result = "" - - if hash.empty? && block.nil? - @indent ? (" " * @indent_count) + "<#{method}/>\n" : "<#{method}/>" - elsif !hash.empty? - key = hash.keys.first.to_s - val = hash[hash.keys.first] - @indent ? "<#{method} #{key}='#{val}'/>\n" : "<#{method} #{key}='#{val}'/>" - else - if @indent - result << "<#{method}>\n" - @indent_count += 1 - result << (" " * @indent_count) + "#{yield}" - @indent_count -= 1 - result << (" " * @indent_count) + "\n" - return result - else - "<#{method}>#{yield}" - end - end - end -=begin - Found a really cool way to do this using method_missing in ruby! - Source: http://stackoverflow.com/questions/29567635/what-distinguishes-new-from-newtrue-indent-xml-document-in-test-first-rub - - - Explanation of &block in ruby - Source: http://mixandgo.com/blog/mastering-ruby-blocks-in-less-than-5-minutes - - The code below were my previous efforts in doing this exercise without the knowledge of method_missing. Preserved for posterity. -=end - -=begin - def hello(attr = {}) - key = attr.keys.first.to_s - value = attr[attr.keys.first] - if block_given? - return @indent ? "\n" + yield + "\n" : "" + yield + "" - elsif attr.size == 0 - "" - else - "" - end - end - - def send(name) - "<#{name}/>" - end - - def goodbye - if block_given? - "" + yield + "" - else - "" - end - end - - def come_back - if block_given? - "" + yield + "" - else - "" - end - end - - def ok_fine(attr = {}) - key = attr.keys.first.to_s - value = attr[attr.keys.first] - if block_given? - "" + yield + "" - elsif attr.size == 0 - "" - else - "" - end - end -=end -end diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb deleted file mode 100644 index b33f52518..000000000 --- a/14_array_extensions/array_extensions.rb +++ /dev/null @@ -1,17 +0,0 @@ -class Array - - def sum - return 0 if self.empty? - return self.inject { |sum, x| sum + x } - end - - def square - return self if self.empty? - return self.map { |x| x ** 2 } - end - - def square! - return self if self.empty? - return self.map! { |x| x ** 2 } - end -end diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb deleted file mode 100644 index 309448ff2..000000000 --- a/15_in_words/in_words.rb +++ /dev/null @@ -1,85 +0,0 @@ -class Fixnum - def in_words - wordnum = { - 0 => "zero", - 1 => "one", - 2 => "two", - 3 => "three", - 4 => "four", - 5 => "five", - 6 => "six", - 7 => "seven", - 8 => "eight", - 9 => "nine", - 10 => "ten", - 11 => "eleven", - 12 => "twelve", - 13 => "thirteen", - 14 => "fourteen", - 15 => "fifteen", - 16 => "sixteen", - 17 => "seventeen", - 18 => "eighteen", - 19 => "nineteen", - 20 => "twenty", - 30 => "thirty", - 40 => "forty", - 50 => "fifty", - 60 => "sixty", - 70 => "seventy", - 80 => "eighty", - 90 => "ninety", - 100 => "hundred", - 1000 => "thousand", - 1000000 => "million", - 1000000000 => "billion", - 1000000000000 => "trillion" - } - - case self - when 0..19 - wordnum[self] - when 20..99 - return wordnum[self] if wordnum.has_key?(self) - ones = self % 10 - tens = self % 100 - ones - "#{wordnum[tens]} #{wordnum[ones]}" - when 100..999 - tens = self % 100 - hundreds = wordnum[self/100] + " " + wordnum[100] - if self % 100 == 0 - return hundreds - else - "#{hundreds} #{(tens % 100).in_words}".strip - end - when 1000..999999 - thousands = "#{(self / 1000).in_words} #{wordnum[1000]}" - if self % 1000 == 0 - return thousands - else - return "#{thousands} #{(self % 1000).in_words}".strip - end - when 1000000..999999999 - millions = "#{(self / 1000000).in_words} #{wordnum[1000000]}" - if self % 1000000 == 0 - return millions - else - return "#{millions} #{(self % 1000000).in_words}".strip - end - when (10 ** 9)...(10 ** 12) - billions = "#{(self / (10 ** 9)).in_words} #{wordnum[10 ** 9]}" - if self % (10 ** 9) == 0 - return billions - else - return "#{billions} #{(self % (10 ** 9)).in_words}".strip - end - when (10 ** 12)...(10 ** 15) - trillions = "#{(self / (10 ** 12)).in_words} #{wordnum[10 ** 12]}" - if self % (10 ** 12) == 0 - return trillions - else - return "#{trillions} #{(self % (10 ** 12)).in_words}".strip - end - end - end -end From 2d6624421e6dc7b511be47570db387bfaaa740be Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Sat, 14 Nov 2015 17:02:14 -0500 Subject: [PATCH 29/33] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e1388608..031046b10 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Test First Ruby +Test First Ruby -- RSpec 3 Edition ========== See [testfirst.org/learn_ruby](http://testfirst.org/learn_ruby#install) for more information about how this will work. From 858defa1f1c4fbd586ada81af066fd42ac7626d6 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 15 Nov 2015 12:13:56 -0500 Subject: [PATCH 30/33] replaced Time.stub syntax to allow(Time) --- 06_performance_monitor/performance_monitor_spec.rb | 8 ++++---- 09_timer/timer_spec.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/06_performance_monitor/performance_monitor_spec.rb b/06_performance_monitor/performance_monitor_spec.rb index 83ca25db1..fa19f3e4b 100644 --- a/06_performance_monitor/performance_monitor_spec.rb +++ b/06_performance_monitor/performance_monitor_spec.rb @@ -24,7 +24,7 @@ end it "takes exactly 0 seconds to run an empty block (with stubs)" do - Time.stub(:now) { @eleven_am } + allow(Time).to receive(:now) { @eleven_am } elapsed_time = measure do end expect(elapsed_time).to eq(0) @@ -39,7 +39,7 @@ it "takes exactly 1 second to run a block that sleeps for 1 second (with stubs)" do fake_time = @eleven_am - Time.stub(:now) { fake_time } + allow(Time).to receive(:now) { fake_time } elapsed_time = measure do fake_time += 60 # adds one minute to fake_time end @@ -57,7 +57,7 @@ it "returns the average time, not the total time, when running multiple times" do run_times = [8,6,5,7] fake_time = @eleven_am - Time.stub(:now) { fake_time } + allow(Time).to receive(:now) { fake_time } average_time = measure(4) do fake_time += run_times.pop end @@ -66,7 +66,7 @@ it "returns the average time when running a random number of times for random lengths of time" do fake_time = @eleven_am - Time.stub(:now) { fake_time } + allow(Time).to receive(:now) { fake_time } number_of_times = rand(10) + 2 average_time = measure(number_of_times) do delay = rand(10) diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index 89cc59bf6..c91421acf 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -15,7 +15,7 @@ end it "should initialize to 0 seconds" do - @timer.seconds.should == 0 + expect(@timer.seconds).to eq(0) end describe 'time_string' do From 3abb5d4d31aa2e3e2502a93d7f705e0274522845 Mon Sep 17 00:00:00 2001 From: Andrew Baik Date: Sun, 15 Nov 2015 19:56:02 -0500 Subject: [PATCH 31/33] updated html RSpec syntax with minor fixes --- .gitignore | 2 +- 00_hello/hello_spec.rb | 12 +-- 00_hello/index.html | 18 ++-- 01_temperature/index.html | 16 ++-- 02_calculator/calculator_spec.rb | 38 ++------ 02_calculator/index.html | 16 ++-- 03_simon_says/index.html | 34 +++---- 04_pig_latin/index.html | 18 ++-- 04_pig_latin/pig_latin_spec.rb | 9 -- 05_silly_blocks/index.html | 18 ++-- 06_performance_monitor/index.html | 22 ++--- 07_hello_friend/hello_friend_spec.rb | 4 +- 07_hello_friend/index.html | 8 +- 08_book_titles/index.html | 18 ++-- 09_timer/index.html | 16 ++-- 09_timer/timer_spec.rb | 6 +- 10_temperature_object/index.html | 38 ++++---- .../temperature_object_spec.rb | 6 -- 11_dictionary/index.html | 32 +++---- 12_rpn_calculator/index.html | 35 +++---- 13_xml_document/index.html | 24 ++--- 14_array_extensions/index.html | 12 +-- 15_in_words/index.html | 94 +++++++++---------- 23 files changed, 229 insertions(+), 267 deletions(-) diff --git a/.gitignore b/.gitignore index c6ef2182b..cbd3b4731 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .idea - ++.DS_Store* diff --git a/00_hello/hello_spec.rb b/00_hello/hello_spec.rb index ab64fc9c4..26075944a 100644 --- a/00_hello/hello_spec.rb +++ b/00_hello/hello_spec.rb @@ -16,7 +16,7 @@ # # You should see an error. **Don't get scared!** Try to read it and figure out what the computer wants to tell you. Somewhere on the first line it should say something like # -# no such file to load -- test-first-teaching/hello/hello (LoadError) +# cannot load such file -- test-first-teaching/hello/hello (LoadError) # # That means that it is looking for a file called `hello.rb` and can't find it. # @@ -36,7 +36,7 @@ # Failures: # # 1) the hello function says hello -# Failure/Error: hello.should == "Hello!" +# Failure/Error: expect(hello).to eq("Hello!") # NameError: # undefined local variable or method `hello' for # # # ./hello/hello_spec.rb:5:in `block (2 levels) in ' @@ -60,9 +60,9 @@ # Failures: # # 1) the hello function says hello -# Failure/Error: hello().should == "Hello!" +# Failure/Error: expect(hello).to eq("Hello!") # expected: "Hello!" -# got: nil (using ==) +# got: nil (compared using ==) # # ./hello/hello_spec.rb:5:in `block (2 levels) in ' # # This means that while it found the file, and it found the function, it's not returning anything! ("nil" is the Ruby way of saying "not anything".) @@ -82,9 +82,9 @@ # Now you should see an error like this: # # 1) the hello function says hello -# Failure/Error: hello().should == "Hello!" +# Failure/Error: expect(hello.to eq("Hello!") # expected: "Hello!" -# got: "whuh?" (using ==) +# got: "whuh?" (compared using ==) # # ./hello/hello_spec.rb:5:in `block (2 levels) in ' # # Correct this by changing "whuh?" to "Hello!". Save it. Run the test again. diff --git a/00_hello/index.html b/00_hello/index.html index bd5cc9084..e4eb3d6b8 100644 --- a/00_hello/index.html +++ b/00_hello/index.html @@ -51,7 +51,7 @@

Watch it fail

You should see an error. Don't get scared! Try to read it and figure out what the computer wants to tell you. Somewhere on the first line it should say something like

-
no such file to load -- test-first-teaching/hello/hello (LoadError)
+
cannot load such file -- test-first-teaching/hello/hello (LoadError)
 

That means that it is looking for a file called hello.rb and can't find it.

@@ -73,7 +73,7 @@

Watch it fail

Failures: 1) the hello function says hello - Failure/Error: hello.should == "Hello!" + Failure/Error: expect(hello).to eq("Hello!") NameError: undefined local variable or method `hello' for #<RSpec::Core::ExampleGroup::Nested_1:0x000001009b8808> # ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>' @@ -99,9 +99,9 @@

Watch it fail

Failures: 1) the hello function says hello - Failure/Error: hello().should == "Hello!" + Failure/Error: expect(hello).to eq("Hello!") expected: "Hello!" - got: nil (using ==) + got: nil (compared using ==) # ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>'
@@ -123,9 +123,9 @@

Watch it fail

Now you should see an error like this:

1) the hello function says hello
-   Failure/Error: hello().should == "Hello!"
+   Failure/Error: expect(hello).to eq("Hello!")
      expected: "Hello!"
-          got: "whuh?" (using ==)
+          got: "whuh?" (compared using ==)
    # ./hello/hello_spec.rb:5:in `block (2 levels) in <top (required)>'
 
@@ -164,17 +164,17 @@

Tests

describe "the hello function" do it "says hello" do - hello.should == "Hello!" + expect(hello).to eq("Hello!") end end describe "the greet function" do it "says hello to someone" do - greet("Alice").should == "Hello, Alice!" + expect(greet("Alice")).to eq("Hello, Alice!") end it "says hello to someone else" do - greet("Bob").should == "Hello, Bob!" + expect(greet("Bob")).to eq("Hello, Bob!") end end diff --git a/01_temperature/index.html b/01_temperature/index.html index 3b6014315..fc87b405d 100644 --- a/01_temperature/index.html +++ b/01_temperature/index.html @@ -62,19 +62,19 @@

Tests

describe "#ftoc" do it "converts freezing temperature" do - ftoc(32).should == 0 + expect(ftoc(32)).to eq(0) end it "converts boiling temperature" do - ftoc(212).should == 100 + expect(ftoc(212)).to eq(100) end it "converts body temperature" do - ftoc(98.6).should == 37 + expect(ftoc(98.6)).to eq(37) end it "converts arbitrary temperature" do - ftoc(68).should == 20 + expect(ftoc(68)).to eq(20) end end @@ -82,19 +82,19 @@

Tests

describe "#ctof" do it "converts freezing temperature" do - ctof(0).should == 32 + expect(ctof(0)).to eq(32) end it "converts boiling temperature" do - ctof(100).should == 212 + expect(ctof(100)).to eq(212) end it "converts arbitrary temperature" do - ctof(20).should == 68 + expect(ctof(20)).to eq(68) end it "converts body temperature" do - ctof(37).should be_within(0.1).of(98.6) + expect(ctof(37)).to be_within(0.1).of(98.6) # Why do we need to use be_within? # See http://www.ruby-forum.com/topic/169330 # and http://en.wikipedia.org/wiki/IEEE_754-2008 diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index bcb7d0040..fef7e9d00 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -78,40 +78,22 @@ # write tests and code for the following: describe "#multiply" do - it "multiplies two numbers" do - expect(multiply([1,2])).to eq(2) - end - it "multiplies several numbers" do - expect(multiply([1,2,3,4])).to eq(24) - end + it "multiplies two numbers" + + it "multiplies several numbers" + end describe "#power" do - it "raises one number to the power of another number" do - expect(power([2,4])).to eq(16) - end + it "raises one number to the power of another number" end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" do - expect(factorial(0)).to eq(1) - end - - it "computes the factorial of 1" do - expect(factorial(1)).to eq(1) - end - - it "computes the factorial of 2" do - expect(factorial(2)).to eq(2) - end - - it "computes the factorial of 5" do - expect(factorial(5)).to eq(120) - end - - it "computes the factorial of 10" do - expect(factorial(10)).to eq(3628800) - end + it "computes the factorial of 0" + it "computes the factorial of 1" + it "computes the factorial of 2" + it "computes the factorial of 5" + it "computes the factorial of 10" end diff --git a/02_calculator/index.html b/02_calculator/index.html index 037732e40..b8c612f4e 100644 --- a/02_calculator/index.html +++ b/02_calculator/index.html @@ -78,39 +78,39 @@

Tests

describe "add" do it "adds 0 and 0" do - add(0,0).should == 0 + expect(add(0,0)).to eq(0) end it "adds 2 and 2" do - add(2,2).should == 4 + expect(add(2,2)).to eq(4) end it "adds positive numbers" do - add(2,6).should == 8 + expect(add(2,6)).to eq(8) end end describe "subtract" do it "subtracts numbers" do - subtract(10,4).should == 6 + expect(subtract(10,4)).to eq(6) end end describe "sum" do it "computes the sum of an empty array" do - sum([]).should == 0 + expect(sum([])).to eq(0) end it "computes the sum of an array of one number" do - sum([7]).should == 7 + expect(sum([7])).to eq(7) end it "computes the sum of an array of two numbers" do - sum([7,11]).should == 18 + expect(sum([7,11])).to eq(18) end it "computes the sum of an array of many numbers" do - sum([1,3,5,7,9]).should == 25 + expect(sum([1,3,5,7,9])).to eq(25) end end diff --git a/03_simon_says/index.html b/03_simon_says/index.html index bfb8d6f29..74de726a0 100644 --- a/03_simon_says/index.html +++ b/03_simon_says/index.html @@ -55,27 +55,27 @@

Tests

describe "Simon says" do describe "echo" do it "should echo hello" do - echo("hello").should == "hello" + expect(echo("hello")).to eq("hello") end it "should echo bye" do - echo("bye").should == "bye" + expect(echo("bye")).to eq("bye") end end describe "shout" do it "should shout hello" do - shout("hello").should == "HELLO" + expect(shout("hello")).to eq("HELLO") end it "should shout multiple words" do - shout("hello world").should == "HELLO WORLD" + expect(shout("hello world")).to eq("HELLO WORLD") end end describe "repeat" do it "should repeat" do - repeat("hello").should == "hello hello" + expect(repeat("hello")).to eq("hello hello") end # Wait a second! How can you make the "repeat" method @@ -83,52 +83,52 @@

Tests

# # Hint: *default values* it "should repeat a number of times" do - repeat("hello", 3).should == "hello hello hello" + expect(repeat("hello", 3)).to eq("hello hello hello") end end describe "start_of_word" do it "returns the first letter" do - start_of_word("hello", 1).should == "h" + expect(start_of_word("hello", 1)).to eq("h") end it "returns the first two letters" do - start_of_word("Bob", 2).should == "Bo" + expect(start_of_word("Bob", 2)).to eq("Bo") end it "returns the first several letters" do s = "abcdefg" - start_of_word(s, 1).should == "a" - start_of_word(s, 2).should == "ab" - start_of_word(s, 3).should == "abc" + expect(start_of_word(s, 1)).to eq("a") + expect(start_of_word(s, 2)).to eq("ab") + expect(start_of_word(s, 3)).to eq("abc") end end describe "first_word" do it "tells us the first word of 'Hello World' is 'Hello'" do - first_word("Hello World").should == "Hello" + expect(first_word("Hello World")).to eq("Hello") end it "tells us the first word of 'oh dear' is 'oh'" do - first_word("oh dear").should == "oh" + expect(first_word("oh dear")).to eq("oh") end end describe "titleize" do it "capitalizes a word" do - titleize("jaws").should == "Jaws" + expect(titleize("jaws")).to eq("Jaws") end it "capitalizes every word (aka title case)" do - titleize("david copperfield").should == "David Copperfield" + expect(titleize("david copperfield")).to eq("David Copperfield") end it "doesn't capitalize 'little words' in a title" do - titleize("war and peace").should == "War and Peace" + expect(titleize("war and peace")).to eq("War and Peace") end it "does capitalize 'little words' at the start of a title" do - titleize("the bridge over the river kwai").should == "The Bridge over the River Kwai" + expect(titleize("the bridge over the river kwai")).to eq("The Bridge over the River Kwai") end end diff --git a/04_pig_latin/index.html b/04_pig_latin/index.html index da46cb79d..b40830700 100644 --- a/04_pig_latin/index.html +++ b/04_pig_latin/index.html @@ -61,46 +61,46 @@

Tests

it "translates a word beginning with a vowel" do s = translate("apple") - s.should == "appleay" + expect(s).to eq("appleay") end it "translates a word beginning with a consonant" do s = translate("banana") - s.should == "ananabay" + expect(s).to eq("ananabay") end it "translates a word beginning with two consonants" do s = translate("cherry") - s.should == "errychay" + expect(s).to eq("errychay") end it "translates two words" do s = translate("eat pie") - s.should == "eatay iepay" + expect(s).to eq("eatay iepay") end it "translates a word beginning with three consonants" do - translate("three").should == "eethray" + expect(translate("three")).to eq("eethray") end it "counts 'sch' as a single phoneme" do s = translate("school") - s.should == "oolschay" + expect(s).to eq("oolschay") end it "counts 'qu' as a single phoneme" do s = translate("quiet") - s.should == "ietquay" + expect(s).to eq("ietquay") end it "counts 'qu' as a consonant even when it's preceded by a consonant" do s = translate("square") - s.should == "aresquay" + expect(s).to eq("aresquay") end it "translates many words" do s = translate("the quick brown fox") - s.should == "ethay ickquay ownbray oxfay" + expect(s).to eq("ethay ickquay ownbray oxfay") end # Test-driving bonus: diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index 6fa540e37..cc659edfd 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -69,13 +69,4 @@ # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course) # * retain the punctuation from the original phrase - it "capitalized words are still capitalized" do - s = translate("hello World") - expect(s).to eq("ellohay Orldway") - end - - it "retain punctuation from original phrase" do - s = translate("Hello, World!") - expect(s).to eq("Ellohay, Orldway!") - end end diff --git a/05_silly_blocks/index.html b/05_silly_blocks/index.html index e13df82de..ada51229b 100644 --- a/05_silly_blocks/index.html +++ b/05_silly_blocks/index.html @@ -54,28 +54,28 @@

Tests

result = reverser do "hello" end - result.should == "olleh" + expect(result).to eq("olleh") end it "reverses each word in the string returned by the default block" do result = reverser do "hello dolly" end - result.should == "olleh yllod" + expect(result).to eq("olleh yllod") end end describe "adder" do it "adds one to the value returned by the default block" do - adder do + expect(adder do 5 - end.should == 6 + end).to eq(6) end it "adds 3 to the value returned by the default block" do - adder(3) do + expect(adder(3) do 5 - end.should == 8 + end).to eq(8) end end @@ -85,7 +85,7 @@

Tests

repeater do block_was_executed = true end - block_was_executed.should == true + expect(block_was_executed).to eq(true) end it "executes the default block 3 times" do @@ -93,7 +93,7 @@

Tests

repeater(3) do n += 1 end - n.should == 3 + expect(n).to eq(3) end it "executes the default block 10 times" do @@ -101,7 +101,7 @@

Tests

repeater(10) do n += 1 end - n.should == 10 + expect(n).to eq(10) end end diff --git a/06_performance_monitor/index.html b/06_performance_monitor/index.html index d50de4207..7e2dc0560 100644 --- a/06_performance_monitor/index.html +++ b/06_performance_monitor/index.html @@ -60,30 +60,30 @@

Tests

it "takes about 0 seconds to run an empty block" do elapsed_time = measure do end - elapsed_time.should be_within(0.1).of(0) + expect(elapsed_time).to be_within(0.1).of(0) end it "takes exactly 0 seconds to run an empty block (with stubs)" do - Time.stub(:now) { @eleven_am } + allow(Time).to receive(:now) { @eleven_am } elapsed_time = measure do end - elapsed_time.should == 0 + expect(elapsed_time).to eq(0) end it "takes about 1 second to run a block that sleeps for 1 second" do elapsed_time = measure do sleep 1 end - elapsed_time.should be_within(0.1).of(1) + expect(elapsed_time).to be_within(0.1).of(1) end it "takes exactly 1 second to run a block that sleeps for 1 second (with stubs)" do fake_time = @eleven_am - Time.stub(:now) { fake_time } + allow(Time).to receive(:now) { fake_time } elapsed_time = measure do fake_time += 60 # adds one minute to fake_time end - elapsed_time.should == 60 + expect(elapsed_time).to eq(60) end it "runs a block N times" do @@ -91,28 +91,28 @@

Tests

measure(4) do n += 1 end - n.should == 4 + expect(n).to eq(4) end it "returns the average time, not the total time, when running multiple times" do run_times = [8,6,5,7] fake_time = @eleven_am - Time.stub(:now) { fake_time } + allow(Time).to receive(:now) { fake_time } average_time = measure(4) do fake_time += run_times.pop end - average_time.should == 6.5 + expect(average_time).to eq(6.5) end it "returns the average time when running a random number of times for random lengths of time" do fake_time = @eleven_am - Time.stub(:now) { fake_time } + allow(Time).to receive(:now) { fake_time } number_of_times = rand(10) + 2 average_time = measure(number_of_times) do delay = rand(10) fake_time += delay end - average_time.should == (fake_time - @eleven_am).to_f/number_of_times + expect(average_time).to eq((fake_time - @eleven_am).to_f/number_of_times) end end diff --git a/07_hello_friend/hello_friend_spec.rb b/07_hello_friend/hello_friend_spec.rb index de5d4962f..f6c07d376 100644 --- a/07_hello_friend/hello_friend_spec.rb +++ b/07_hello_friend/hello_friend_spec.rb @@ -40,7 +40,7 @@ # # 'Friend says hello' FAILED # expected: "Hello!", -# got: nil (using ==) +# got: nil (compared using ==) # ./friend_spec.rb:5: # # This means that there is a method, but it's not returning anything! ("nil" is the Ruby way of saying "not anything".) @@ -61,7 +61,7 @@ # # 'Friend says hello' FAILED # expected: "Hello!", -# got: "whuh?" (using ==) +# got: "whuh?" (compared using ==) # ./friend_spec.rb:5: # # Correct this by changing "whuh?" to "Hello!". Save it. Run the test again. diff --git a/07_hello_friend/index.html b/07_hello_friend/index.html index 865ca858f..55f4e035c 100644 --- a/07_hello_friend/index.html +++ b/07_hello_friend/index.html @@ -77,7 +77,7 @@

Watch it fail some more

'Friend says hello' FAILED
 expected: "Hello!",
-     got: nil (using ==)
+     got: nil (compared using ==)
 ./friend_spec.rb:5:
 
@@ -100,7 +100,7 @@

Watch it fail yet again

'Friend says hello' FAILED
 expected: "Hello!",
-     got: "whuh?" (using ==)
+     got: "whuh?" (compared using ==)
 ./friend_spec.rb:5:
 
@@ -136,11 +136,11 @@

Tests

describe Friend do it "says hello" do - Friend.new.greeting.should == "Hello!" + expect(Friend.new.greeting).to eq("Hello!") end it "says hello to someone" do - Friend.new.greeting("Bob").should == "Hello, Bob!" + expect(Friend.new.greeting("Bob")).to eq("Hello, Bob!") end end diff --git a/08_book_titles/index.html b/08_book_titles/index.html index 50dfe0ddd..b5da4af8e 100644 --- a/08_book_titles/index.html +++ b/08_book_titles/index.html @@ -62,52 +62,52 @@

Tests

describe 'title' do it 'should capitalize the first letter' do @book.title = "inferno" - @book.title.should == "Inferno" + expect(@book.title).to eq("Inferno") end it 'should capitalize every word' do @book.title = "stuart little" - @book.title.should == "Stuart Little" + expect(@book.title).to eq("Stuart Little") end describe 'should capitalize every word except...' do describe 'articles' do specify 'the' do @book.title = "alexander the great" - @book.title.should == "Alexander the Great" + expect(@book.title).to eq("Alexander the Great") end specify 'a' do @book.title = "to kill a mockingbird" - @book.title.should == "To Kill a Mockingbird" + expect(@book.title).to eq("To Kill a Mockingbird") end specify 'an' do @book.title = "to eat an apple a day" - @book.title.should == "To Eat an Apple a Day" + expect(@book.title).to eq("To Eat an Apple a Day") end end specify 'conjunctions' do @book.title = "war and peace" - @book.title.should == "War and Peace" + expect(@book.title).to eq("War and Peace") end specify 'prepositions' do @book.title = "love in the time of cholera" - @book.title.should == "Love in the Time of Cholera" + expect(@book.title).to eq("Love in the Time of Cholera") end end describe 'should always capitalize...' do specify 'I' do @book.title = "what i wish i knew when i was 20" - @book.title.should == "What I Wish I Knew When I Was 20" + expect(@book.title).to eq("What I Wish I Knew When I Was 20") end specify 'the first word' do @book.title = "the man in the iron mask" - @book.title.should == "The Man in the Iron Mask" + expect(@book.title).to eq("The Man in the Iron Mask") end end end diff --git a/09_timer/index.html b/09_timer/index.html index f1ea5a720..fcf7a2c66 100644 --- a/09_timer/index.html +++ b/09_timer/index.html @@ -55,28 +55,28 @@

Tests

end it "should initialize to 0 seconds" do - @timer.seconds.should == 0 + expect(@timer.seconds).to eq(0) end describe 'time_string' do it "should display 0 seconds as 00:00:00" do @timer.seconds = 0 - @timer.time_string.should == "00:00:00" + expect(@timer.time_string).to eq("00:00:00") end it "should display 12 seconds as 00:00:12" do @timer.seconds = 12 - @timer.time_string.should == "00:00:12" + expect(@timer.time_string).to eq("00:00:12") end it "should display 66 seconds as 00:01:06" do @timer.seconds = 66 - @timer.time_string.should == "00:01:06" + expect(@timer.time_string).to eq("00:01:06") end it "should display 4000 seconds as 01:06:40" do @timer.seconds = 4000 - @timer.time_string.should == "01:06:40" + expect(@timer.time_string).to eq("01:06:40") end end @@ -87,13 +87,13 @@

Tests

# # describe 'padded' do # it 'pads zero' do - # @timer.padded(0).should == '00' + # expect(@timer.padded(0)).to eq('00') # end # it 'pads one' do - # @timer.padded(1).should == '01' + # expect(@timer.padded(1)).to eq('01') # end # it "doesn't pad a two-digit number" do - # @timer.padded(12).should == '12' + # expect(@timer.padded(12)).to eq('12') # end # end diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index c91421acf..40b33f23f 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -47,13 +47,13 @@ # # describe 'padded' do # it 'pads zero' do - # @timer.padded(0).should == '00' + # expect(@timer.padded(0)).to eq('00') # end # it 'pads one' do - # @timer.padded(1).should == '01' + # expect(@timer.padded(1)).to eq('01') # end # it "doesn't pad a two-digit number" do - # @timer.padded(12).should == '12' + # expect(@timer.padded(12)).to eq('12') # end # end diff --git a/10_temperature_object/index.html b/10_temperature_object/index.html index a86e7b851..6c88c6e7f 100644 --- a/10_temperature_object/index.html +++ b/10_temperature_object/index.html @@ -62,44 +62,44 @@

Tests

describe "can be constructed with an options hash" do describe "in degrees fahrenheit" do it "at 50 degrees" do - Temperature.new(:f => 50).in_fahrenheit.should == 50 + expect(Temperature.new(:f => 50).in_fahrenheit).to eq(50) end describe "and correctly convert to celsius" do it "at freezing" do - Temperature.new(:f => 32).in_celsius.should == 0 + expect(Temperature.new(:f => 32).in_celsius).to eq(0) end it "at boiling" do - Temperature.new(:f => 212).in_celsius.should == 100 + expect(Temperature.new(:f => 212).in_celsius).to eq(100) end it "at body temperature" do - Temperature.new(:f => 98.6).in_celsius.should == 37 + expect(Temperature.new(:f => 98.6).in_celsius).to eq(37) end it "at an arbitrary temperature" do - Temperature.new(:f => 68).in_celsius.should == 20 + expect(Temperature.new(:f => 68).in_celsius).to eq(20) end end end describe "in degrees celsius" do it "at 50 degrees" do - Temperature.new(:c => 50).in_celsius.should == 50 + expect(Temperature.new(:c => 50).in_celsius).to eq(50) end describe "and correctly convert to fahrenheit" do it "at freezing" do - Temperature.new(:c => 0).in_fahrenheit.should == 32 + expect(Temperature.new(:c => 0).in_fahrenheit).to eq(32) end it "at boiling" do - Temperature.new(:c => 100).in_fahrenheit.should == 212 + expect(Temperature.new(:c => 100).in_fahrenheit).to eq(212) end it "at body temperature" do - Temperature.new(:c => 37).in_fahrenheit.should be_within(0.1).of(98.6) + expect(Temperature.new(:c => 37).in_fahrenheit).to be_within(0.1).of(98.6) # Why do we need to use be_within here? # See http://www.ruby-forum.com/topic/169330 # and http://groups.google.com/group/rspec/browse_thread/thread/f3ebbe3c313202bb @@ -116,13 +116,13 @@

Tests

describe "can be constructed via factory methods" do it "in degrees celsius" do - Temperature.from_celsius(50).in_celsius.should == 50 - Temperature.from_celsius(50).in_fahrenheit.should == 122 + expect(Temperature.from_celsius(50).in_celsius).to eq(50) + expect(Temperature.from_celsius(50).in_fahrenheit).to eq(122) end it "in degrees fahrenheit" do - Temperature.from_fahrenheit(50).in_fahrenheit.should == 50 - Temperature.from_fahrenheit(50).in_celsius.should == 10 + expect(Temperature.from_fahrenheit(50).in_fahrenheit).to eq(50) + expect(Temperature.from_fahrenheit(50).in_celsius).to eq(10) end end @@ -142,23 +142,23 @@

Tests

describe "Temperature subclasses" do describe "Celsius subclass" do it "is constructed in degrees celsius" do - Celsius.new(50).in_celsius.should == 50 - Celsius.new(50).in_fahrenheit.should == 122 + expect(Celsius.new(50).in_celsius).to eq(50) + expect(Celsius.new(50).in_fahrenheit).to eq(122) end it "is a Temperature subclass" do - Celsius.new(0).should be_a(Temperature) + expect(Celsius.new(0)).to be_a(Temperature) end end describe "Fahrenheit subclass" do it "is constructed in degrees fahrenheit" do - Fahrenheit.new(50).in_fahrenheit.should == 50 - Fahrenheit.new(50).in_celsius.should == 10 + expect(Fahrenheit.new(50).in_fahrenheit).to eq(50) + expect(Fahrenheit.new(50).in_celsius).to eq(10) end it "is a Temperature subclass" do - Fahrenheit.new(0).should be_a(Temperature) + expect(Fahrenheit.new(0)).to be_a(Temperature) end end end diff --git a/10_temperature_object/temperature_object_spec.rb b/10_temperature_object/temperature_object_spec.rb index b95ed92e6..5c0af8685 100644 --- a/10_temperature_object/temperature_object_spec.rb +++ b/10_temperature_object/temperature_object_spec.rb @@ -97,13 +97,7 @@ # run *all* the tests during your refactoring, to make sure you did it right # describe "utility class methods" do - it "ftoc calls properly on class" do - expect(Temperature.ftoc(32)).to eq(0) - end - it "ctof calls properly on class" do - expect(Temperature.ctof(0)).to eq(32) - end end # Here's another way to solve the problem! diff --git a/11_dictionary/index.html b/11_dictionary/index.html index 778d45ee2..4b4d4463f 100644 --- a/11_dictionary/index.html +++ b/11_dictionary/index.html @@ -53,71 +53,71 @@

Tests

end it 'is empty when created' do - @d.entries.should == {} + expect(@d.entries).to eq({}) end it 'can add whole entries with keyword and definition' do @d.add('fish' => 'aquatic animal') - @d.entries.should == {'fish' => 'aquatic animal'} - @d.keywords.should == ['fish'] + expect(@d.entries).to eq({'fish' => 'aquatic animal'}) + expect(@d.keywords).to eq(['fish']) end it 'add keywords (without definition)' do @d.add('fish') - @d.entries.should == {'fish' => nil} - @d.keywords.should == ['fish'] + expect(@d.entries).to eq({'fish' => nil}) + expect(@d.keywords).to eq(['fish']) end it 'can check whether a given keyword exists' do - @d.include?('fish').should be_false + expect(@d.include?('fish').to be_false end it "doesn't cheat when checking whether a given keyword exists" do - @d.include?('fish').should be_false # if the method is empty, this test passes with nil returned + expect(@d.include?('fish')).to be_false # if the method is empty, this test passes with nil returned @d.add('fish') - @d.include?('fish').should be_true # confirms that it actually checks - @d.include?('bird').should be_false # confirms not always returning true after add + expect(@d.include?('fish')).to be_true # confirms that it actually checks + expect(@d.include?('bird')).to be_false # confirms not always returning true after add end it "doesn't include a prefix that wasn't added as a word in and of itself" do @d.add('fish') - @d.include?('fi').should be_false + expect(@d.include?('fi')).to be_false end it "doesn't find a word in empty dictionary" do - @d.find('fi').should be_empty # {} + expect(@d.find('fi')).to be_empty # {} end it 'finds nothing if the prefix matches nothing' do @d.add('fiend') @d.add('great') - @d.find('nothing').should be_empty + expect(@d.find('nothing')).to be_empty end it "finds an entry" do @d.add('fish' => 'aquatic animal') - @d.find('fish').should == {'fish' => 'aquatic animal'} + expect(@d.find('fish')).to eq({'fish' => 'aquatic animal'}) end it 'finds multiple matches from a prefix and returns the entire entry (keyword + definition)' do @d.add('fish' => 'aquatic animal') @d.add('fiend' => 'wicked person') @d.add('great' => 'remarkable') - @d.find('fi').should == {'fish' => 'aquatic animal', 'fiend' => 'wicked person'} + expect(@d.find('fi')).to eq({'fish' => 'aquatic animal', 'fiend' => 'wicked person'}) end it 'lists keywords alphabetically' do @d.add('zebra' => 'African land animal with stripes') @d.add('fish' => 'aquatic animal') @d.add('apple' => 'fruit') - @d.keywords.should == %w(apple fish zebra) + expect(@d.keywords).to eq(%w(apple fish zebra)) end it 'can produce printable output like so: [keyword] "definition"' do @d.add('zebra' => 'African land animal with stripes') @d.add('fish' => 'aquatic animal') @d.add('apple' => 'fruit') - @d.printable.should == %Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"} + expect(@d.printable).to eq(%Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"}) end end diff --git a/12_rpn_calculator/index.html b/12_rpn_calculator/index.html index b19f645d7..5237a7d4e 100644 --- a/12_rpn_calculator/index.html +++ b/12_rpn_calculator/index.html @@ -90,7 +90,7 @@

Tests

calculator.push(2) calculator.push(3) calculator.plus - calculator.value.should == 5 + expect(calculator.value).to eq(5) end it "adds three numbers" do @@ -98,16 +98,16 @@

Tests

calculator.push(3) calculator.push(4) calculator.plus - calculator.value.should == 7 + expect(calculator.value).to eq(7) calculator.plus - calculator.value.should == 9 + expect(calculator.value).to eq(9) end it "subtracts the second number from the first number" do calculator.push(2) calculator.push(3) calculator.minus - calculator.value.should == -1 + expect(calculator.value).to eq(-1) end it "adds and subtracts" do @@ -115,9 +115,9 @@

Tests

calculator.push(3) calculator.push(4) calculator.minus - calculator.value.should == -1 + expect(calculator.value).to eq(-1) calculator.plus - calculator.value.should == 1 + expect(calculator.value).to eq(1) end it "multiplies and divides" do @@ -125,9 +125,9 @@

Tests

calculator.push(3) calculator.push(4) calculator.divide - calculator.value.should == (3.0 / 4.0) + expect(calculator.value).to eq(3.0 / 4.0) calculator.times - calculator.value.should == 2.0 * (3.0 / 4.0) + expect(calculator.value).to eq(2.0 * (3.0 / 4.0)) end it "resolves operator precedence unambiguously" do @@ -137,7 +137,7 @@

Tests

calculator.plus calculator.push(3) calculator.times - calculator.value.should == (1+2)*3 + expect(calculator.value).to eq((1+2)*3) # 1 2 3 * + => 1 + (2 * 3) calculator.push(1) @@ -145,7 +145,7 @@

Tests

calculator.push(3) calculator.times calculator.plus - calculator.value.should == 1+(2*3) + expect(calculator.value).to eq(1+(2*3)) end it "fails informatively when there's not enough values stacked away" do @@ -168,23 +168,18 @@

Tests

# extra credit it "tokenizes a string" do - calculator.tokens("1 2 3 * + 4 5 - /").should == - [1, 2, 3, :*, :+, 4, 5, :-, :/] + expect(calculator.tokens("1 2 3 * + 4 5 - /")).to eq([1, 2, 3, :*, :+, 4, 5, :-, :/]) end # extra credit it "evaluates a string" do - calculator.evaluate("1 2 3 * +").should == - ((2 * 3) + 1) + expect(calculator.evaluate("1 2 3 * +")).to eq((2 * 3) + 1) - calculator.evaluate("4 5 -").should == - (4 - 5) + expect(calculator.evaluate("4 5 -")).to eq(4 - 5) - calculator.evaluate("2 3 /").should == - (2.0 / 3.0) + expect(calculator.evaluate("2 3 /")).to eq(2.0 / 3.0) - calculator.evaluate("1 2 3 * + 4 5 - /").should == - (1.0 + (2 * 3)) / (4 - 5) + expect(calculator.evaluate("1 2 3 * + 4 5 - /")).to eq((1.0 + (2 * 3)) / (4 - 5)) end end diff --git a/13_xml_document/index.html b/13_xml_document/index.html index e7fbeb7ad..900ff72d5 100644 --- a/13_xml_document/index.html +++ b/13_xml_document/index.html @@ -62,57 +62,57 @@

Tests

end it "renders an empty tag" do - @xml.hello.should == "<hello/>" + expect(@xml.hello).to eq("<hello/>") end it "renders a tag with attributes" do - @xml.hello(:name => 'dolly').should == "<hello name='dolly'/>" + expect(@xml.hello(:name => 'dolly')).to eq("<hello name='dolly'/>") end it "renders a randomly named tag" do tag_name = (1..8).map{|i| ('a'..'z').to_a[rand(26)]}.join - @xml.send(tag_name).should == "<#{tag_name}/>" + expect(@xml.send(tag_name)).to eq("<#{tag_name}/>") end it "renders block with text inside" do - @xml.hello do + expect(@xml.hello do "dolly" - end.should == "<hello>dolly</hello>" + end).to eq("<hello>dolly</hello>") end it "nests one level" do - @xml.hello do + expect(@xml.hello do @xml.goodbye - end.should == "<hello><goodbye/></hello>" + end).to eq("<hello><goodbye/></hello>") end it "nests several levels" do xml = XmlDocument.new - xml.hello do + expect(xml.hello do xml.goodbye do xml.come_back do xml.ok_fine(:be => "that_way") end end - end.should == "<hello><goodbye><come_back><ok_fine be='that_way'/></come_back></goodbye></hello>" + end).to eq("<hello><goodbye><come_back><ok_fine be='that_way'/></come_back></goodbye></hello>") end it "indents" do @xml = XmlDocument.new(true) - @xml.hello do + expect(@xml.hello do @xml.goodbye do @xml.come_back do @xml.ok_fine(:be => "that_way") end end - end.should == + end).to eq( "<hello>\n" + " <goodbye>\n" + " <come_back>\n" + " <ok_fine be='that_way'/>\n" + " </come_back>\n" + " </goodbye>\n" + - "</hello>\n" + "</hello>\n") end end diff --git a/14_array_extensions/index.html b/14_array_extensions/index.html index 28005d4ca..ebbe06502 100644 --- a/14_array_extensions/index.html +++ b/14_array_extensions/index.html @@ -51,25 +51,25 @@

Tests

describe "#sum" do it "has a #sum method" do - [].should respond_to(:sum) + expect([]).to respond_to(:sum) end it "should be 0 for an empty array" do - [].sum.should == 0 + expect([].sum).to eq(0) end it "should add all of the elements" do - [1,2,4].sum.should == 7 + expect([1,2,4].sum).to eq(7) end end describe '#square' do it "does nothing to an empty array" do - [].square.should == [] + expect([].square).to eq([]) end it "returns a new array containing the squares of each element" do - [1,2,3].square.should == [1,4,9] + expect([1,2,3].square).to eq([1,4,9]) end end @@ -77,7 +77,7 @@

Tests

it "squares each element of the original array" do array = [1,2,3] array.square! - array.should == [1,4,9] + expect(array).to eq([1,4,9]) end end diff --git a/15_in_words/index.html b/15_in_words/index.html index 6525174c1..e235d4796 100644 --- a/15_in_words/index.html +++ b/15_in_words/index.html @@ -63,80 +63,80 @@

Tests

describe Fixnum do it "reads 0 to 9" do - 0.in_words.should == 'zero' - 1.in_words.should == 'one' - 2.in_words.should == 'two' - 3.in_words.should == 'three' - 4.in_words.should == 'four' - 5.in_words.should == 'five' - 6.in_words.should == 'six' - 7.in_words.should == 'seven' - 8.in_words.should == 'eight' - 9.in_words.should == 'nine' + expect(0.in_words).to eq('zero') + expect(1.in_words).to eq('one') + expect(2.in_words).to eq('two') + expect(3.in_words).to eq('three') + expect(4.in_words).to eq('four') + expect(5.in_words).to eq('five') + expect(6.in_words).to eq('six') + expect(7.in_words).to eq('seven') + expect(8.in_words).to eq('eight') + expect(9.in_words).to eq('nine') end it "reads 10 to 12" do - 10.in_words.should == 'ten' - 11.in_words.should == 'eleven' - 12.in_words.should == 'twelve' + expect(10.in_words).to eq('ten') + expect(11.in_words).to eq('eleven') + expect(12.in_words).to eq('twelve') end it "reads teens" do - 13.in_words.should == 'thirteen' - 14.in_words.should == 'fourteen' - 15.in_words.should == 'fifteen' - 16.in_words.should == 'sixteen' - 17.in_words.should == 'seventeen' - 18.in_words.should == 'eighteen' - 19.in_words.should == 'nineteen' + expect(13.in_words).to eq('thirteen') + expect(14.in_words).to eq('fourteen') + expect(15.in_words).to eq('fifteen') + expect(16.in_words).to eq('sixteen') + expect(17.in_words).to eq('seventeen') + expect(18.in_words).to eq('eighteen') + expect(19.in_words).to eq('nineteen') end it "reads tens" do - 20.in_words.should == 'twenty' - 30.in_words.should == 'thirty' - 40.in_words.should == 'forty' - 50.in_words.should == 'fifty' - 60.in_words.should == 'sixty' - 70.in_words.should == 'seventy' - 80.in_words.should == 'eighty' - 90.in_words.should == 'ninety' + expect(20.in_words).to eq('twenty') + expect(30.in_words).to eq('thirty') + expect(40.in_words).to eq('forty') + expect(50.in_words).to eq('fifty') + expect(60.in_words).to eq('sixty') + expect(70.in_words).to eq('seventy') + expect(80.in_words).to eq('eighty') + expect(90.in_words).to eq('ninety') end it "reads various other numbers less than 100" do - 20.in_words.should == 'twenty' - 77.in_words.should == 'seventy seven' - 99.in_words.should == 'ninety nine' + expect(20.in_words).to eq('twenty') + expect(77.in_words).to eq('seventy seven') + expect(99.in_words).to eq('ninety nine') end it "reads hundreds" do - 100.in_words.should == 'one hundred' - 200.in_words.should == 'two hundred' - 300.in_words.should == 'three hundred' - 123.in_words.should == 'one hundred twenty three' - 777.in_words.should == 'seven hundred seventy seven' - 818.in_words.should == 'eight hundred eighteen' - 512.in_words.should == 'five hundred twelve' - 999.in_words.should == 'nine hundred ninety nine' + expect(100.in_words).to eq('one hundred') + expect(200.in_words).to eq('two hundred') + expect(300.in_words).to eq('three hundred') + expect(123.in_words).to eq('one hundred twenty three') + expect(777.in_words).to eq('seven hundred seventy seven') + expect(818.in_words).to eq('eight hundred eighteen') + expect(512.in_words).to eq('five hundred twelve') + expect(999.in_words).to eq('nine hundred ninety nine') end it "reads thousands" do - 1000.in_words.should == 'one thousand' - 32767.in_words.should == 'thirty two thousand seven hundred sixty seven' - 32768.in_words.should == 'thirty two thousand seven hundred sixty eight' + expect(1000.in_words).to eq('one thousand') + expect(32767.in_words).to eq('thirty two thousand seven hundred sixty seven') + expect(32768.in_words).to eq('thirty two thousand seven hundred sixty eight') end it "reads millions" do - 10_000_001.in_words.should == 'ten million one' + expect(10_000_001.in_words).to eq('ten million one') end it "reads billions" do - 1_234_567_890.in_words.should == 'one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety' + expect(1_234_567_890.in_words).to eq('one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety') end it "reads trillions" do - 1_000_000_000_000.in_words.should == 'one trillion' - 1_000_000_000_001.in_words.should == 'one trillion one' - 1_888_259_040_036.in_words.should == 'one trillion eight hundred eighty eight billion two hundred fifty nine million forty thousand thirty six' + expect(1_000_000_000_000.in_words).to eq('one trillion') + expect(1_000_000_000_001.in_words).to eq('one trillion one') + expect(1_888_259_040_036.in_words).to eq('one trillion eight hundred eighty eight billion two hundred fifty nine million forty thousand thirty six') end end From 30ac9fb27936948f78195f20499cf4bab1ebaa07 Mon Sep 17 00:00:00 2001 From: Erik Trautman Date: Mon, 16 Nov 2015 08:59:56 -0500 Subject: [PATCH 32/33] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 031046b10..706b785e9 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Test First Ruby -- RSpec 3 Edition ========== -See [testfirst.org/learn_ruby](http://testfirst.org/learn_ruby#install) for more information about how this will work. +See [testfirst.org/learn_ruby](http://testfirst.org/learn_ruby#install) for more information about how this will work. These test-first Ruby challenges have been forked from [https://github.com/alexch/learn_ruby](https://github.com/alexch/learn_ruby) and updated to use RSpec 3 instead of RSpec 2. ### On a High Level From 290917dc5e1742cb13dc2f7e6438e81d71aafc71 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Tue, 13 Sep 2016 19:35:20 -0400 Subject: [PATCH 33/33] Escape the file path passed to rspec_opts I added `Shellwords.escape` so that any whitespace in the file path doesn't cause the task to self-destruct. --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 89d716ec9..e024f9b81 100644 --- a/Rakefile +++ b/Rakefile @@ -8,6 +8,6 @@ desc "run tests for this lab" RSpec::Core::RakeTask.new do |task| lab = Rake.application.original_dir task.pattern = "#{lab}/*_spec.rb" - task.rspec_opts = [ "-I#{lab}", "-I#{lab}/solution", '-f documentation', '-r ./rspec_config'] + task.rspec_opts = [ "-I#{Shellwords.escape(lab)}", "-I#{Shellwords.escape(lab)}/solution", '-f documentation', '-r ./rspec_config'] task.verbose = false end