├── .ruby-version ├── Gemfile ├── Gemfile.lock ├── LICENSE.md ├── README.md ├── command-query ├── README.md └── exercises │ ├── adult_spec.rb │ ├── apple_spec.rb │ ├── appointments_spec.rb │ ├── baby_spec.rb │ ├── beers_spec.rb │ ├── catalog_spec.rb │ ├── children_spec.rb │ ├── clearance_spec.rb │ ├── clock_spec.rb │ ├── cupcakes_spec.rb │ ├── dog_spec.rb │ ├── door_spec.rb │ ├── drops_spec.rb │ ├── floor_spec.rb │ ├── kid_spec.rb │ ├── leather_chair_spec.rb │ ├── light_spec.rb │ ├── milk_bottle_spec.rb │ ├── money_spec.rb │ ├── music_spec.rb │ ├── person_spec.rb │ ├── pills_spec.rb │ ├── roll_call_spec.rb │ ├── santa_spec.rb │ ├── student_spec.rb │ ├── teeth_spec.rb │ ├── tire_spec.rb │ ├── wallet_spec.rb │ ├── water_spec.rb │ └── yak_spec.rb ├── data-types ├── collections │ ├── .rspec │ ├── README.md │ ├── advanced_nested_collections │ │ ├── .rspec │ │ └── spec │ │ │ ├── advanced_nesting_spec.rb │ │ │ ├── nesting.rb │ │ │ └── spec_helper.rb │ └── spec │ │ ├── arrays_spec.rb │ │ ├── hashes_spec.rb │ │ ├── nested_collections_spec.rb │ │ └── spec_helper.rb ├── ints_and_floats │ ├── .rspec │ ├── README.md │ └── spec │ │ ├── ints_and_floats_spec.rb │ │ └── spec_helper.rb └── strings │ ├── .rspec │ ├── README.md │ └── spec │ ├── spec_helper.rb │ └── strings_spec.rb ├── enumerables ├── CONTRIBUTING.md ├── LICENSE ├── README.md └── exercises_1 │ ├── .rspec │ ├── README.md │ ├── objects │ ├── gnome.rb │ ├── squid.rb │ ├── thing.rb │ └── unicorn.rb │ └── spec │ ├── all_pattern_spec.rb │ ├── all_spec.rb │ ├── any_pattern_spec.rb │ ├── any_spec.rb │ ├── basic_enums_spec.rb │ ├── count_pattern_spec.rb │ ├── count_spec.rb │ ├── find_pattern_spec.rb │ ├── find_spec.rb │ ├── find_using_max_by_spec.rb │ ├── group_by_pattern_spec.rb │ ├── group_by_spec.rb │ ├── map_pattern_spec.rb │ ├── map_spec.rb │ ├── none_pattern_spec.rb │ ├── none_spec.rb │ ├── one_pattern_spec.rb │ ├── one_spec.rb │ ├── reduce_pattern_spec.rb │ ├── reduce_spec.rb │ ├── reject_pattern_spec.rb │ ├── reject_spec.rb │ ├── select_pattern_spec.rb │ ├── select_spec.rb │ ├── sort_by_pattern_spec.rb │ ├── sort_by_spec.rb │ ├── spec_helper.rb │ ├── zip_pattern_spec.rb │ └── zip_spec.rb ├── initialize ├── README.md ├── lib │ └── aardvark.rb └── spec │ ├── aardvark_spec.rb │ ├── beaver_spec.rb │ ├── cat_spec.rb │ ├── dog_spec.rb │ ├── eel_spec.rb │ ├── ferret_spec.rb │ ├── groundhog_spec.rb │ ├── horse_spec.rb │ ├── iguana_spec.rb │ ├── jackalope_spec.rb │ ├── kangaroo_spec.rb │ ├── lion_spec.rb │ ├── monkey_spec.rb │ ├── narwhal_spec.rb │ ├── octopus_spec.rb │ ├── parrot_spec.rb │ ├── quail_spec.rb │ └── rabbit_spec.rb ├── iteration ├── README.md └── exercises │ ├── .rspec │ └── spec │ ├── all_pattern_spec.rb │ ├── count_pattern_spec.rb │ ├── find_pattern_spec.rb │ ├── group_by_pattern_spec.rb │ ├── inject_pattern_spec.rb │ ├── map_pattern_spec.rb │ ├── max_and_min_by_pattern_spec.rb │ ├── select_pattern_spec.rb │ └── spec_helper.rb ├── mythical-creatures ├── README.md ├── lib │ └── unicorn.rb └── spec │ ├── centaur_spec.rb │ ├── direwolf_spec.rb │ ├── dragon_spec.rb │ ├── hobbit_spec.rb │ ├── lovisa_spec.rb │ ├── medusa_spec.rb │ ├── ogre_spec.rb │ ├── pirate_spec.rb │ ├── spec_helper.rb │ ├── unicorn_spec.rb │ ├── vampire_spec.rb │ ├── werewolf_spec.rb │ └── wizard_spec.rb ├── objects-and-methods ├── README.md ├── exercise-1 │ ├── lib │ │ └── .keep │ └── spec │ │ ├── bag_spec.rb │ │ ├── candy_spec.rb │ │ ├── costume_spec.rb │ │ └── trick_or_treater_spec.rb └── exercise-2 │ ├── lib │ └── .keep │ └── spec │ ├── bag_spec.rb │ ├── candy_spec.rb │ ├── costume_spec.rb │ └── trick_or_treater_spec.rb └── problem-solving ├── .rspec ├── README.md └── spec ├── character_count_spec.rb ├── escape_characters_spec.rb ├── hello_spec.rb ├── mad_lib_spec.rb ├── retirement_spec.rb ├── simple_math_spec.rb └── spec_helper.rb /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.7.2 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gem "rspec" 4 | gem "pry" 5 | gem "pry-byebug" 6 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | byebug (11.1.3) 5 | coderay (1.1.2) 6 | diff-lcs (1.4.4) 7 | method_source (1.0.0) 8 | pry (0.13.1) 9 | coderay (~> 1.1) 10 | method_source (~> 1.0) 11 | pry-byebug (3.9.0) 12 | byebug (~> 11.0) 13 | pry (~> 0.13.0) 14 | rspec (3.10.0) 15 | rspec-core (~> 3.10.0) 16 | rspec-expectations (~> 3.10.0) 17 | rspec-mocks (~> 3.10.0) 18 | rspec-core (3.10.1) 19 | rspec-support (~> 3.10.0) 20 | rspec-expectations (3.10.1) 21 | diff-lcs (>= 1.2.0, < 2.0) 22 | rspec-support (~> 3.10.0) 23 | rspec-mocks (3.10.1) 24 | diff-lcs (>= 1.2.0, < 2.0) 25 | rspec-support (~> 3.10.0) 26 | rspec-support (3.10.1) 27 | 28 | PLATFORMS 29 | ruby 30 | 31 | DEPENDENCIES 32 | pry 33 | pry-byebug 34 | rspec 35 | 36 | BUNDLED WITH 37 | 2.1.4 38 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2021 Turing 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ruby Exercises 2 | 3 | This is a collection of exercises to practice various aspects of Ruby. 4 | 5 | Practicing in this manner (small, bite-sized problems that you can do repeatedly) is a _fantastic_ way to solidify programming concepts. 6 | 7 | Each folder in this repository is a set of related exercises. Open up the folder and read the README to learn more about them. 8 | 9 | ## Structure of this repository 10 | - Each folder contains a `README.md` file that provides a summary of what skills you will develop with these exercises 11 | - Each exercise file within the folder will begin with comments providing more detail on: 12 | 1. How you need to manipulate or work with this file to complete the exercise. 13 | 2. Some exercises will require you to manipulate the "current" file and run it. Other exercises will require you to create _new_ files and then reference those files for the "current" file to work. 14 | 15 | ## Setup 16 | 17 | ### 1. Clone this repository 18 | 19 | You don't need to fork this repository; clone it to your laptop 20 | 21 | in your `/turing` directory on your laptop (or whatever directory you want this repository to live inside of), run: 22 | 23 | ``` 24 | // using ssh keys 25 | git clone git@github.com:turingschool/ruby-exercises.git 26 | // using https, if the above doesn't work: 27 | git clone https://github.com/turingschool/ruby-exercises.git 28 | ``` 29 | 30 | Once this command runs, you'll now have a "local" copy of this entire repository, living right on your laptop. 31 | 32 | ### 2. From the command line, `cd` into the `ruby-exercises` directory. 33 | 34 | ### 2. Install `bundler` gem 35 | 36 | run `gem install bundler` in your terminal 37 | 38 | ### 3. From the command line, run `bundle install` 39 | 40 | You _should_ see something like this: 41 | 42 | ``` 43 | $ bundle install 44 | Fetching gem metadata from https://rubygems.org/........ 45 | Resolving dependencies... 46 | Using bundler 2.1.4 47 | Using byebug 11.1.3 48 | Fetching coderay 1.1.2 49 | Installing coderay 1.1.2 50 | Using diff-lcs 1.4.4 51 | Using method_source 1.0.0 52 | Using pry 0.13.1 53 | Fetching pry-byebug 3.9.0 54 | Installing pry-byebug 3.9.0 55 | Fetching rspec-support 3.10.1 56 | Installing rspec-support 3.10.1 57 | Fetching rspec-core 3.10.1 58 | Installing rspec-core 3.10.1 59 | Fetching rspec-expectations 3.10.1 60 | Installing rspec-expectations 3.10.1 61 | Fetching rspec-mocks 3.10.1 62 | Installing rspec-mocks 3.10.1 63 | Fetching rspec 3.10.0 64 | Installing rspec 3.10.0 65 | Bundle complete! 3 Gemfile dependencies, 12 gems now installed. 66 | Use `bundle info [gemname]` to see where a bundled gem is installed. 67 | ``` 68 | If you see that, great! 69 | 70 | If you get an error like: 71 | 72 | ``` 73 | zsh: command not found: bundle 74 | ``` 75 | 76 | you need to install the `bundler` gem. Run: 77 | 78 | ``` 79 | $ gem install bundler 80 | ``` 81 | 82 | If this command throws an error, you either: 83 | 1. Don't have `rbenv` installed 84 | 2. You've not yet installed a version of Ruby. 85 | 86 | Please check that you've followed [mod0 setup instructions](http://mod0.turing.io/setup-instructions) 87 | and this guide to [install rbenv and a version of ruby](https://github.com/turingschool-examples/backend_module_0_capstone#environment). 88 | 89 | > Hey, hold up. What is this `bundle` thing, and what does it do? what does `gem install bundler` do? 90 | 91 | Great question! 92 | 93 | `bundler` is Ruby's [package manager](https://bundler.io/). If you want to install extra code that works with Ruby, you'll use `bundler` to do it. Once you _have_ the `bundler` installed, it is "used" by typing `bundle` into your terminal. 94 | 95 | It is used in conjunction with the `Gemfile` and `Gemfile.lock` files you see in this repository, to manage Ruby gems. 96 | 97 | Here's an exhaustive amount of information about gems, if you're so inclined: [guides.rubygems.org/what-is-a-gem](https://guides.rubygems.org/what-is-a-gem/) 98 | 99 | ------------------- 100 | 101 | Once `bundle` has run successfully, open up the first test! 102 | 103 | ``` 104 | $ cd data-types/strings 105 | $ atom . 106 | ``` 107 | 108 | And read through the `README.md` for further instructions! 109 | 110 | If you want to view the instructions in your web browser, you can view them here: [data-types/strings/README.md](https://github.com/turingschool/ruby-exercises/tree/main/data-types/strings) 111 | 112 | 113 | ---------------------------------- 114 | -------------------------------------------------------------------------------- /command-query/README.md: -------------------------------------------------------------------------------- 1 | # Command/Query Separation 2 | 3 | ## Queries 4 | 5 | Sometimes we want to get information about something. Is it Wednesday? How many people have signed up to attend the event? Which channel is your favorite? Asking the question doesn't change anything. It's either Wednesday or it isn't, and asking the question doesn't make it Thursday. 6 | 7 | ## Commands 8 | 9 | Other times, we do something to the world, and something changes. We throw something in the trash. Now the trash can has more stuff in it. We buy a drink. Now we have less money. 10 | 11 | ## One or the Other 12 | 13 | In many software systems we try to keep a clear separation between obtaining data and changing things. When we're asking about something, we don't change things, and when we change things, we don't get information back. If, when we've told the system to change, we want to know whether or not anything actually happened, or what the new situation is, we'll have to go ask. 14 | 15 | These exercises focus on these two types of operations. 16 | 17 | ## Exercises 18 | 19 | - `leather_chair_spec.rb` 20 | - `tire_spec.rb` 21 | - `pills_spec.rb` 22 | - `floor_spec.rb` 23 | - `milk_bottle_spec.rb` 24 | - `person_spec.rb` 25 | - `door_spec.rb` 26 | - `teeth_spec.rb` 27 | - `music_spec.rb` 28 | - `drops_spec.rb` 29 | - `light_spec.rb` 30 | - `baby_spec.rb` 31 | - `kid_spec.rb` 32 | - `beers_spec.rb` 33 | - `teenager_spec.rb` 34 | - `adult_spec.rb` 35 | - `roll_call_spec.rb` 36 | - `santa_spec.rb` 37 | - `dog_spec.rb` 38 | - `children_spec.rb` 39 | - `water_spec.rb` 40 | - `clock_spec.rb` 41 | - `appointments_spec.rb` 42 | - `yak_spec.rb` 43 | - `money_spec.rb` 44 | - `cupcake_spec.rb` 45 | - `student_spec.rb` 46 | - `catalogue_spec.rb` 47 | - `wallet_spec.rb` 48 | - `clearance_spec.rb` 49 | 50 | ## Go make up more 51 | 52 | ... and submit pull requests 53 | -------------------------------------------------------------------------------- /command-query/exercises/adult_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'adult' 3 | 4 | RSpec.describe Adult do 5 | it 'does not get drunk too easily' do 6 | adult = Adult.new 7 | 8 | adult.consume_an_alcoholic_beverage 9 | expect(adult.sober?).to be true 10 | 11 | adult.consume_an_alcoholic_beverage 12 | expect(adult.sober?).to be true 13 | 14 | adult.consume_an_alcoholic_beverage 15 | expect(adult.sober?).to be false 16 | 17 | adult.consume_an_alcoholic_beverage 18 | expect(adult.sober?).to be false 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /command-query/exercises/apple_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'apple' 3 | 4 | RSpec.describe Apple do 5 | it 'is not ripe when created' do 6 | apple = Apple.new 7 | 8 | expect(apple.ripe?).to be false 9 | end 10 | 11 | it 'is not ripe until it is three weeks old' do 12 | apple = Apple.new 13 | 14 | 2.times { apple.wait_a_week } 15 | expect(apple.ripe?).to be false 16 | 17 | 1.times { apple.wait_a_week } 18 | expect(apple.ripe?).to be true 19 | end 20 | end 21 | 22 | -------------------------------------------------------------------------------- /command-query/exercises/appointments_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'appointments' 3 | 4 | RSpec.describe Appointments do 5 | it 'has no appointments' do 6 | slots = Appointments.new 7 | 8 | expect(slots.earliest).to be_nil 9 | end 10 | 11 | it 'has an earliest one' do 12 | slots = Appointments.new 13 | t1 = Time.new(2014, 3, 14, 4, 30) 14 | 15 | slots.at(t1) 16 | 17 | expect(slots.earliest).to eq(t1) 18 | end 19 | 20 | it 'has an earliest of several' do 21 | slots = Appointments.new 22 | t1 = Time.new(2014, 6, 14, 16, 30) 23 | t2 = Time.new(2014, 2, 28, 8, 30) 24 | t3 = Time.new(2014, 2, 28, 11) 25 | slots.at t1 26 | slots.at t2 27 | slots.at t3 28 | 29 | expect(slots.earliest).to eq(t2) 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /command-query/exercises/baby_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'baby' 3 | 4 | RSpec.describe Baby do 5 | it 'is tired' do 6 | baby = Baby.new 7 | 8 | expect(baby.tired?).to be true 9 | end 10 | 11 | it 'is not tired after a nap' do 12 | baby = Baby.new 13 | 14 | baby.nap 15 | 16 | expect(baby.tired?).to be false 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /command-query/exercises/beers_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'beers' 3 | 4 | RSpec.describe Beers do 5 | it 'starts at 99' do 6 | beers = Beers.new 7 | 8 | expect(beers.inventory).to eq(99) 9 | end 10 | 11 | it 'decreases inventory' do 12 | beers = Beers.new 13 | beers.take_one_down_and_pass_it_around 14 | 15 | expect(beers.inventory).to eq(98) 16 | 17 | 53.times { beers.take_one_down_and_pass_it_around } 18 | expect(beers.inventory).to eq(45) 19 | end 20 | 21 | it 'restocks' do 22 | beers = Beers.new 23 | 24 | 43.times { beers.take_one_down_and_pass_it_around } 25 | 26 | beers.restock 27 | 28 | expect(beers.inventory).to eq(99) 29 | 30 | 5.times { beers.take_one_down_and_pass_it_around } 31 | beers.restock 32 | expect(beers.inventory).to eq(99) 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /command-query/exercises/catalog_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'product' 3 | require_relative 'catalog' 4 | 5 | RSpec.describe Catalog do 6 | it 'starts with no products' do 7 | catalog = Catalog.new 8 | expect(catalog.cheapest).to be_nil 9 | end 10 | 11 | it 'has a cheapest of one' do 12 | catalog = Catalog.new 13 | catalog << Product.new('scissors', 8) 14 | expect(catalog.cheapest).to eq('scissors') 15 | end 16 | 17 | it 'has a cheapest product' do 18 | catalog = Catalog.new 19 | catalog << Product.new('cupcake', 3) 20 | catalog << Product.new('shirt', 23) 21 | catalog << Product.new('button', 1) 22 | 23 | expect(catalog.cheapest).to eq('button') 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /command-query/exercises/children_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'child' 3 | require_relative 'children' 4 | 5 | RSpec.describe Children do 6 | it 'has no eldest' do 7 | children = Children.new 8 | 9 | expect(children.eldest).to be_nil 10 | end 11 | 12 | it 'has one eldest' do 13 | children = Children.new 14 | children << Child.new('Sarah', 5) 15 | 16 | expect(children.eldest.name).to eq('Sarah') 17 | end 18 | 19 | it 'gets the eldest of several' do 20 | children = Children.new 21 | children << Child.new('Robert', 2) 22 | children << Child.new('Fran', 8) 23 | children << Child.new('Hilbert', 4) 24 | expect(children.eldest.name).to eq('Fran') 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /command-query/exercises/clearance_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'clearance' 3 | require_relative 'item' 4 | 5 | RSpec.describe Clearance do 6 | it 'has no items on clearance' do 7 | clearance = Clearance.new 8 | expect(clearance.best_deal).to be_nil 9 | end 10 | 11 | it 'has highest percent off one item' do 12 | clearance = Clearance.new 13 | # the discount here is a price, so this discount would be 20 percent 14 | clearance << Item.new('socks', price: 5, discount: 1) 15 | 16 | expect(clearance.best_deal).to eq('socks') 17 | end 18 | 19 | it 'has higest percent off many items' do 20 | clearance = Clearance.new 21 | clearance << Item.new('shirt', price: 16, discount: 2) 22 | clearance << Item.new('pants', price: 10, discount: 5) 23 | clearance << Item.new('jacket', price: 30, discount: 10) 24 | 25 | expect(clearance.best_deal).to eq('pants') 26 | end 27 | end 28 | 29 | -------------------------------------------------------------------------------- /command-query/exercises/clock_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'clock' 3 | 4 | RSpec.describe Clock do 5 | it 'starts at 6' do 6 | clock = Clock.new 7 | expect(clock.time).to eq(6) 8 | end 9 | 10 | it 'measures the passage of time' do 11 | clock = Clock.new 12 | clock.wait 13 | expect(clock.time).to eq(7) 14 | 15 | 3.times { clock.wait } 16 | expect(clock.time).to eq(10) 17 | end 18 | 19 | it 'is not in military time' do 20 | clock = Clock.new 21 | 22 | 8.times { clock.wait } 23 | expect(clock.time).to eq(2) 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /command-query/exercises/cupcakes_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'cupcake' 3 | require_relative 'cupcakes' 4 | 5 | RSpec.describe Cupcakes do 6 | it 'has no sweetest when there are no cupcakes' do 7 | cupcakes = Cupcakes.new 8 | expect(cupcakes.sweetest).to be_nil 9 | end 10 | 11 | it 'has a sweetest with one cupcake' do 12 | cupcakes = Cupcakes.new 13 | cupcakes << Cupcake.new('Carrot', 5) # 5 grams of sugar 14 | 15 | expect(cupcakes.sweetest.flavor).to eq('Carrot') 16 | end 17 | 18 | it 'has a sweetest cupcake' do 19 | cupcakes = Cupcakes.new 20 | cupcakes << Cupcake.new('Carrot', 5) 21 | cupcakes << Cupcake.new('Caramel', 12) 22 | cupcakes << Cupcake.new('Chocolate', 8) 23 | 24 | expect(cupcakes.sweetest.flavor).to eq('Caramel') 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /command-query/exercises/dog_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'dog' 3 | 4 | RSpec.describe Dog do 5 | it 'is hungry' do 6 | dog = Dog.new 7 | 8 | expect(dog.hungry?).to be true 9 | end 10 | 11 | xit 'eats' do 12 | dog = Dog.new 13 | dog.eat 14 | 15 | expect(dog.hungry?).to be false 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /command-query/exercises/door_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'door' 3 | 4 | RSpec.describe Door do 5 | it 'is locked' do 6 | door = Door.new 7 | 8 | expect(door.locked?).to be true 9 | end 10 | 11 | xit 'unlocks the door' do 12 | door = Door.new 13 | 14 | door.unlock 15 | 16 | expect(door.locked?).to be false 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /command-query/exercises/drops_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'drops' 3 | 4 | RSpec.describe Drops do 5 | it 'has no drops' do 6 | drops = Drops.new 7 | 8 | expect(drops.count).to eq(0) 9 | end 10 | 11 | xit 'drips' do 12 | drops = Drops.new 13 | drops.drip 14 | 15 | expect(drops.count).to eq(1) 16 | 17 | 3.times { drops.drip } 18 | 19 | expect(drops.count).to eq(4) 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /command-query/exercises/floor_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'floor' 3 | 4 | RSpec.describe Floor do 5 | it 'is dirty by default' do 6 | floor = Floor.new 7 | 8 | expect(floor.dirty?).to be true 9 | end 10 | 11 | xit 'is clean after it is washed' do 12 | floor = Floor.new 13 | 14 | floor.wash 15 | 16 | expect(floor.dirty?).to be false 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /command-query/exercises/kid_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'kid' 3 | 4 | RSpec.describe Kid do 5 | it 'has not eaten sugar' do 6 | kid = Kid.new 7 | 8 | expect(kid.grams_of_sugar_eaten).to eq(0) 9 | end 10 | 11 | xit 'gets 5 grams from eating candy' do 12 | kid = Kid.new 13 | 14 | kid.eat_candy 15 | 16 | expect(kid.grams_of_sugar_eaten).to eq(5) 17 | 18 | 5.times { kid.eat_candy } 19 | 20 | expect(kid.grams_of_sugar_eaten).to eq(30) 21 | end 22 | 23 | xit 'is not hyperactive' do 24 | kid = Kid.new 25 | 26 | expect(kid.hyperactive?).to be false 27 | end 28 | 29 | xit 'is hyperactive after 60 grams of sugar' do 30 | kid = Kid.new 31 | 32 | 11.times { kid.eat_candy } 33 | 34 | expect(kid.hyperactive?).to be false 35 | 36 | kid.eat_candy 37 | 38 | expect(kid.hyperactive?).to be true 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /command-query/exercises/leather_chair_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'leather_chair' 3 | 4 | RSpec.describe LeatherChair do 5 | it 'is not faded' do 6 | chair = LeatherChair.new 7 | 8 | expect(chair.faded?).to be false 9 | end 10 | 11 | xit 'becomes faded when exposed to sunlight' do 12 | chair = LeatherChair.new 13 | 14 | chair.expose_to_sunlight 15 | 16 | expect(chair.faded?).to be true 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /command-query/exercises/light_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'light' 3 | 4 | RSpec.describe Light do 5 | it 'is off' do 6 | light = Light.new 7 | 8 | expect(light.on?).to be false 9 | end 10 | 11 | it 'can be turned on' do 12 | light = Light.new 13 | 14 | light.turn_on 15 | 16 | expect(light.on?).to be true 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /command-query/exercises/milk_bottle_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'milk_bottle' 3 | 4 | RSpec.describe MilkBottle do 5 | it 'starts off as full' do 6 | bottle = MilkBottle.new 7 | expect(bottle.full?).to be true 8 | end 9 | 10 | xit 'spills milk' do 11 | bottle = MilkBottle.new 12 | 13 | bottle.spill 14 | 15 | expect(bottle.full?).to be false 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /command-query/exercises/money_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'money' 3 | 4 | RSpec.describe Money do 5 | it 'starts at zero' do 6 | money = Money.new 7 | 8 | expect(money.amount).to eq(0) 9 | end 10 | 11 | xit 'can earn money' do 12 | money = Money.new 13 | 14 | money.earn(20) 15 | 16 | expect(money.amount).to eq(20) 17 | 18 | money.earn(13) 19 | 20 | expect(money.amount).to eq(33) 21 | end 22 | end 23 | 24 | -------------------------------------------------------------------------------- /command-query/exercises/music_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'music' 3 | 4 | RSpec.describe Music do 5 | it 'is not loud' do 6 | music = Music.new 7 | 8 | expect(music.loud?).to be false 9 | end 10 | 11 | xit 'is loud after turning up the volume' do 12 | music = Music.new 13 | 14 | music.turn_up 15 | 16 | expect(music.loud?).to be true 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /command-query/exercises/person_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'person' 3 | 4 | RSpec.describe Person do 5 | it 'is zero years old when born' do 6 | person = Person.new 7 | 8 | expect(person.age).to eq(0) 9 | end 10 | 11 | xit 'gets older' do 12 | person = Person.new 13 | 14 | person.happy_birthday 15 | 16 | expect(person.age).to eq(1) 17 | 18 | 32.times { person.happy_birthday } 19 | 20 | expect(person.age).to eq(33) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /command-query/exercises/pills_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'pills' 3 | 4 | RSpec.describe Pills do 5 | it 'bottle starts with 60 pills' do 6 | pills = Pills.new 7 | 8 | expect(pills.count).to eq(60) 9 | end 10 | 11 | xit 'it pops a pill' do 12 | pills = Pills.new 13 | 14 | pills.pop 15 | 16 | expect(pills.count).to eq(59) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /command-query/exercises/roll_call_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'roll_call' 3 | 4 | RSpec.describe RollCall do 5 | it 'has no names' do 6 | roll_call = RollCall.new 7 | expect(roll_call.longest_name).to be nil 8 | end 9 | 10 | xit 'has a longest of one' do 11 | roll_call = RollCall.new 12 | 13 | roll_call << 'Oda' 14 | 15 | expect(roll_call.longest_name).to eq('Oda') 16 | end 17 | 18 | xit 'has longest of several' do 19 | roll_call = RollCall.new 20 | roll_call << "Ann" 21 | roll_call << "Alexandra" 22 | roll_call << "Roger" 23 | 24 | expect(roll_call.longest_name).to eq('Alexandra') 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /command-query/exercises/santa_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'santa' 3 | 4 | RSpec.describe Santa do 5 | it 'fits down the chimney' do 6 | santa = Santa.new 7 | 8 | expect(santa.fits?).to be true 9 | end 10 | 11 | xit 'does not fit down the chimney if he eats too many cookies' do 12 | santa = Santa.new 13 | santa.eats_cookies 14 | 15 | expect(santa.fits?).to be true 16 | 17 | santa.eats_cookies 18 | expect(santa.fits?).to be true 19 | 20 | santa.eats_cookies 21 | expect(santa.fits?).to be false 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /command-query/exercises/student_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'student' 3 | 4 | RSpec.describe Student do 5 | it 'has a mediocre grade' do 6 | student = Student.new 7 | 8 | expect(student.grade).to eq('C') 9 | end 10 | 11 | xit 'can improve its grade' do 12 | student = Student.new 13 | 14 | student.study 15 | expect(student.grade).to eq('B') 16 | 17 | student.study 18 | expect(student.grade).to eq('A') 19 | end 20 | 21 | xit 'can only get so good' do 22 | student = Student.new 23 | 24 | 3.times { student.study } 25 | 26 | expect(student.grade).to eq('A') 27 | end 28 | 29 | xit 'can get worse' do 30 | student = Student.new 31 | 32 | student.slack_off 33 | expect(student.grade).to eq('D') 34 | 35 | student.slack_off 36 | expect(student.grade).to eq('F') 37 | end 38 | 39 | xit 'can only get so worse' do 40 | student = Student.new 41 | 42 | 100.times { student.slack_off } 43 | expect(student.grade).to eq('F') 44 | end 45 | 46 | xit 'slacking off is immediately noticable' do 47 | student = Student.new 48 | 49 | 100.times { student.study } 50 | student.slack_off 51 | 52 | expect(student.grade).to eq('B') 53 | end 54 | 55 | xit 'however, so is studying' do 56 | student = Student.new 57 | 58 | 100.times { student.slack_off } 59 | student.study 60 | 61 | expect(student.grade).to eq('D') 62 | 63 | end 64 | end 65 | 66 | -------------------------------------------------------------------------------- /command-query/exercises/teeth_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'teeth' 3 | 4 | RSpec.describe Teeth do 5 | it 'isnt clean by default' do 6 | teeth = Teeth.new 7 | 8 | expect(teeth.clean?).to be false 9 | end 10 | 11 | xit 'are clean after brushing them' do 12 | teeth = Teeth.new 13 | 14 | teeth.brush 15 | expect(teeth.clean?).to be true 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /command-query/exercises/tire_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'tire' 3 | 4 | RSpec.describe Tire do 5 | it 'does not start out flat' do 6 | tire = Tire.new 7 | 8 | expect(tire.flat?).to be false 9 | end 10 | 11 | xit 'can have a blowout' do 12 | tire = Tire.new 13 | 14 | tire.blow_out 15 | expect(tire.flat?).to be true 16 | end 17 | 18 | end 19 | 20 | -------------------------------------------------------------------------------- /command-query/exercises/wallet_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'wallet' 3 | 4 | RSpec.describe Wallet do 5 | it 'starts at zero' do 6 | wallet = Wallet.new 7 | 8 | expect(wallet.cents).to eq(0) 9 | end 10 | 11 | xit 'can add pennies' do 12 | wallet = Wallet.new 13 | 14 | wallet << :penny 15 | expect(wallet.cents).to eq(1) 16 | 17 | 3.times { wallet << :penny } 18 | expect(wallet.cents).to eq(4) 19 | end 20 | 21 | xit 'can add nickels' do 22 | wallet = Wallet.new 23 | 24 | wallet << :nickel 25 | expect(wallet.cents).to eq(5) 26 | 27 | 3.times { wallet << :nickel } 28 | expect(wallet.cents).to eq(20) 29 | end 30 | 31 | xit 'can add dimes' do 32 | wallet = Wallet.new 33 | 34 | wallet << :dime 35 | expect(wallet.cents).to eq(10) 36 | 37 | 3.times { wallet << :dime } 38 | expect(wallet.cents).to eq(40) 39 | end 40 | 41 | xit 'can add quarters' do 42 | wallet = Wallet.new 43 | 44 | wallet << :quarter 45 | expect(wallet.cents).to eq(25) 46 | 47 | 3.times { wallet << :quarter } 48 | expect(wallet.cents).to eq(100) 49 | end 50 | 51 | xit 'can take coins out' do 52 | wallet = Wallet.new 53 | wallet << :penny 54 | wallet << :penny 55 | wallet << :penny 56 | wallet << :penny 57 | 58 | wallet.take(:penny) 59 | expect(wallet.cents).to eq(3) 60 | 61 | wallet.take(:penny, :penny) 62 | expect(wallet.cents).to eq(1) 63 | end 64 | 65 | xit 'can taake various coins out' do 66 | wallet = Wallet.new 67 | wallet << :penny 68 | wallet << :dime 69 | wallet << :quarter 70 | wallet << :quarter 71 | 72 | wallet.take(:dime, :quarter) 73 | expect(wallet.cents).to eq(26) 74 | end 75 | 76 | xit 'ignores coins that arent there' do 77 | wallet = Wallet.new 78 | wallet << :penny 79 | wallet.take(:dime) 80 | 81 | expect(wallet.cents).to eq(1) 82 | end 83 | -------------------------------------------------------------------------------- /command-query/exercises/water_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'water' 3 | 4 | RSpec.describe Water do 5 | it 'is at room temperature' do 6 | water = Water.new 7 | 8 | expect(water.temperature).to eq(295) # Measured in Kelvin 9 | end 10 | 11 | xit 'can be heated' do 12 | water = Water.new 13 | 14 | water.heat 15 | expect(water.temperature).to eq(296) 16 | 17 | 20.times { water.heat } 18 | expect(water.temperature).to eq(316) 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /command-query/exercises/yak_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'yak' 3 | 4 | RSpec.describe Yak do 5 | it 'is hairy' do 6 | yak = Yak.new 7 | 8 | expect(yak.hairy?).to be true 9 | end 10 | 11 | xit 'can be shaved' do 12 | yak = Yak.new 13 | 14 | yak.shave 15 | 16 | expect(yak.hairy?).to be false 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /data-types/collections/.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /data-types/collections/README.md: -------------------------------------------------------------------------------- 1 | The files in this directory contain several exercises for you to complete. 2 | 3 | Before you begin, look at the Ruby Docs ([https://ruby-doc.org/core-2.6.5/](https://ruby-doc.org/core-2.6.5/)) for `Array` ([https://ruby-doc.org/core-2.6.5/Array.html](https://ruby-doc.org/core-2.6.5/Array.html)) and `Hash` ([https://ruby-doc.org/core-2.6.5/Hash.html](https://ruby-doc.org/core-2.6.5/Hash.html)). 4 | 5 | Research the listed methods below for each exercise. You will need them to complete the exercises. 6 | 7 | The exercises will require you to look up some additional methods not listed here. 8 | 9 | An alum has put together a video walk-through of the `arrays.rb` and `advanced_nested_collections` exercise. 10 | 11 | Please check them out - you'll learn how to use `binding.pry` to quickly test ideas and assumptions, as well as many other useful approaches to solving problems with code. 12 | 13 | - `arrays.rb` walkthrough: [https://youtu.be/RUnd1Uu0AyE](https://youtu.be/RUnd1Uu0AyE) 14 | - `advanced_nested_collections/nesting_spec.rb` walkthrough: [https://youtu.be/9AaElA4elDU](https://youtu.be/9AaElA4elDU) 15 | 16 | 17 | ### Methods you'll need to use for `arrays.rb` 18 | 19 | * `[]` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-5B-5D)) 20 | * `[]=` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-5B-5D-3D)) 21 | * `<<` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-3C-3C)) 22 | * `push` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-push)) 23 | * `pop` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-pop)) 24 | * `length`/size ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-length)) 25 | * `rotate` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-rotate)) 26 | * `include`? ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-include-3F)) 27 | * `flatten` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-flatten)) 28 | * `compact` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-compact)) 29 | * `join` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-join)) 30 | * `shift` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-shift)) 31 | * `unshift` ([docs](https://ruby-doc.org/core-2.6.5/Array.html#method-i-unshift)) 32 | 33 | ### methods you'll need to use for `hashes.rb` 34 | 35 | * `new(default)` - this version of .new gives the hash default values 36 | * `[]` 37 | * `[]=` 38 | * `delete` 39 | * `keys` 40 | * `values` 41 | * `length/size` 42 | -------------------------------------------------------------------------------- /data-types/collections/advanced_nested_collections/.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /data-types/collections/advanced_nested_collections/spec/advanced_nesting_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative 'nesting' 2 | 3 | # The intent of this exercise is to practice working with nested collections. 4 | # Some tests will be able to pass without any enumeration, and others will require 5 | # more complex iteration over multiple portions of the nested collection. 6 | 7 | # All tests can be completed without using any enumerable other than #each. 8 | # My suggestion would be to complete all tests using only #each, and then 9 | # to go back over each test and refactor it using a different enumerable. 10 | 11 | # The collection you're going to be using lives in ./nesting.rb and is called stores. 12 | # If you spot an error or want to make this exercise better, please let us know! 13 | 14 | 15 | RSpec.describe 'Advanced Nested Collections' do 16 | it 'test 1' do 17 | # EXAMPLE 18 | employees = stores[:olive_garden][:employees] 19 | 20 | expected = ["Jeff", "Zach", "Samantha"] 21 | expect(employees).to eq(expected) 22 | end 23 | 24 | xit 'test 2' do 25 | # Find the ingredients for pancakes 26 | pancake_ingredients = _____ 27 | 28 | expected = ["Flour", "Eggs", "Milk", "Syrup"] 29 | expect(pancake_ingredients).to eq(expected) 30 | end 31 | 32 | xit 'test 3' do 33 | # Find the price of risotto 34 | risotto_price = ____ 35 | 36 | expect(risotto_price).to eq(12) 37 | end 38 | 39 | xit 'test 4' do 40 | # Find the ingredients for a Big Mac 41 | big_mac_ingredients = ____ 42 | 43 | expected = ['Bun','Hamburger','Ketchup','pickles'] 44 | expect(big_mac_ingredients).to eq(expected) 45 | end 46 | 47 | xit 'test 5' do 48 | # Find a list of restaurants 49 | store_names = ____ 50 | 51 | expected = [:olive_garden, :dennys, :macdonalds] 52 | expect(store_names).to eq(expected) 53 | end 54 | 55 | xit 'test 6' do 56 | # Find dishes names for Olive Garden 57 | dishes_names = ____ 58 | 59 | expect(dishes_names).to eq(['Risotto', 'Steak']) 60 | end 61 | 62 | xit 'test 7' do 63 | # Return a list of employees across 64 | # all restaurants 65 | employee_names = ____ 66 | 67 | expected = ["Jeff", "Zach", "Samantha", "Bob", "Sue", "James", "Alvin", "Simon", "Theodore"] 68 | expect(employee_names).to eq(expected) 69 | end 70 | 71 | xit 'test 8' do 72 | # Return a list of all ingredients 73 | # across all restaurants 74 | ingredients = ____ 75 | 76 | expected = [ 77 | "Rice", 78 | "Cheese", 79 | "Butter", 80 | "Beef", 81 | "Garlic", 82 | "Flour", 83 | "Eggs", 84 | "Milk", 85 | "Syrup", 86 | "Flour", 87 | "Eggs", 88 | "Syrup", 89 | "Bun", 90 | "Hamburger", 91 | "Ketchup", 92 | "pickles", 93 | "Potatoes", 94 | "Salt" 95 | ] 96 | expect(ingredients).to eq(expected) 97 | end 98 | 99 | xit 'test 9' do 100 | # Return the full menu price for Olive Garden 101 | full_menu_price = ____ 102 | 103 | expect(full_menu_price).to eq(27) 104 | end 105 | 106 | xit 'test 10' do 107 | # Return the full menu for Olive Garden 108 | 109 | olive_garden_menu = _____ 110 | 111 | expected = { 112 | "Risotto" => { 113 | :name => "Risotto", 114 | :ingredients => ["Rice", "Cheese", "Butter"], 115 | :price => 12 116 | }, 117 | "Steak" => { 118 | :name => "Steak", 119 | :ingredients => ["Beef", "Garlic"], 120 | :price => 15 121 | } 122 | } 123 | expect(olive_garden_menu).to eq(expected) 124 | end 125 | 126 | xit 'test 11' do 127 | # Return a full menu across all restaurants 128 | full_menu = ____ 129 | 130 | expected = { 131 | "Risotto" => { 132 | :name => "Risotto", 133 | :ingredients => ["Rice", "Cheese", "Butter"], 134 | :price => 12 135 | }, 136 | "Steak" => { 137 | :name => "Steak", 138 | :ingredients => ["Beef", "Garlic"], 139 | :price => 15 140 | }, 141 | "Pancakes" => { 142 | :name => "Pancakes", 143 | :ingredients => ["Flour", "Eggs", "Milk", "Syrup"], 144 | :price => 10 145 | }, 146 | "Waffles" => { 147 | :name => "Waffles", 148 | :ingredients => ["Flour", "Eggs", "Syrup"], 149 | :price => 7 150 | }, 151 | "Big Mac" => { 152 | :name => "Big Mac", 153 | :ingredients => ["Bun", "Hamburger", "Ketchup", "pickles"], 154 | :price => 5 155 | }, 156 | "Fries" => { 157 | :name => "Fries", 158 | :ingredients => ["Potatoes", "Salt"], 159 | :price => 2 160 | } 161 | } 162 | expect(full_menu).to eq(expected) 163 | end 164 | end 165 | -------------------------------------------------------------------------------- /data-types/collections/advanced_nested_collections/spec/nesting.rb: -------------------------------------------------------------------------------- 1 | def stores 2 | { 3 | olive_garden: { 4 | employees: [ 5 | 'Jeff', 6 | 'Zach', 7 | 'Samantha' 8 | ], 9 | dishes: [ 10 | { 11 | name: 'Risotto', 12 | ingredients: [ 13 | 'Rice', 14 | 'Cheese', 15 | 'Butter' 16 | ], 17 | price: 12 18 | }, 19 | { 20 | name: 'Steak', 21 | ingredients: [ 22 | 'Beef', 23 | 'Garlic' 24 | ], 25 | price: 15 26 | } 27 | ] 28 | }, 29 | dennys: { 30 | employees: [ 31 | 'Bob', 32 | 'Sue', 33 | 'James' 34 | ], 35 | dishes: [ 36 | { 37 | name: 'Pancakes', 38 | ingredients: [ 39 | 'Flour', 40 | 'Eggs', 41 | 'Milk', 42 | 'Syrup' 43 | ], 44 | price: 10 45 | }, 46 | { 47 | name: 'Waffles', 48 | ingredients: [ 49 | 'Flour', 50 | 'Eggs', 51 | 'Syrup' 52 | ], 53 | price: 7 54 | } 55 | ] 56 | }, 57 | macdonalds: { 58 | employees: [ 59 | 'Alvin', 60 | 'Simon', 61 | 'Theodore' 62 | ], 63 | dishes: [ 64 | { 65 | name: 'Big Mac', 66 | ingredients: [ 67 | 'Bun', 68 | 'Hamburger', 69 | 'Ketchup', 70 | 'pickles' 71 | ], 72 | price: 5 73 | }, 74 | { 75 | name: 'Fries', 76 | ingredients: [ 77 | 'Potatoes', 78 | 'Salt' 79 | ], 80 | price: 2 81 | } 82 | ] 83 | } 84 | } 85 | end 86 | -------------------------------------------------------------------------------- /data-types/collections/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause 4 | # this file to always be loaded, without a need to explicitly require it in any 5 | # files. 6 | # 7 | # Given that it is always loaded, you are encouraged to keep this file as 8 | # light-weight as possible. Requiring heavyweight dependencies from this file 9 | # will add to the boot time of your test suite on EVERY test run, even for an 10 | # individual file that may not need all of that loaded. Instead, consider making 11 | # a separate helper file that requires the additional dependencies and performs 12 | # the additional setup, and require it from the spec files that actually need 13 | # it. 14 | # 15 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 16 | RSpec.configure do |config| 17 | # rspec-expectations config goes here. You can use an alternate 18 | # assertion/expectation library such as wrong or the stdlib/minitest 19 | # assertions if you prefer. 20 | config.expect_with :rspec do |expectations| 21 | # This option will default to `true` in RSpec 4. It makes the `description` 22 | # and `failure_message` of custom matchers include text for helper methods 23 | # defined using `chain`, e.g.: 24 | # be_bigger_than(2).and_smaller_than(4).description 25 | # # => "be bigger than 2 and smaller than 4" 26 | # ...rather than: 27 | # # => "be bigger than 2" 28 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 29 | end 30 | 31 | # rspec-mocks config goes here. You can use an alternate test double 32 | # library (such as bogus or mocha) by changing the `mock_with` option here. 33 | config.mock_with :rspec do |mocks| 34 | # Prevents you from mocking or stubbing a method that does not exist on 35 | # a real object. This is generally recommended, and will default to 36 | # `true` in RSpec 4. 37 | mocks.verify_partial_doubles = true 38 | end 39 | 40 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 41 | # have no way to turn it off -- the option exists only for backwards 42 | # compatibility in RSpec 3). It causes shared context metadata to be 43 | # inherited by the metadata hash of host groups and examples, rather than 44 | # triggering implicit auto-inclusion in groups with matching metadata. 45 | config.shared_context_metadata_behavior = :apply_to_host_groups 46 | 47 | # The settings below are suggested to provide a good initial experience 48 | # with RSpec, but feel free to customize to your heart's content. 49 | =begin 50 | # This allows you to limit a spec run to individual examples or groups 51 | # you care about by tagging them with `:focus` metadata. When nothing 52 | # is tagged with `:focus`, all examples get run. RSpec also provides 53 | # aliases for `it`, `describe`, and `context` that include `:focus` 54 | # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 55 | config.filter_run_when_matching :focus 56 | 57 | # Allows RSpec to persist some state between runs in order to support 58 | # the `--only-failures` and `--next-failure` CLI options. We recommend 59 | # you configure your source control system to ignore this file. 60 | config.example_status_persistence_file_path = "spec/examples.txt" 61 | 62 | # Limits the available syntax to the non-monkey patched syntax that is 63 | # recommended. For more details, see: 64 | # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ 65 | # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 66 | # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode 67 | config.disable_monkey_patching! 68 | 69 | # This setting enables warnings. It's recommended, but in some cases may 70 | # be too noisy due to issues in dependencies. 71 | config.warnings = true 72 | 73 | # Many RSpec users commonly either run the entire suite or an individual 74 | # file, and it's useful to allow more verbose output when running an 75 | # individual spec file. 76 | if config.files_to_run.one? 77 | # Use the documentation formatter for detailed output, 78 | # unless a formatter has already been configured 79 | # (e.g. via a command-line flag). 80 | config.default_formatter = "doc" 81 | end 82 | 83 | # Print the 10 slowest examples and example groups at the 84 | # end of the spec run, to help surface which specs are running 85 | # particularly slow. 86 | config.profile_examples = 10 87 | 88 | # Run specs in random order to surface order dependencies. If you find an 89 | # order dependency and want to debug it, you can fix the order by providing 90 | # the seed, which is printed after each run. 91 | # --seed 1234 92 | config.order = :random 93 | 94 | # Seed global randomization in this process using the `--seed` CLI option. 95 | # Setting this allows you to use `--seed` to deterministically reproduce 96 | # test failures related to randomization by passing the same `--seed` value 97 | # as the one that triggered the failure. 98 | Kernel.srand config.seed 99 | =end 100 | end 101 | -------------------------------------------------------------------------------- /data-types/ints_and_floats/.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /data-types/ints_and_floats/README.md: -------------------------------------------------------------------------------- 1 | The ints_and_floats file contain several exercises for you to work on. Make the tests pass. 2 | 3 | 4 | -------------------------------------------------------------------------------- /data-types/ints_and_floats/spec/ints_and_floats_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'ints and floats' do 2 | it 'test 1' do 3 | lucky = 7 4 | unlucky = 13 5 | # Using the two variables defined above, 6 | # add the lucky number and the unlucky number 7 | sum = ________ 8 | expect(sum).to eq(20) 9 | end 10 | 11 | xit 'test 2' do 12 | lucky = 7 13 | unlucky = 13 14 | # Using the two variables defined above, 15 | # subtract the unlucky from the lucky 16 | difference = ________ 17 | expect(difference).to eq(-6) 18 | end 19 | 20 | xit 'test 3' do 21 | lucky = 7 22 | unlucky = 13 23 | # Using the two variables defined above, 24 | # divide unlucky by lucky 25 | # NOTE: this is integer division 26 | quotient = ________ 27 | expect(quotient).to eq(1) 28 | end 29 | 30 | xit 'test 4' do 31 | lucky = 7 32 | unlucky = 13 33 | # Using the two variables defined above, 34 | # divide unlucky by lucky 35 | quotient = ________ 36 | expect(quotient).to eq(1.8571428571428572) 37 | end 38 | 39 | xit 'test 5' do 40 | lucky = 7 41 | unlucky = 13 42 | # Using the two variables defined above, 43 | # find the remainder of the unlucky divided by the lucky 44 | remainder = ____________ 45 | expect(remainder).to eq(6) 46 | end 47 | 48 | xit 'test 6' do 49 | lucky = 7 50 | # Using the variable defined above, 51 | # find out if the lucky number is even 52 | even = _________ 53 | expect(even).to eq(false) 54 | end 55 | 56 | xit 'test 7' do 57 | pi = 3.14 58 | # Using the variable defined above, 59 | # round the number to the nearest whole number 60 | rounded = _________ 61 | expect(rounded).to eq(3) 62 | end 63 | 64 | xit 'test 8' do 65 | pi = 3.14 66 | # Using the variable defined above, 67 | # round the number to one decimal place 68 | rounded = _________ 69 | expect(rounded).to eq(3.1) 70 | end 71 | 72 | xit 'test 9' do 73 | pi = 3.14 74 | # Using the variable defined above, 75 | # round the number to the next highest whole number 76 | rounded = _________ 77 | expect(rounded).to eq(4) 78 | end 79 | end 80 | -------------------------------------------------------------------------------- /data-types/strings/.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /data-types/strings/README.md: -------------------------------------------------------------------------------- 1 | The `strings_spec.rb` file has a number of exercises to help you better work with strings in ruby. 2 | 3 | 1. Make sure you followed the instructions listed in `README.md` in the root ruby-exercises folder to install `rspec` via the bundler 4 | 2. Open `strings_spec.rb` in Atom 5 | 3. In terminal, use `rspec strings_spec.rb` to execute the file 6 | * **NOTE:** Test files (typically end with _spec.rb, `example_spec.rb`) execute with `rspec` instead of the `ruby` command you are used to 7 | 4. Read the error message, fill the blanks in each `strings_spec.rb` test, save the file in Atom, and execute until each test passes 8 | * **NOTE:** Remove the `x` in `xit` to enable each test as you go – placing `skip` under the `do` line works similarly 9 | 10 | Below is a list of methods that will help you complete the exercises - research each of them on the [Ruby Docs for Strings](https://ruby-doc.org/core-2.6.5/String.html) to learn more about what they do and how they are used. 11 | 12 | There are additional methods that you will need to complete the exercises; each of these additional methods can be found in the Ruby Docs for Strings. 13 | 14 | * `String#capitalize`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-capitalize) 15 | * `String#count`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-count) 16 | * `String#concat`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-concat) 17 | * `String#chomp`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-chomp) 18 | * `String#chop`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-chop) 19 | * `String#delete`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-delete) 20 | * `String#gsub`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-gsub) 21 | * `String#include?`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-include-3F) 22 | * `String#length`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-length) 23 | * `String#reverse`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-reverse) 24 | * `String#sub`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-sub) 25 | * `String#strip`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-strip) 26 | * `String#upcase`[docs](https://ruby-doc.org/core-2.6.5/String.html#method-i-upcase) 27 | 28 | 29 | 30 | ``` 31 | -------------------------------------------------------------------------------- /data-types/strings/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause 4 | # this file to always be loaded, without a need to explicitly require it in any 5 | # files. 6 | # 7 | # Given that it is always loaded, you are encouraged to keep this file as 8 | # light-weight as possible. Requiring heavyweight dependencies from this file 9 | # will add to the boot time of your test suite on EVERY test run, even for an 10 | # individual file that may not need all of that loaded. Instead, consider making 11 | # a separate helper file that requires the additional dependencies and performs 12 | # the additional setup, and require it from the spec files that actually need 13 | # it. 14 | # 15 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 16 | RSpec.configure do |config| 17 | # rspec-expectations config goes here. You can use an alternate 18 | # assertion/expectation library such as wrong or the stdlib/minitest 19 | # assertions if you prefer. 20 | config.expect_with :rspec do |expectations| 21 | # This option will default to `true` in RSpec 4. It makes the `description` 22 | # and `failure_message` of custom matchers include text for helper methods 23 | # defined using `chain`, e.g.: 24 | # be_bigger_than(2).and_smaller_than(4).description 25 | # # => "be bigger than 2 and smaller than 4" 26 | # ...rather than: 27 | # # => "be bigger than 2" 28 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 29 | end 30 | 31 | # rspec-mocks config goes here. You can use an alternate test double 32 | # library (such as bogus or mocha) by changing the `mock_with` option here. 33 | config.mock_with :rspec do |mocks| 34 | # Prevents you from mocking or stubbing a method that does not exist on 35 | # a real object. This is generally recommended, and will default to 36 | # `true` in RSpec 4. 37 | mocks.verify_partial_doubles = true 38 | end 39 | 40 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 41 | # have no way to turn it off -- the option exists only for backwards 42 | # compatibility in RSpec 3). It causes shared context metadata to be 43 | # inherited by the metadata hash of host groups and examples, rather than 44 | # triggering implicit auto-inclusion in groups with matching metadata. 45 | config.shared_context_metadata_behavior = :apply_to_host_groups 46 | 47 | # The settings below are suggested to provide a good initial experience 48 | # with RSpec, but feel free to customize to your heart's content. 49 | =begin 50 | # This allows you to limit a spec run to individual examples or groups 51 | # you care about by tagging them with `:focus` metadata. When nothing 52 | # is tagged with `:focus`, all examples get run. RSpec also provides 53 | # aliases for `it`, `describe`, and `context` that include `:focus` 54 | # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 55 | config.filter_run_when_matching :focus 56 | 57 | # Allows RSpec to persist some state between runs in order to support 58 | # the `--only-failures` and `--next-failure` CLI options. We recommend 59 | # you configure your source control system to ignore this file. 60 | config.example_status_persistence_file_path = "spec/examples.txt" 61 | 62 | # Limits the available syntax to the non-monkey patched syntax that is 63 | # recommended. For more details, see: 64 | # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ 65 | # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 66 | # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode 67 | config.disable_monkey_patching! 68 | 69 | # This setting enables warnings. It's recommended, but in some cases may 70 | # be too noisy due to issues in dependencies. 71 | config.warnings = true 72 | 73 | # Many RSpec users commonly either run the entire suite or an individual 74 | # file, and it's useful to allow more verbose output when running an 75 | # individual spec file. 76 | if config.files_to_run.one? 77 | # Use the documentation formatter for detailed output, 78 | # unless a formatter has already been configured 79 | # (e.g. via a command-line flag). 80 | config.default_formatter = "doc" 81 | end 82 | 83 | # Print the 10 slowest examples and example groups at the 84 | # end of the spec run, to help surface which specs are running 85 | # particularly slow. 86 | config.profile_examples = 10 87 | 88 | # Run specs in random order to surface order dependencies. If you find an 89 | # order dependency and want to debug it, you can fix the order by providing 90 | # the seed, which is printed after each run. 91 | # --seed 1234 92 | config.order = :random 93 | 94 | # Seed global randomization in this process using the `--seed` CLI option. 95 | # Setting this allows you to use `--seed` to deterministically reproduce 96 | # test failures related to randomization by passing the same `--seed` value 97 | # as the one that triggered the failure. 98 | Kernel.srand config.seed 99 | =end 100 | end 101 | -------------------------------------------------------------------------------- /enumerables/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jumpstart Lab 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /enumerables/README.md: -------------------------------------------------------------------------------- 1 | # Enums Exercises 2 | 3 | This project introduces enumerable methods one at a time. 4 | 5 | First it shows how one might solve a problem using `#each`, to demonstrate 6 | that there is no magic involved. This is just plain Ruby. 7 | 8 | Second, it demonstrates how to use a specific enumerable method to solve the exact same problem. 9 | 10 | The exercises for a given enumerable method will reside in two separate 11 | test suites, one for `#each` and one for the enumerable method itself. 12 | 13 | Each test suite is built up in the following way: 14 | 15 | 1. A test that is passing, which shows the basic pattern. 16 | 2. A test where the block of the loop is missing. 17 | 3. A test where the entire loop is missing. 18 | 4. Any number of tests where all but the input data and the assertion 19 | are missing. 20 | 21 | This gradually transfers the responsibility to you, the learner, while still 22 | providing examples that you can look at. 23 | 24 | Remember that each enumerable method is first implemented in terms of `each`, 25 | so by looking at the methods in this way, we'll gain some insight 26 | into how each method is put together under the hood. 27 | 28 | ## Solving the Exercises 29 | 30 | ```shell 31 | $ cd ruby-exercises/enumerables/exercises_1 32 | ``` 33 | 34 | Open up `map_pattern_spec.rb` and `map_spec.rb` in Atom, side by side: 35 | 36 | ![Set up map and map_pattern_spec side by side](/images/enumerables-setup-map.jpg) 37 | 38 | The goal of these exercises is to help you understand enumerables, both how 39 | they work and how they can be replicated through the use of the #each, the 40 | basis of all enumerables. 41 | 42 | For each method, there are two files of interest. Let's start with `map`: 43 | 44 | 1. `exercises_1/spec/map_pattern_spec.rb` 45 | 2. `exercises_1/spec/map_spec.rb` 46 | 47 | In the `map_pattern_spec.rb` you'll find a collection of exercises which do what `map` is good at, 48 | but they do it just with `each`. This file will give us some insight into how map works internally, 49 | hopefully helping us gain a deeper understanding around when we might use each one. 50 | 51 | Then, in the other file, `map_spec.rb` you'll find the same examples using `map` itself. 52 | These examples will generally be much more concise, and will give a good demonstration 53 | of how using the appropriate enumerable method (as opposed to doing everything with `each`) 54 | can make our lives much easier! 55 | 56 | ### Running the Test Files 57 | 58 | To run each example, we'll simply use the `rspec` command in combination with the 59 | path to that file. For example to run the 2 map files above, we would use: 60 | 61 | ``` 62 | $ rspec exercises_1/spec/map_pattern_spec.rb 63 | $ rspec exercises_1/spec/map_spec.rb 64 | ``` 65 | 66 | ### Recommended Order 67 | 68 | Consider working through the exercises in this order: 69 | 70 | * `map` 71 | * `select` 72 | * `find` (aka `detect`) 73 | * `sort_by` 74 | * `count` 75 | * `reject` 76 | * `reduce` (this one is tricky!) 77 | * `any?` 78 | * `all?` 79 | * `none?` 80 | * `one?` 81 | * `group_by` (this one is tricky too!) 82 | * `zip` 83 | 84 | You should work on the `pattern` test file in conjunction with the specific enumerable. `select_pattern_spec.rb` alongside `select_spec.rb`, etc. 85 | 86 | __Recommended Workflow__ 87 | 88 | Here's how we recommend you work through the exercises: 89 | 90 | * Open your text editor with two panes (left and right) 91 | * In the left pane, open the pattern file like `map_pattern_spec.rb` 92 | * In the right pane, open the matching file like `map_spec.rb` 93 | * Run the `map_pattern_spec.rb` and solve the first exercise 94 | * Run the `map_spec.rb` and solve the same exercise 95 | * Repeat for each matching pair of exercises 96 | 97 | -------------------------------------------------------------------------------- /enumerables/exercises_1/.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /enumerables/exercises_1/README.md: -------------------------------------------------------------------------------- 1 | Most of the instructions for how to work through these exercises are contained in the `readme.md` one directory up from here. 2 | 3 | That said, here's the summary: 4 | 5 | ------------------ 6 | 7 | Open up `map_pattern_spec.rb` and `map_spec.rb` in Atom, side by side: 8 | 9 | ![Set up map and map_pattern_spec side by side](/images/enumerables-setup-map.jpg) 10 | 11 | Notice how `map_pattern_spec.rb` makes use of the `#each` method, while `map_spec.rb` uses `#map`. 12 | 13 | Remove the skip from `test_doubles` on `map_pattern_spec`; run the tests to make sure there is a failure. 14 | 15 | Next, make the test pass. 16 | 17 | Now do the same thing for `map_spec.rb` 18 | 19 | Keep ping-ponging back-and-forth as you work through the files. 20 | 21 | Here's the order to work through: 22 | 23 | * `map` 24 | * `select` 25 | * `find` (aka `detect`) 26 | * `sort_by` 27 | * `count` 28 | * `reject` 29 | * `reduce` (this one is tricky!) 30 | * `any?` 31 | * `all?` 32 | * `none?` 33 | * `one?` 34 | * `group_by` (this one is tricky too!) 35 | * `zip` 36 | 37 | When done (or before you've finished all of these, if you feel like it) go check out exercises in the `exercises_2` directory. 38 | 39 | -------------------------------------------------------------------------------- /enumerables/exercises_1/objects/gnome.rb: -------------------------------------------------------------------------------- 1 | class Gnome 2 | def initialize(type) 3 | @type = type 4 | end 5 | 6 | def roving? 7 | @type == 'roving' 8 | end 9 | end 10 | 11 | -------------------------------------------------------------------------------- /enumerables/exercises_1/objects/squid.rb: -------------------------------------------------------------------------------- 1 | class Squid 2 | def initialize(size) 3 | @size = size 4 | end 5 | 6 | def giant? 7 | @size == 'giant' 8 | end 9 | end 10 | 11 | -------------------------------------------------------------------------------- /enumerables/exercises_1/objects/thing.rb: -------------------------------------------------------------------------------- 1 | class Thing 2 | def initialize(adjective) 3 | @adjective = adjective 4 | end 5 | 6 | def weird? 7 | @adjective == 'weird' 8 | end 9 | end 10 | 11 | -------------------------------------------------------------------------------- /enumerables/exercises_1/objects/unicorn.rb: -------------------------------------------------------------------------------- 1 | class Unicorn 2 | def initialize(color) 3 | @color = color 4 | end 5 | 6 | def pink? 7 | @color == 'pink' 8 | end 9 | end 10 | 11 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/all_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'All Pattern' do 2 | it 'all zeros' do 3 | numbers = [0, 0, 0, 0, 0, 0, 0] 4 | all_zeros = true 5 | numbers.each do |number| 6 | all_zeros = false unless number.zero? 7 | end 8 | expect(all_zeros).to eq(true) 9 | end 10 | 11 | xit 'not all zeros' do 12 | numbers = [0, 0, 0, 0, 1, 0, 0, 0] 13 | all_zeros = true 14 | numbers.each do |number| 15 | # Your code goes here 16 | end 17 | expect(all_zeros).to eq(false) 18 | end 19 | 20 | xit 'all gone' do 21 | words = ["gone", "gone", "gone", "gone", "gone", "gone", "gone"] 22 | all_gone = true 23 | # Your code goes here 24 | expect(all_gone).to eq(true) 25 | end 26 | 27 | xit 'not all gone' do 28 | words = ["gone", "gone", "gone", "gone", "gone", "there", "gone", "gone"] 29 | # Your code goes here 30 | expect(all_gone).to eq(false) 31 | end 32 | 33 | xit 'all empty' do 34 | strings = ["", "", "", "", "", "", ""] 35 | # Your code goes here 36 | expect(all_empty).to eq(true) 37 | end 38 | 39 | xit 'not all empty' do 40 | strings = ["", "", "", "full", "", "", ""] 41 | # Your code goes here 42 | expect(all_empty).to eq(false) 43 | end 44 | 45 | xit 'not all uppercase' do 46 | words = ["DOUGHNUT", "CASH", "MAIN", "bOWl", "SMACK", "SAND"] 47 | # Your code goes here 48 | expect(all_caps).to eq(false) 49 | end 50 | 51 | xit 'all lies' do 52 | lies = [false, false, false, false] 53 | # Your code goes here 54 | expect(all_lies).to eq(true) 55 | end 56 | 57 | xit 'all multiples of seven' do 58 | numbers = [42, 14, 35, 49, 28, 56, 21, 7] 59 | # Your code goes here 60 | expect(all_multiples_of_7).to eq(true) 61 | end 62 | 63 | xit 'not all 3 digits long' do 64 | numbers = [981, 831, 509, 332, 892, 8999, 110] 65 | # Your code goes here 66 | expect(all_3_digits).to eq(false) 67 | end 68 | 69 | xit 'all four letter words' do 70 | words = ["love", "hate", "fire", "bird", "call"] 71 | # Your code goes here 72 | expect(all_4_letters).to eq(true) 73 | end 74 | end 75 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/all_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'all test' do 2 | it 'test all zeroes' do 3 | numbers = [0, 0, 0, 0, 0, 0, 0] 4 | all_zeros = numbers.all? do |number| 5 | number.zero? 6 | end 7 | expect(all_zeros).to eq(true) 8 | end 9 | 10 | xit 'not all zeroes' do 11 | numbers = [0, 0, 0, 0, 1, 0, 0, 0] 12 | all_zeros = numbers.all? do |number| 13 | # Your code goes here 14 | end 15 | expect(all_zeros).to eq(false) 16 | end 17 | 18 | xit 'all gone' do 19 | words = ["gone", "gone", "gone", "gone", "gone", "gone", "gone"] 20 | # Your code goes here 21 | expect(all_gone).to eq(true) 22 | end 23 | 24 | xit 'not all gone' do 25 | words = ["gone", "gone", "gone", "gone", "gone", "there", "gone", "gone"] 26 | # Your code goes here 27 | expect(all_gone).to eq(false) 28 | end 29 | 30 | xit 'all empty' do 31 | strings = ["", "", "", "", "", "", ""] 32 | # Your code goes here 33 | expect(all_empty).to eq(true) 34 | end 35 | 36 | xit 'not all empty' do 37 | strings = ["", "", "", "full", "", "", ""] 38 | # Your code goes here 39 | expect(all_empty).to eq(false) 40 | end 41 | 42 | xit 'not all uppercase' do 43 | words = ["DOUGHNUT", "CASH", "MAIN", "bOWl", "SMACK", "SAND"] 44 | # Your code goes here 45 | expect(all_uppercase).to eq(false) 46 | end 47 | 48 | xit 'all lies' do 49 | lies = [false, false, false, false] 50 | # Your code goes here 51 | expect(all_lies).to eq(true) 52 | end 53 | 54 | xit 'multiples of 7' do 55 | numbers = [42, 14, 35, 49, 28, 56, 21, 7] 56 | # Your code goes here 57 | expect(all_multiples_of_7).to eq(true) 58 | end 59 | 60 | xit 'not all three digits long' do 61 | numbers = [981, 831, 509, 332, 892, 8999, 110] 62 | # Your code goes here 63 | expect(all_3_digits).to eq(false) 64 | end 65 | 66 | xit 'all four letter words' do 67 | words = ["love", "hate", "fire", "bird", "call"] 68 | # Your code goes here 69 | expect(all_4_letters).to eq(true) 70 | end 71 | end 72 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/any_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'Any Pattern Test' do 2 | 3 | it 'has at least one zero' do 4 | numbers = [2, 0, 9, 3, 0, 1] 5 | has_zero = false 6 | numbers.each do |number| 7 | has_zero = true if number.zero? 8 | end 9 | expect(has_zero).to eq(true) 10 | end 11 | 12 | xit 'does not have any zeros' do 13 | numbers = [3, 1, 3, 2, 4, 9, 8] 14 | has_zero = false 15 | numbers.each do |number| 16 | # Your code goes here 17 | end 18 | expect(has_zero).to eq(false) 19 | end 20 | 21 | xit 'has at least one alice' do 22 | names = ["Bill", "Bob", "Burton", "Alice", "Brandon"] 23 | has_alice = false 24 | # Your code goes here 25 | expect(has_alice).to eq(true) 26 | end 27 | 28 | xit 'no alices' do 29 | names = ["Chuck", "Charlene", "Cory", "Chris", "Carl"] 30 | # Your code goes here 31 | expect(has_alice).to eq(false) 32 | end 33 | 34 | xit 'has a multi word phrase' do 35 | phrases = ["Sure!", "OK.", "I have no idea.", "Really?Whatever."] 36 | # Your code goes here 37 | expect(has_multi_word_phrase).to eq(true) 38 | end 39 | 40 | xit 'has no monkeys' do 41 | animals = ["elephant", "hippo", "jaguar", "python"] 42 | # Your code goes here 43 | expect(has_monkeys).to eq(false) 44 | end 45 | 46 | xit 'has no multiples of five' do 47 | numbers = [3, 1, 3, 2, 4, 9, 8] 48 | # Your code goes here 49 | expect(multiples_of_5).to eq(false) 50 | end 51 | end 52 | 53 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/any_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'Any test' do 2 | it 'has at least one zero' do 3 | numbers = [2, 0, 9, 3, 0, 1] 4 | has_zero = numbers.any? do |number| 5 | number.zero? 6 | end 7 | expect(has_zero).to eq(true) 8 | end 9 | 10 | xit 'does not have zeroes' do 11 | numbers = [3, 1, 3, 2, 4, 9, 8] 12 | has_zero = numbers.any? do |number| 13 | # Your code goes here 14 | end 15 | expect(has_zero).to eq(false) 16 | end 17 | 18 | xit 'has at least one alice' do 19 | names = ["Bill", "Bob", "Burton", "Alice", "Brandon"] 20 | # Your code goes here 21 | expect(has_alice).to eq(true) 22 | end 23 | 24 | xit 'no alices' do 25 | names = ["Chuck", "Charlene", "Cory", "Chris", "Carl"] 26 | # Your code goes here 27 | expect(has_alice).to eq(false) 28 | end 29 | 30 | xit 'has a multi word phrase' do 31 | phrases = ["Sure!", "OK.", "I have no idea.", "Really?Whatever."] 32 | # Your code goes here 33 | expect(multi_word_phrase).to eq(true) 34 | end 35 | 36 | xit 'no monkeys' do 37 | animals = ["elephant", "hippo", "jaguar", "python"] 38 | # Your code goes here 39 | expect(has_monkeys).to eq(false) 40 | end 41 | 42 | xit 'no multiples of five' do 43 | numbers = [3, 1, 3, 2, 4, 9, 8] 44 | # Your code goes here 45 | expect(multiples_of_5).to eq(false) 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/basic_enums_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'Basic Enums Test' do 2 | it 'solve for x' do 3 | s = '' 4 | 5.times do 5 | # write code here 6 | end 7 | expect(s).to eq('xxxxx') 8 | end 9 | 10 | xit 'simple sum' do 11 | sum = 0 12 | numbers = [1, 2, 3, 4, 5] 13 | # write code here 14 | expect(sum).to eq(15) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/count_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'Count Pattern Test' do 2 | 3 | it 'counts words with an e' do 4 | words = ["thing", "phone", "bark", "belt", "shoe", "bath"] 5 | tally = 0 6 | words.each do |word| 7 | tally += 1 if word.include?('e') 8 | end 9 | expect(tally).to eq(3) 10 | end 11 | 12 | xit 'counts numbers greater than 17' do 13 | numbers = [9, 18, 12, 17, 1, 3, 99] 14 | tally = 0 15 | numbers.each do |number| 16 | # Your code goes here 17 | end 18 | expect(tally).to eq(2) 19 | end 20 | 21 | xit 'words that are uppercase' do 22 | words = ["trousers", "SOCKS", "sweater", "Cap", "SHOE", "TIE"] 23 | tally = 0 24 | # Your code goes here 25 | expect(tally).to eq(3) 26 | end 27 | 28 | xit 'words ending in ing' do 29 | words = ["thought", "brake", "shin", "juice", "trash"] 30 | # Your code goes here 31 | expect(tally).to eq(0) 32 | end 33 | 34 | xit 'even numbers' do 35 | numbers = [9, 2, 1, 3, 18, 39, 71, 4, 6] 36 | # Your code goes here 37 | expect(tally).to eq(4) 38 | end 39 | 40 | xit 'multiples of 5' do 41 | numbers = [2, 5, 19, 25, 35, 67] 42 | # Your code goes here 43 | expect(tally).to eq(3) 44 | end 45 | 46 | xit 'round prices' do 47 | prices = [1.0, 3.9, 5.99, 18.5, 20.0] 48 | # Your code goes here 49 | expect(tally).to eq(2) 50 | end 51 | 52 | xit 'four letter words' do 53 | words = ["bake", "bark", "corn", "apple", "wart", "bird", "umbrella", "fart"] 54 | # Your code goes here 55 | expect(tally).to eq(6) 56 | end 57 | 58 | end 59 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/count_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'count test' do 2 | 3 | it 'words with e' do 4 | words = ["thing", "phone", "bark", "belt", "shoe", "bath"] 5 | tally = words.count do |word| 6 | word.include?('e') 7 | end 8 | expect(tally).to eq(3) 9 | end 10 | 11 | xit 'numbers greater than 17' do 12 | numbers = [9, 18, 12, 17, 1, 3, 99] 13 | tally = numbers.count do |number| 14 | # Your code goes here 15 | end 16 | expect(tally).to eq(2) 17 | end 18 | 19 | xit 'words that are uppercase' do 20 | words = ["trousers", "SOCKS", "sweater", "Cap", "SHOE", "TIE"] 21 | # Your code goes here 22 | expect(tally).to eq(3) 23 | end 24 | 25 | xit 'words ending in ing' do 26 | words = ["thought", "brake", "shin", "juice", "trash"] 27 | # Your code goes here 28 | expect(tally).to eq(0) 29 | end 30 | 31 | xit 'even numbers' do 32 | numbers = [9, 2, 1, 3, 18, 39, 71, 4, 6] 33 | # Your code goes here 34 | expect(tally).to eq(4) 35 | end 36 | 37 | xit 'multiples of 5' do 38 | numbers = [2, 5, 19, 25, 35, 67] 39 | # Your code goes here 40 | expect(tally).to eq(3) 41 | end 42 | 43 | xit 'round prices' do 44 | prices = [1.0, 3.9, 5.99, 18.5, 20.0] 45 | # Your code goes here 46 | expect(tally).to eq(2) 47 | end 48 | 49 | xit 'four letter words' do 50 | words = ["bake", "bark", "corn", "apple", "wart", "bird", "umbrella", "fart"] 51 | # Your code goes here 52 | expect(tally).to eq(6) 53 | end 54 | end 55 | 56 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/find_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'find pattern' do 2 | 3 | it 'finds first seven letter word' do 4 | words = ["capricious", "berry", "unicorn", "bag", "apple", "festering", "pretzel", "pencil"] 5 | found = nil 6 | words.each do |word| 7 | if word.length == 7 8 | found = word 9 | break 10 | end 11 | end 12 | expect(found).to eq("unicorn") 13 | end 14 | 15 | xit 'no waldo' do 16 | words = ["scarf", "sandcastle", "flag", "pretzel", "crow", "key"] 17 | found = nil 18 | words.each do |word| 19 | # Your code goes here 20 | end 21 | expect(found).to eq(nil) 22 | end 23 | 24 | xit 'found waldo' do 25 | words = ["noise", "dog", "fair", "house", "waldo", "bucket", "fish"] 26 | found = nil 27 | # Your code goes here 28 | expect(found).to eq("waldo") 29 | end 30 | 31 | xit 'no three letter words' do 32 | words = ["piglet", "porridge", "bear", "blueberry"] 33 | # Your code goes here 34 | expect(found).to eq(nil) 35 | end 36 | 37 | xit 'finds 13' do 38 | numbers = [2, 13, 19, 8, 3, 27] 39 | # Your code goes here 40 | expect(found).to eq(13) 41 | end 42 | 43 | xit 'first even number' do 44 | numbers = [3, 7, 13, 11, 10, 2, 17] 45 | # Your code goes here 46 | expect(found).to eq(10) 47 | end 48 | 49 | xit 'first multiple of 3' do 50 | numbers = [2, 8, 9, 27, 24, 5] 51 | # Your code goes here 52 | expect(found).to eq(9) 53 | end 54 | 55 | xit 'first word starting with q' do 56 | words = ["weirdo", "quill", "fast", "quaint", "quitter", "koala"] 57 | # Your code goes here 58 | expect(found).to eq("quill") 59 | end 60 | 61 | xit 'first word ending with er' do 62 | words = ["biggest", "pour", "blight", "finger", "pie", "border"] 63 | # Your code goes here 64 | expect(found).to eq("finger") 65 | end 66 | 67 | xit 'first number greater than 20' do 68 | numbers = [1, 8, 19, 21, 29, 31, 34] 69 | # Your code goes here 70 | expect(found).to eq(21) 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/find_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'find test' do 2 | 3 | it 'first seven letter word' do 4 | words = ["capricious", "berry", "unicorn", "bag", "apple", "festering", "pretzel", "pencil"] 5 | found = words.find do |word| 6 | word.length == 7 7 | end 8 | expect(found).to eq("unicorn") 9 | end 10 | 11 | xit 'no waldo' do 12 | words = ["scarf", "sandcastle", "flag", "pretzel", "crow", "key"] 13 | found = words.find do |word| 14 | # Your code goes here 15 | end 16 | expect(found).to eq(nil) 17 | end 18 | 19 | xit 'found waldo' do 20 | words = ["noise", "dog", "fair", "house", "waldo", "bucket", "fish"] 21 | # Your code goes here 22 | expect(found).to eq("waldo") 23 | end 24 | 25 | xit 'no three letter words' do 26 | words = ["piglet", "porridge", "bear", "blueberry"] 27 | # Your code goes here 28 | expect(found).to eq(nil) 29 | end 30 | 31 | xit 'find 13' do 32 | numbers = [2, 13, 19, 8, 3, 27] 33 | # Your code goes here 34 | expect(found).to eq(13) 35 | end 36 | 37 | xit 'find first even number' do 38 | numbers = [3, 7, 13, 11, 10, 2, 17] 39 | # Your code goes here 40 | expect(found).to eq(10) 41 | end 42 | 43 | xit 'first multiple of 3' do 44 | numbers = [2, 8, 9, 27, 24, 5] 45 | # Your code goes here 46 | expect(found).to eq(9) 47 | end 48 | 49 | xit 'first word starting with q' do 50 | words = ["weirdo", "quill", "fast", "quaint", "quitter", "koala"] 51 | # Your code goes here 52 | expect(found).to eq("quill") 53 | end 54 | 55 | xit 'first word ending with er' do 56 | words = ["biggest", "pour", "blight", "finger", "pie", "border"] 57 | # Your code goes here 58 | expect(found).to eq("finger") 59 | end 60 | 61 | xit 'first number greater than 20' do 62 | numbers = [1, 8, 19, 21, 29, 31, 34] 63 | # Your code goes here 64 | expect(found).to eq(21) 65 | end 66 | end 67 | 68 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/find_using_max_by_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'max_by' do 2 | # You get the first test for free... it's already passing. 3 | it 'longest word' do 4 | words = ["apple", "banana", "cherry", "date", "eggplant"] 5 | 6 | found_word = words.max_by do |word| 7 | word.length 8 | end 9 | 10 | expect(found_word).to eq("eggplant") 11 | end 12 | 13 | # This one is missing the block inside the loop. 14 | xit 'shortest word' do 15 | words = ["apple", "banana", "cherry", "date", "eggplant"] 16 | 17 | found_word = words.max_by do |word| 18 | # write code here 19 | end 20 | 21 | expect(found_word).to eq("date") 22 | end 23 | 24 | # This one is missing the entire loop 25 | xit 'array with the most items' do 26 | arrays = [[:a, :b, :c], [1, 2, 3, 4, 5], ["zoo", :things, :stuff]] 27 | 28 | # write code here 29 | 30 | expect(biggest_array).to eq([1,2,3,4,5]) 31 | end 32 | 33 | # You're on your own on this one. 34 | xit 'array with fewest items' do 35 | arrays = [[:a, :b, :c], [1, 2, 3, 4, 5], ["zoo", :things, :stuff]] 36 | 37 | # write code here 38 | 39 | expect(smallest_array).to eq([:a, :b, :c]) 40 | end 41 | 42 | xit 'biggest number' do 43 | numbers = [1, 10, 100, 1000, 10000, 1000000] 44 | 45 | # write code here 46 | 47 | expect(found).to eq(1000000) 48 | end 49 | 50 | xit 'smallest number' do 51 | numbers = [1, 10, 100, 1000, 10000, 1000000] 52 | 53 | # write code here 54 | 55 | expect(found).to eq(1) 56 | end 57 | 58 | xit 'most programmers' do 59 | programmers = {ruby: ["katrina", "sandi", "jim", "aaron", "desi"], java: ["abby", "jon", "susan"]} 60 | 61 | # write code here 62 | 63 | expect(most_programmers.first).to eq(:ruby) 64 | end 65 | 66 | xit 'fewest programmers' do 67 | programmers = {ruby: ["katrina", "sandi", "jim", "aaron", "desi"], java: ["abby", "jon", "susan"]} 68 | 69 | # write code here 70 | 71 | expect(fewest_programmers.first).to eq(:java) 72 | end 73 | end 74 | 75 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/group_by_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'group by pattern' do 2 | 3 | it 'group words by length' do 4 | words = ["sue", "alice", "steve", "sally", "adam", "fort", "tops", "dog", "cat"] 5 | grouped = Hash.new {|hash, key| hash[key] = []} 6 | words.each do |word| 7 | grouped[word.length] << word 8 | end 9 | expected = {3=>["sue", "dog", "cat"], 4=>["adam", "fort", "tops"], 5=>["alice", "steve", "sally"]} 10 | expect(grouped).to eq(expected) 11 | end 12 | 13 | xit 'groups by odds and evens' do 14 | numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 15 | odd_and_even = Hash.new {|hash, key| hash[key] = []} 16 | numbers.each do |number| 17 | # Your code goes here 18 | end 19 | expected = {1=>[1, 1, 3, 5, 13, 21, 55], 0=>[2, 8, 34]} 20 | expect(odd_and_even).to eq(expected) 21 | end 22 | 23 | xit 'groups by first letter' do 24 | words = ["ant", "axis", "albatross", "bolt", "badge", "butter", "car", "cdr", "column"] 25 | words_by_first_letter = Hash.new {|hash, key| hash[key] = []} 26 | # Your code goes here 27 | expected = {"a"=>["ant", "axis", "albatross"], "b"=>["bolt", "badge", "butter"], "c"=>["car", "cdr", "column"]} 28 | expect(words_by_first_letter).to eq(expected) 29 | end 30 | 31 | xit 'group by uniqueness' do 32 | words = ["one", "two", "one", "TWO", "three", "one", "three", "three", "three"] 33 | # Your code goes here 34 | expected = {"one"=>["one", "one", "one"], "two"=>["two", "TWO"], "three"=>["three", "three", "three", "three"]} 35 | expect(grouped).to eq(expected) 36 | end 37 | 38 | xit 'group by number of zeroes' do 39 | numbers = [1, 3, 500, 200, 4000, 3000, 10000, 90, 20, 500000] 40 | # Your code goes here 41 | expected = {0=>[1, 3], 2=>[500, 200], 3=>[4000, 3000], 4=>[10000], 1=>[90, 20], 5=>[500000]} 42 | expect(grouped).to eq(expected) 43 | end 44 | 45 | xit 'group by order of magnitude' do 46 | numbers = [1, 3, 503, 239, 4938, 3932, 19982, 93, 21, 501787] 47 | # Your code goes here 48 | expected = {1=>[1, 3], 2=>[93, 21], 3=>[503, 239], 4=>[4938, 3932], 5=>[19982], 6=>[501787]} 49 | expect(grouped).to eq(expected) 50 | end 51 | end 52 | 53 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/group_by_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'group by' do 2 | it 'groups words by length' do 3 | words = ["sue", "alice", "steve", "sally", "adam", "fort", "tops", "dog", "cat"] 4 | grouped = words.group_by do |word| 5 | word.length 6 | end 7 | expected = {3=>["sue", "dog", "cat"], 4=>["adam", "fort", "tops"], 5=>["alice", "steve", "sally"]} 8 | expect(grouped).to eq(expected) 9 | end 10 | 11 | xit 'group by odd and even' do 12 | numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 13 | odd_and_even = numbers.group_by do |number| 14 | # Your code goes here 15 | end 16 | expected = {1=>[1, 1, 3, 5, 13, 21, 55], 0=>[2, 8, 34]} 17 | expect(odd_and_even).to eq(expected) 18 | end 19 | 20 | xit 'group by first letter' do 21 | words = ["ant", "axis", "albatross", "bolt", "badge", "butter", "car", "cdr", "column"] 22 | # Your code goes here 23 | expected = {"a"=>["ant", "axis", "albatross"], "b"=>["bolt", "badge", "butter"], "c"=>["car", "cdr", "column"]} 24 | expect(words_by_first_letter).to eq(expected) 25 | end 26 | 27 | xit 'group by uniqueness' do 28 | words = ["one", "two", "one", "TWO", "three", "one", "three", "three", "three"] 29 | # Your code goes here 30 | expected = {"one"=>["one", "one", "one"], "two"=>["two", "TWO"], "three"=>["three", "three", "three", "three"]} 31 | expect(grouped).to eq(expected) 32 | end 33 | 34 | xit 'grouped by number of zeroes' do 35 | numbers = [1, 3, 500, 200, 4000, 3000, 10000, 90, 20, 500000] 36 | # Your code goes here 37 | expected = {0=>[1, 3], 2=>[500, 200], 3=>[4000, 3000], 4=>[10000], 1=>[90, 20], 5=>[500000]} 38 | expect(grouped).to eq(expected) 39 | end 40 | 41 | xit 'grouped by order of magnitude' do 42 | numbers = [1, 3, 503, 239, 4938, 3932, 19982, 93, 21, 501787] 43 | # Your code goes here 44 | expected = {1=>[1, 3], 2=>[93, 21], 3=>[503, 239], 4=>[4938, 3932], 5=>[19982], 6=>[501787]} 45 | expect(grouped).to eq(expected) 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/map_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'map pattern' do 2 | 3 | it 'capitalizes' do 4 | names = ["alice", "bob", "charlie"] 5 | capitalized_names = [] 6 | names.each do |name| 7 | capitalized_names << name.capitalize 8 | end 9 | expect(capitalized_names).to eq(["Alice", "Bob", "Charlie"]) 10 | end 11 | 12 | xit 'doubles' do 13 | numbers = [1, 2, 3, 4, 5] 14 | doubles = [] 15 | numbers.each do |number| 16 | # Your code goes here 17 | end 18 | expect(doubles).to eq([2, 4, 6, 8, 10]) 19 | end 20 | 21 | xit 'squares' do 22 | numbers = [1, 2, 3, 4, 5] 23 | squares = [] 24 | # Your code goes here 25 | expect(squares).to eq([1, 4, 9, 16, 25]) 26 | end 27 | 28 | xit 'lengths' do 29 | names = ["alice", "bob", "charlie", "david", "eve"] 30 | # Your code goes here 31 | expect(lengths).to eq([5, 3, 7, 5, 3]) 32 | end 33 | 34 | xit 'normalize zip codes' do 35 | numbers = [234, 10, 9119, 38881] 36 | # Your code goes here 37 | expect(zip_code).to eq(["00234", "00010", "09119", "38881"]) 38 | end 39 | 40 | xit 'backwards' do 41 | names = ["alice", "bob", "charlie", "david", "eve"] 42 | # Your code goes here 43 | expect(backwards).to eq(["ecila", "bob", "eilrahc", "divad", "eve"]) 44 | end 45 | 46 | xit 'words with no vowels' do 47 | words = ["green", "sheep", "travel", "least", "boat"] 48 | # Your code goes here 49 | expect(without_vowels).to eq(["grn", "shp", "trvl", "lst", "bt"]) 50 | end 51 | 52 | xit 'trims last letter' do 53 | animals = ["dog", "cat", "mouse", "frog", "platypus"] 54 | # Your code goes here 55 | expect(trimmed).to eq(["do", "ca", "mous", "fro", "platypu"]) 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/map_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'map' do 2 | 3 | it 'capitalizes' do 4 | names = ["alice", "bob", "charlie"] 5 | capitalized_names = names.map do |name| 6 | name.capitalize 7 | end 8 | expect(capitalized_names).to eq(["Alice", "Bob", "Charlie"]) 9 | end 10 | 11 | xit 'doubles' do 12 | numbers = [1, 2, 3, 4, 5] 13 | doubles = numbers.map do |number| 14 | # Your code goes here 15 | end 16 | expect(doubles).to eq([2, 4, 6, 8, 10]) 17 | end 18 | 19 | xit 'squares' do 20 | numbers = [1, 2, 3, 4, 5] 21 | # Your code goes here 22 | expect(squares).to eq([1, 4, 9, 16, 25]) 23 | end 24 | 25 | xit 'lengths' do 26 | names = ["alice", "bob", "charlie", "david", "eve"] 27 | # Your code goes here 28 | expect(lengths).to eq([5, 3, 7, 5, 3]) 29 | end 30 | 31 | xit 'normalize zip codes' do 32 | numbers = [234, 10, 9119, 38881] 33 | # Your code goes here 34 | expect(zip_codes).to eq(["00234", "00010", "09119", "38881"]) 35 | end 36 | 37 | xit 'backwards' do 38 | names = ["alice", "bob", "charlie", "david", "eve"] 39 | # Your code goes here 40 | expect(backwards).to eq(["ecila", "bob", "eilrahc", "divad", "eve"]) 41 | end 42 | 43 | xit 'words with no vowels' do 44 | words = ["green", "sheep", "travel", "least", "boat"] 45 | # Your code goes here 46 | expect(without_vowels).to eq(["grn", "shp", "trvl", "lst", "bt"]) 47 | end 48 | 49 | xit 'trims last letter' do 50 | animals = ["dog", "cat", "mouse", "frog", "platypus"] 51 | # Your code goes here 52 | expect(trimmed).to eq(["do", "ca", "mous", "fro", "platypu"]) 53 | end 54 | end 55 | 56 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/none_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'none pattern' do 2 | 3 | it 'none are broken' do 4 | things = ["functional", "working", "works", "fixed", "good"] 5 | none_broken = true 6 | things.each do |thing| 7 | none_broken = false if thing == "broken" 8 | end 9 | expect(none_broken).to eq(true) 10 | end 11 | 12 | xit 'double negative' do 13 | numbers = [9, 3, 3, 7, 6, -5, 1] 14 | not_none_negative = true 15 | numbers.each do |number| 16 | # Your code goes here 17 | end 18 | expect(not_none_negative).to eq(false) 19 | end 20 | 21 | xit 'none are negative' do 22 | numbers = [9, 3, 1, 8, 3, 3, 5] 23 | none_negative = true 24 | # Your code goes here 25 | expect(none_negative).to eq(true) 26 | end 27 | 28 | xit 'none shall pass' do 29 | critters = ["elf", "hobbit", "dwarf", "wizard", "human"] 30 | # Your code goes here 31 | expect(none_shall_pass).to eq(true) 32 | end 33 | 34 | xit 'one or more shall pass' do 35 | phrases = ["go", "left", "can cross", "shall pass", "must stay", "tarried"] 36 | # Your code goes here 37 | expect(none_shall_pass).to eq(false) 38 | end 39 | 40 | xit 'none even' do 41 | numbers = [3, 9, 15, 21, 19] 42 | # Your code goes here 43 | expect(none_even).to eq(true) 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/none_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'none' do 2 | 3 | it 'none are broken' do 4 | things = ["functional", "working", "works", "fixed", "good"] 5 | none_broken = things.none? do |thing| 6 | thing == "broken" 7 | end 8 | expect(none_broken).to eq(true) 9 | end 10 | 11 | xit 'double negative' do 12 | numbers = [9, 3, 3, 7, 6, -5, 1] 13 | not_none_negative = numbers.none? do |number| 14 | # Your code goes here 15 | end 16 | expect(not_none_negative).to eq(false) 17 | end 18 | 19 | xit 'none are negative' do 20 | numbers = [9, 3, 1, 8, 3, 3, 5] 21 | # Your code goes here 22 | expect(none_negative).to eq(true) 23 | end 24 | 25 | xit 'none shall pass' do 26 | critters = ["elf", "hobbit", "dwarf", "wizard", "human"] 27 | # Your code goes here 28 | expect(none_shall_pass).to eq(true) 29 | end 30 | 31 | xit 'one or more shall pass' do 32 | phrases = ["go", "left", "can cross", "shall pass", "must stay", "tarried"] 33 | # Your code goes here 34 | expect(none_shall_pass).to eq(false) 35 | end 36 | 37 | xit 'none even' do 38 | numbers = [3, 9, 15, 21, 19] 39 | # Your code goes here 40 | expect(none_even).to eq(true) 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/one_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'one pattern' do 2 | 3 | it 'one more' do 4 | words = ["bigger", "better", "more", "improved", "advantageous"] 5 | count = 0 6 | words.each do |word| 7 | count += 1 if word == 'more' 8 | end 9 | exactly_one = count == 1 10 | expect(exactly_one).to eq(true) 11 | end 12 | 13 | xit 'not even one ring' do 14 | ornaments = ["bracelet", "anklet", "earring"] 15 | count = 0 16 | ornaments.each do |ornament| 17 | # Your code goes here 18 | end 19 | exactly_one_ring = count == 1 20 | expect(exactly_one_ring).to eq(false) 21 | end 22 | 23 | xit 'not just one ring' do 24 | ornaments = ["bracelet", "ring", "ring", "anklet", "earring"] 25 | count = 0 26 | # Your code goes here 27 | exactly_one_ring = count == 1 28 | expect(exactly_one_ring).to eq(false) 29 | end 30 | 31 | xit 'one time' do 32 | words = ["morning", "time", "evening", "noon", "dusk", "dawn"] 33 | # Your code goes here 34 | exactly_one_time = count == 1 35 | expect(exactly_one_time).to eq(true) 36 | end 37 | 38 | xit 'one double digit number' do 39 | numbers = [8, 2, 10, 333, 9, 101] 40 | # Your code goes here 41 | exactly_one_double_digit = count == 1 42 | expect(exactly_one_double_digit).to eq(true) 43 | end 44 | 45 | xit 'not one even number' do 46 | numbers = [3, 20, 81, 10, 391, 32] 47 | # Your code goes here 48 | exactly_one_even_number = count == 1 49 | expect(exactly_one_even_number).to eq(false) 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/one_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'one' do 2 | 3 | it 'one more' do 4 | words = ["bigger", "better", "more", "improved", "advantageous"] 5 | exactly_one = words.one? do |word| 6 | word == 'more' 7 | end 8 | expect(exactly_one).to eq(true) 9 | end 10 | 11 | xit 'not even one ring' do 12 | ornaments = ["bracelet", "anklet", "earring"] 13 | exactly_one_ring = ornaments.one? do |ornament| 14 | # Your code goes here 15 | end 16 | expect(exactly_one_ring).to eq(false) 17 | end 18 | 19 | xit 'not just one ring' do 20 | ornaments = ["bracelet", "ring", "ring", "anklet", "earring"] 21 | # Your code goes here 22 | expect(exactly_one_ring).to eq(false) 23 | end 24 | 25 | xit 'one time' do 26 | words = ["morning", "time", "evening", "noon", "dusk", "dawn"] 27 | # Your code goes here 28 | expect(exactly_one_time).to eq(true) 29 | end 30 | 31 | xit 'one double digit number' do 32 | numbers = [8, 2, 10, 333, 9, 101] 33 | # Your code goes here 34 | expect(exactly_one_double_digit).to eq(true) 35 | end 36 | 37 | xit 'not even one number' do 38 | numbers = [3, 20, 81, 10, 391, 32] 39 | # Your code goes here 40 | expect(exactly_one_even_number).to eq(false) 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/reduce_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'reduce pattern' do 2 | it 'sums a list of numbers' do 3 | numbers = [32, 1, 21, 5, 81, 333] 4 | sum = 0 5 | numbers.each do |number| 6 | sum = sum + number 7 | end 8 | expect(sum).to eq(473) 9 | end 10 | 11 | xit 'subtracts a list of numbers' do 12 | numbers = [28, 12, 38, 1, 91] 13 | difference = 0 14 | numbers.each do |number| 15 | # Your code goes here 16 | end 17 | expect(difference).to eq(-170) 18 | end 19 | 20 | xit 'multiplies a list of numbers' do 21 | numbers = [2, 3, 5, 7] 22 | product = 1 23 | # Your code goes here 24 | expect(product).to eq(210) 25 | end 26 | 27 | xit 'capitalizes key words in phrase' do 28 | keywords = ["fish", "blue"] 29 | phrase = 'one fish two fish red fish blue fish' 30 | # Your code goes here 31 | expect(phrase).to eq('one FISH two FISH red FISH BLUE FISH') 32 | end 33 | 34 | xit 'divide 560 by a bunch of numbers' do 35 | numbers = [2, 2, 2, 5, 7] 36 | quotient = 560 37 | # Your code goes here 38 | expect(quotient).to eq(2) 39 | end 40 | 41 | xit 'subtracts smallest numbers from 100' do 42 | elements = [[8, 5, 3], [1, 9, 11], [4, 7, 2], [19, 34, 6]] 43 | difference = 100 44 | # Your code goes here 45 | expect(difference).to eq(88) 46 | end 47 | 48 | xit 'adds all second values together' do 49 | elements = [["a", 1], ["b", 9], ["c", 21]] 50 | sum = 0 51 | # Your code goes here 52 | expect(sum).to eq(31) 53 | end 54 | 55 | end 56 | 57 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/reduce_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'reduce' do 2 | 3 | it 'sums a list of numbers' do 4 | numbers = [32, 1, 21, 5, 81, 333] 5 | result = numbers.reduce(0) do |sum, number| 6 | sum + number 7 | end 8 | expect(result).to eq(473) 9 | end 10 | 11 | xit 'subtracts a list of numbers' do 12 | numbers = [28, 12, 38, 1, 91] 13 | result = numbers.reduce(0) do |difference, number| 14 | # Your code goes here 15 | end 16 | expect(result).to eq(-170) 17 | end 18 | 19 | xit 'multiplies a list of numbers' do 20 | numbers = [2, 3, 5, 7] 21 | # initial value is 1 22 | # Your code goes here 23 | expect(result).to eq(210) 24 | end 25 | 26 | xit 'capitalize key words in phrase' do 27 | keywords = ["fish", "blue"] 28 | # initial value is 'one fish two fish red fish blue fish' 29 | # Your code goes here 30 | expect(result).to eq('one FISH two FISH red FISH BLUE FISH') 31 | end 32 | 33 | xit 'divides 560 by a bunch of numbers' do 34 | numbers = [2, 2, 2, 5, 7] 35 | # initial value is 560 36 | # Your code goes here 37 | expect(result).to eq(2) 38 | end 39 | 40 | xit 'subtract smallest values from 100' do 41 | elements = [[8, 5, 3], [1, 9, 11], [4, 7, 2], [19, 34, 6]] 42 | # initial value is 100 43 | # Your code goes here 44 | expect(result).to eq(88) 45 | end 46 | 47 | xit 'adds all second values together' do 48 | elements = [["a", 1], ["b", 9], ["c", 21]] 49 | # initial value is 0 50 | # Your code goes here 51 | expect(result).to eq(31) 52 | end 53 | end 54 | 55 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/reject_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'reject pattern' do 2 | 3 | it 'removes zeroes' do 4 | numbers = [2, 93, 7, 0, 0, 1, 0, 31, 0, 368] 5 | filtered = [] 6 | numbers.each do |number| 7 | filtered << number unless number.zero? 8 | end 9 | expect(filtered).to eq([2, 93, 7, 1, 31, 368]) 10 | end 11 | 12 | xit 'removes vowels' do 13 | letters = ["a", "l", "l", " ", "y", "o", "u", "r", " ", "b", "a", "s", "e", " ", "a", "r", "e", " ", "b", "e", "l", "o", "n", "g", " ", "t", "o", " ", "u", "s"] 14 | remaining = [] 15 | letters.each do |letter| 16 | # Your code goes here 17 | end 18 | expect(remaining).to eq(["l", "l", " ", "r", " ", "b", "s", " ", "r", " ", "b", "l", "n", "g", " ", "t", " ", "s"]) 19 | end 20 | 21 | xit 'removes numbers divisible by 3' do 22 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 23 | remaining = [] 24 | # Your code goes here 25 | expect(remaining).to eq([1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20]) 26 | end 27 | 28 | xit 'removes words longer than 3 letters' do 29 | skip 30 | words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] 31 | # Your code goes here 32 | expect(selected).to eq(["bad", "cat", "dog", "red"]) 33 | end 34 | 35 | xit 'removes words ending in e' do 36 | words = ["are", "you", "strike", "thinking", "belt", "piece", "warble", "sing", "pipe"] 37 | # Your code goes here 38 | expect(selected).to eq(["you", "thinking", "belt", "sing"]) 39 | end 40 | 41 | xit 'removes words ending in ing' do 42 | words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"] 43 | # Your code goes here 44 | expect(selected).to eq(["finger", "drought", "bingo", "purposeful"]) 45 | end 46 | 47 | xit 'removes words containing e' do 48 | words = ["four", "red", "five", "blue", "pizza", "purple"] 49 | # Your code goes here 50 | expect(selected).to eq(["four", "pizza"]) 51 | end 52 | 53 | xit 'removes dinosaurs' do 54 | animals = ["tyrannosaurus", "narwhal", "eel", "achillesaurus", "qingxiusaurus"] 55 | # Your code goes here 56 | expect(notasaurus).to eq(["narwhal", "eel"]) 57 | end 58 | 59 | xit 'removes numbers' do 60 | elements = ["cat", "dog", 23, 81.1, 56, "aimless", 43] 61 | # Your code goes here 62 | expect(not_numbers).to eq(["cat", "dog", "aimless"]) 63 | end 64 | 65 | xit 'removes floats' do 66 | elements = ["cat", "dog", 32.333, 23, 56, "aimless", 43.2] 67 | # Your code goes here 68 | expect(not_numbers).to eq(["cat", "dog", 23, 56, "aimless"]) 69 | end 70 | 71 | xit 'removes animals starting with vowels' do 72 | animals = ["aardvark", "bonobo", "cat", "dog", "elephant"] 73 | # Your code goes here 74 | expect(remaining).to eq(["bonobo", "cat", "dog"]) 75 | end 76 | 77 | xit 'removes upcased words' do 78 | words = ["CAT", "dog", "AIMLESS", "Trevor", "butter"] 79 | # Your code goes here 80 | expect(remaining).to eq(["dog", "Trevor", "butter"]) 81 | end 82 | 83 | xit 'removes arrays' do 84 | elements = ["CAT", ["dog"], 23, [56, 3, 8], "AIMLESS", 43, "butter"] 85 | # Your code goes here 86 | expect(remaining).to eq(["CAT", 23, "AIMLESS", 43, "butter"]) 87 | end 88 | 89 | xit 'removes hashes' do 90 | elements = ["cat", {:dog=>"fido"}, 23, {:stuff=>"things"}, "aimless", 43] 91 | # Your code goes here 92 | expect(remaining).to eq(["cat", 23, "aimless", 43]) 93 | end 94 | end 95 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/reject_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'reject' do 2 | 3 | it 'removes zeroes' do 4 | numbers = [2, 93, 7, 0, 0, 1, 0, 31, 0, 368] 5 | filtered = numbers.reject do |number| 6 | number.zero? 7 | end 8 | expect(filtered).to eq([2, 93, 7, 1, 31, 368]) 9 | end 10 | 11 | xit 'removes vowels' do 12 | letters = ["a", "l", "l", " ", "y", "o", "u", "r", " ", "b", "a", "s", "e", " ", "a", "r", "e", " ", "b", "e", "l", "o", "n", "g", " ", "t", "o", " ", "u", "s"] 13 | remaining = letters.reject do |letter| 14 | # Your code goes here 15 | end 16 | expect(remaining).to eq(["l", "l", " ", "r", " ", "b", "s", " ", "r", " ", "b", "l", "n", "g", " ", "t", " ", "s"]) 17 | end 18 | 19 | xit 'remove numbers divisible by 3' do 20 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 21 | # Your code goes here 22 | expect(remaining).to eq([1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20]) 23 | end 24 | 25 | xit 'remove words longer tghan three letters' do 26 | words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] 27 | # Your code goes here 28 | expect(selected).to eq(["bad", "cat", "dog", "red"]) 29 | end 30 | 31 | xit 'remove words ending in e' do 32 | words = ["are", "you", "strike", "thinking", "belt", "piece", "warble", "sing", "pipe"] 33 | # Your code goes here 34 | expect(selected).to eq(["you", "thinking", "belt", "sing"]) 35 | end 36 | 37 | xit 'remove words ending in ing' do 38 | words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"] 39 | # Your code goes here 40 | expect(selected).to eq(["finger", "drought", "bingo", "purposeful"]) 41 | end 42 | 43 | xit 'remove words containing e' do 44 | words = ["four", "red", "five", "blue", "pizza", "purple"] 45 | # Your code goes here 46 | expect(selected).to eq(["four", "pizza"]) 47 | end 48 | 49 | xit 'remove dinosaurs' do 50 | animals = ["tyrannosaurus", "narwhal", "eel", "achillesaurus", "qingxiusaurus"] 51 | # Your code goes here 52 | expect(notasaurus).to eq(["narwhal", "eel"]) 53 | end 54 | 55 | xit 'remove numbers' do 56 | elements = ["cat", "dog", 23, 81.1, 56, "aimless", 43] 57 | # Your code goes here 58 | expect(not_numbers).to eq(["cat", "dog", "aimless"]) 59 | end 60 | 61 | xit 'remove floats' do 62 | elements = ["cat", "dog", 32.333, 23, 56, "aimless", 43.2] 63 | # Your code goes here 64 | expect(not_numbers).to eq(["cat", "dog", 23, 56, "aimless"]) 65 | end 66 | 67 | xit 'remove animals starting with a vowel' do 68 | animals = ["aardvark", "bonobo", "cat", "dog", "elephant"] 69 | # Your code goes here 70 | expect(remaining).to eq(["bonobo", "cat", "dog"]) 71 | end 72 | 73 | xit 'remove upcased words' do 74 | words = ["CAT", "dog", "AIMLESS", "Trevor", "butter"] 75 | # Your code goes here 76 | expect(remaining).to eq(["dog", "Trevor", "butter"]) 77 | end 78 | 79 | xit 'remove arrays' do 80 | elements = ["CAT", ["dog"], 23, [56, 3, 8], "AIMLESS", 43, "butter"] 81 | # Your code goes here 82 | expect(remaining).to eq(["CAT", 23, "AIMLESS", 43, "butter"]) 83 | end 84 | 85 | xit 'remove hashes' do 86 | elements = ["cat", {:dog=>"fido"}, 23, {:stuff=>"things"}, "aimless", 43] 87 | # Your code goes here 88 | expect(remaining).to eq(["cat", 23, "aimless", 43]) 89 | end 90 | end 91 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/select_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'select pattern' do 2 | 3 | it 'picks even numbers' do 4 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 5 | evens = [] 6 | numbers.each do |number| 7 | evens << number if number.even? 8 | end 9 | expect(evens).to eq([2, 4, 6, 8, 10]) 10 | end 11 | 12 | xit 'picks odd numbers' do 13 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 14 | odds = [] 15 | numbers.each do |number| 16 | # Your code goes here 17 | end 18 | expect(odds).to eq([1, 3, 5, 7, 9]) 19 | end 20 | 21 | xit 'words with three letters' do 22 | words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] 23 | selected = [] 24 | # Your code goes here 25 | expect(selected).to eq(["bad", "cat", "dog", "red"]) 26 | end 27 | 28 | xit 'words with more than three letters' do 29 | words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] 30 | # Your code goes here 31 | expect(selected).to eq(["pill", "finger", "blue", "table"]) 32 | end 33 | 34 | xit 'words ending in e' do 35 | words = ["are", "you", "strike", "thinking", "belt", "piece", "warble", "sing", "pipe"] 36 | # Your code goes here 37 | expect(selected).to eq(["are", "strike", "piece", "warble", "pipe"]) 38 | end 39 | 40 | xit 'words ending in ing' do 41 | words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"] 42 | # Your code goes here 43 | expect(selected).to eq(["bring", "singing"]) 44 | end 45 | 46 | xit 'words containing e' do 47 | words = ["four", "red", "five", "blue", "pizza", "purple"] 48 | # Your code goes here 49 | expect(selected).to eq(["red", "five", "blue", "purple"]) 50 | end 51 | 52 | xit 'dinosaurs' do 53 | animals = ["tyrannosaurus", "narwhal", "eel", "achillesaurus", "qingxiusaurus"] 54 | # Your code goes here 55 | expect(dinosaurs).to eq(["tyrannosaurus", "achillesaurus", "qingxiusaurus"]) 56 | end 57 | 58 | xit 'floats' do 59 | numbers = [3, 1.4, 3.5, 2, 4.9, 9.1, 8.0] 60 | # Your code goes here 61 | expect(floats).to eq([1.4, 3.5, 4.9, 9.1, 8.0]) 62 | end 63 | 64 | xit 'arrays' do 65 | elements = ["CAT", ["dog"], 23, [56, 3, 8], "AIMLESS", 43, "butter"] 66 | # Your code goes here 67 | expect(arrays).to eq([["dog"], [56, 3, 8]]) 68 | end 69 | 70 | xit 'hashes' do 71 | elements = ["cat", {:dog=>"fido"}, 23, {:stuff=>"things"}, "aimless", 43] 72 | # Your code goes here 73 | expect(hashes).to eq([{:dog=>"fido"}, {:stuff=>"things"}]) 74 | end 75 | end 76 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/select_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'select' do 2 | 3 | it 'even numbers' do 4 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 5 | evens = numbers.select do |number| 6 | number.even? 7 | end 8 | expect(evens).to eq([2, 4, 6, 8, 10]) 9 | end 10 | 11 | xit 'odd numbers' do 12 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 13 | odds = numbers.select do |number| 14 | # Your code goes here 15 | end 16 | expect(odds).to eq([1, 3, 5, 7, 9]) 17 | end 18 | 19 | xit 'words with three letters' do 20 | words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] 21 | # Your code goes here 22 | expect(selected).to eq(["bad", "cat", "dog", "red"]) 23 | end 24 | 25 | xit 'words with more than three letters' do 26 | words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] 27 | # Your code goes here 28 | expect(selected).to eq(["pill", "finger", "blue", "table"]) 29 | end 30 | 31 | xit 'wordss ending in e' do 32 | words = ["are", "you", "strike", "thinking", "belt", "piece", "warble", "sing", "pipe"] 33 | # Your code goes here 34 | expect(selected).to eq(["are", "strike", "piece", "warble", "pipe"]) 35 | end 36 | 37 | xit 'words ending in ing' do 38 | words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"] 39 | # Your code goes here 40 | expect(selected).to eq(["bring", "singing"]) 41 | end 42 | 43 | xit 'words containing e' do 44 | words = ["four", "red", "five", "blue", "pizza", "purple"] 45 | # Your code goes here 46 | expect(selected).to eq(["red", "five", "blue", "purple"]) 47 | end 48 | 49 | xit 'dinosaurs' do 50 | animals = ["tyrannosaurus", "narwhal", "eel", "achillesaurus", "qingxiusaurus"] 51 | # Your code goes here 52 | expect(dinosaurs).to eq(["tyrannosaurus", "achillesaurus", "qingxiusaurus"]) 53 | end 54 | 55 | xit 'floats' do 56 | numbers = [3, 1.4, 3.5, 2, 4.9, 9.1, 8.0] 57 | # Your code goes here 58 | expect(floats).to eq([1.4, 3.5, 4.9, 9.1, 8.0]) 59 | end 60 | 61 | xit 'arrays' do 62 | elements = ["CAT", ["dog"], 23, [56, 3, 8], "AIMLESS", 43, "butter"] 63 | # Your code goes here 64 | expect(arrays).to eq([["dog"], [56, 3, 8]]) 65 | end 66 | 67 | xit 'hashes' do 68 | elements = ["cat", {:dog=>"fido"}, 23, {:stuff=>"things"}, "aimless", 43] 69 | # Your code goes here 70 | expect(hashes).to eq([{:dog=>"fido"}, {:stuff=>"things"}]) 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/sort_by_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'sort_by pattern' do 2 | 3 | it 'sorts alphabetically' do 4 | words = ["broccoli", "Carrots", "FISH", "Bacon", "candy"] 5 | transformed = [] 6 | words.each do |word| 7 | transformed << [word.downcase, word] 8 | end 9 | transformed = transformed.sort 10 | sorted = [] 11 | transformed.each do |sort_key, word| 12 | sorted << word 13 | end 14 | expect(sorted).to eq(["Bacon", "broccoli", "candy", "Carrots", "FISH"]) 15 | end 16 | 17 | xit 'alphabetically by last letter' do 18 | things = ["pill", "box", "glass", "water", "sponge"] 19 | transformed = [] 20 | things.each do |thing| 21 | # Your code goes here 22 | end 23 | transformed = transformed.sort 24 | sorted = [] 25 | transformed.each do |sort_key, thing| 26 | sorted << thing 27 | end 28 | expect(sorted).to eq(["sponge", "pill", "water", "glass", "box"]) 29 | end 30 | 31 | xit 'sort by distance' do 32 | distances = ["1cm", "9cm", "30cm", "4cm", "2cm"] 33 | transformed = [] 34 | # Your code goes here 35 | transformed = transformed.sort 36 | sorted = [] 37 | transformed.each do |sort_key, distance| 38 | sorted << distance 39 | end 40 | expect(sorted).to eq(["1cm", "2cm", "4cm", "9cm", "30cm"]) 41 | end 42 | 43 | xit 'by length' do 44 | words = ["heteromorph", "ancyloceratina", "bioengineering", "mathematical", "bug"] 45 | # Your code goes here 46 | expect(sorted).to eq(["bug", "heteromorph", "mathematical", "ancyloceratina", "bioengineering"]) 47 | end 48 | 49 | xit 'by proximity to ten' do 50 | prices = [3.02, 9.91, 17.9, 10.01, 11.0] 51 | # Your code goes here 52 | expect(sorted).to eq([10.01, 9.91, 11.0, 3.02, 17.9]) 53 | end 54 | 55 | xit 'by number of cents' do 56 | prices = [3.02, 9.91, 7.9, 10.01, 11.0] 57 | # Your code goes here 58 | expect(sorted).to eq([11.0, 10.01, 3.02, 7.9, 9.91]) 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/sort_by_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'sort_by' do 2 | 3 | it 'alphabetically' do 4 | words = ["broccoli", "Carrots", "FISH", "Bacon", "candy"] 5 | sorted = words.sort_by do |word| 6 | word.downcase 7 | end 8 | expect(sorted).to eq(["Bacon", "broccoli", "candy", "Carrots", "FISH"]) 9 | end 10 | 11 | xit 'alphabetically by last letter' do 12 | things = ["pill", "box", "glass", "water", "sponge"] 13 | sorted = things.sort_by do |thing| 14 | # Your code goes here 15 | end 16 | expect(sorted).to eq(["sponge", "pill", "water", "glass", "box"]) 17 | end 18 | 19 | xit 'distance' do 20 | distances = ["1cm", "9cm", "30cm", "4cm", "2cm"] 21 | # Your code goes here 22 | expect(sorted).to eq(["1cm", "2cm", "4cm", "9cm", "30cm"]) 23 | end 24 | 25 | xit 'length' do 26 | words = ["heteromorph", "ancyloceratina", "bioengineering", "mathematical", "bug"] 27 | # Your code goes here 28 | expect(sorted).to eq(["bug", "heteromorph", "mathematical", "ancyloceratina", "bioengineering"]) 29 | end 30 | 31 | xit 'proximity to ten' do 32 | prices = [3.02, 9.91, 17.9, 10.01, 11.0] 33 | # Your code goes here 34 | expect(sorted).to eq([10.01, 9.91, 11.0, 3.02, 17.9]) 35 | end 36 | 37 | xit 'number of cents' do 38 | prices = [3.02, 9.91, 7.9, 10.01, 11.0] 39 | # Your code goes here 40 | expect(sorted).to eq([11.0, 10.01, 3.02, 7.9, 9.91]) 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/zip_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'zip pattern' do 2 | 3 | it 'speed interview pairs' do 4 | list1 = ["Alice", "Bob", "Charlie"] 5 | list2 = ["Xenia", "Yves", "Zach"] 6 | pairs = [] 7 | list1.length.times do |i| 8 | pairs << [list1[i], list2[i]] 9 | end 10 | expect(pairs).to eq([["Alice", "Xenia"], ["Bob", "Yves"], ["Charlie", "Zach"]]) 11 | end 12 | 13 | xit 'menu' do 14 | list1 = ["NY Strip Steak", "Lamb Stew", "Grilled Salmon"] 15 | list2 = [29, 23, 26] 16 | prices = [] 17 | list1.length.times do |i| 18 | # Your code goes here 19 | end 20 | expect(prices).to eq([["NY Strip Steak", 29], ["Lamb Stew", 23], ["Grilled Salmon", 26]]) 21 | end 22 | 23 | xit 'lottery tickets' do 24 | list1 = ["Alice", "Bob", "Charlie"] 25 | list2 = [298, 166, 237] 26 | tickets = [] 27 | # Your code goes here 28 | expect(tickets).to eq([["Alice", 298], ["Bob", 166], ["Charlie", 237]]) 29 | end 30 | 31 | xit 'equivalent dna sequences' do 32 | strand1 = ["G", "A", "T", "T", "A", "C", "A"] 33 | strand2 = ["G", "A", "T", "G", "A", "C", "A"] 34 | # Your code goes here 35 | expect(pairs).to eq([["G", "G"], ["A", "A"], ["T", "T"], ["T", "G"], ["A", "A"], ["C", "C"], ["A", "A"]]) 36 | end 37 | 38 | xit 'solitaire' do 39 | list1 = ["Ace", "6", "10", "Queen"] 40 | list2 = [:clubs, :diamonds, :hearts, :spades] 41 | # Your code goes here 42 | expect(cards).to eq([["Ace", :clubs], ["6", :diamonds], ["10", :hearts], ["Queen", :spades]]) 43 | end 44 | 45 | xit 'colors' do 46 | list1 = ["shoes", "tie", "umbrella"] 47 | list2 = ["red", "pink", "black"] 48 | # Your code goes here 49 | expect(fashion).to eq([["shoes", "red"], ["tie", "pink"], ["umbrella", "black"]]) 50 | end 51 | 52 | end 53 | 54 | -------------------------------------------------------------------------------- /enumerables/exercises_1/spec/zip_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'zip' do 2 | 3 | it 'speed interview pairs' do 4 | list1 = ["Alice", "Bob", "Charlie"] 5 | list2 = ["Xenia", "Yves", "Zach"] 6 | pairs = list1.zip(list2) 7 | expect(pairs).to eq([["Alice", "Xenia"], ["Bob", "Yves"], ["Charlie", "Zach"]]) 8 | end 9 | 10 | xit 'test menu' do 11 | list1 = ["NY Strip Steak", "Lamb Stew", "Grilled Salmon"] 12 | list2 = [29, 23, 26] 13 | # Your code goes here 14 | expect(prices).to eq([["NY Strip Steak", 29], ["Lamb Stew", 23], ["Grilled Salmon", 26]]) 15 | end 16 | 17 | xit 'lottery tickets' do 18 | list1 = ["Alice", "Bob", "Charlie"] 19 | list2 = [298, 166, 237] 20 | # Your code goes here 21 | expect(tickets).to eq([["Alice", 298], ["Bob", 166], ["Charlie", 237]]) 22 | end 23 | 24 | xit 'equivalent dna sequences' do 25 | strand1 = ["G", "A", "T", "T", "A", "C", "A"] 26 | strand2 = ["G", "A", "T", "G", "A", "C", "A"] 27 | # Your code goes here 28 | expect(pairs).to eq([["G", "G"], ["A", "A"], ["T", "T"], ["T", "G"], ["A", "A"], ["C", "C"], ["A", "A"]]) 29 | end 30 | 31 | xit 'solitaire' do 32 | list1 = ["Ace", "6", "10", "Queen"] 33 | list2 = [:clubs, :diamonds, :hearts, :spades] 34 | # Your code goes here 35 | expect(cards).to eq([["Ace", :clubs], ["6", :diamonds], ["10", :hearts], ["Queen", :spades]]) 36 | end 37 | 38 | xit 'colors' do 39 | list1 = ["shoes", "tie", "umbrella"] 40 | list2 = ["red", "pink", "black"] 41 | # Your code goes here 42 | expect(fashion).to eq([["shoes", "red"], ["tie", "pink"], ["umbrella", "black"]]) 43 | end 44 | 45 | end 46 | 47 | -------------------------------------------------------------------------------- /initialize/README.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | These exercises are intended to practice with the concept of initialization. Some of them will require additional methods besides initialize. 4 | 5 | ## Running the tests 6 | 7 | Navigate to the `initialize` folder. 8 | 9 | Run each test with `rspec spec/_spec.rb`. Work on the tests in alphabetical order beginning with `aardvark_spec.rb`, then `beaver_spec.rb`, etc. 10 | 11 | If you see an error like: 12 | 13 | ``` 14 | > ruby initialize/test/beaver_spec.rb 15 | Traceback (most recent call last): 16 | 2: from initialize/test/beaver_spec.rb:3:in `
' 17 | 1: from /Users/joshthompson/.rvm/rubies/ruby-2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require' 18 | /Users/joshthompson/.rvm/rubies/ruby-2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ./lib/beaver (LoadError) 19 | ``` 20 | 21 | You'll have to _create_ the missing file. Look in the `lib` directory, see how the only file is `aardfark.rb`? This error message is saying 22 | > I cannot find ./lib/beaver.rb 23 | 24 | So, create a `beaver.rb` file in the `lib` directory, and try it again! 25 | 26 | ## Learning about Initialization 27 | 28 | You'll be well served to read [Classes and Objects - Part I (LaunchSchool)](https://launchschool.com/books/oo_ruby/read/classes_and_objects_part1). 29 | 30 | These exercises pair very well with the above written explanations. 31 | 32 | ------------------------------- 33 | 34 | The difficulty of these exercises steps up when you get to `kangaroo_spec.rb`, so once you've completed a few of them, consider jumping tracks to look at the `mythical-creatures` exercises. 35 | 36 | Once you've completed some of _those_ exercises, you'll be in good shape to come back to these initialization exercises and make some more progress. 37 | 38 | Good luck! 39 | -------------------------------------------------------------------------------- /initialize/lib/aardvark.rb: -------------------------------------------------------------------------------- 1 | 2 | class Aardvark 3 | attr_reader :name 4 | 5 | def initialize(name) 6 | @name = name 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /initialize/spec/aardvark_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/aardvark' 3 | 4 | RSpec.describe Aardvark do 5 | it 'exists' do 6 | art = Aardvark.new("Art") 7 | 8 | expect(art).to be_an_instance_of(Aardvark) 9 | end 10 | 11 | it 'has a name' do 12 | art = Aardvark.new("Art") 13 | 14 | expect(art.name).to eq("Art") 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /initialize/spec/beaver_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/beaver' 3 | 4 | RSpec.describe Beaver do 5 | it 'exists' do 6 | barry = Beaver.new("Barry") 7 | 8 | expect(barry).to be_an_instance_of(Beaver) 9 | end 10 | 11 | it 'has a name' do 12 | barry = Beaver.new("Barry") 13 | 14 | expect(barry.name).to eq("Barry the Beaver") 15 | end 16 | 17 | it 'could have a different name' do 18 | beatrice = Beaver.new("Beatrice") 19 | 20 | expect(beatrice.name).to eq("Beatrice the Beaver") 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /initialize/spec/cat_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/cat' 3 | 4 | RSpec.describe Cat do 5 | it 'exists' do 6 | carlos = Cat.new("Carlos") 7 | 8 | expect(carlos).to be_an_instance_of(Cat) 9 | end 10 | 11 | it 'has a name' do 12 | carlos = Cat.new("Carlos") 13 | 14 | expect(carlos.name).to eq("Carlos") 15 | end 16 | 17 | it 'has a sound' do 18 | carlos = Cat.new("Carlos") 19 | 20 | expect(carlos.sound).to eq("meow") 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /initialize/spec/dog_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/dog' 3 | 4 | RSpec.describe Dog do 5 | it 'exists' do 6 | doug = Dog.new("Doug", "Golden Retriever") 7 | 8 | expect(doug).to be_an_instance_of(Dog) 9 | end 10 | 11 | it 'has a greeting' do 12 | doug = Dog.new("Doug", "Golden Retriever") 13 | 14 | expect(doug.greeting).to eq("Woof, I'm Doug the Golden Retriever!") 15 | end 16 | 17 | it 'can have a different greeting' do 18 | dolly = Dog.new("Dolly", "Lab") 19 | 20 | expect(dolly.greeting).to eq("Woof, I'm Dolly the Lab!") 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /initialize/spec/eel_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/eel' 3 | 4 | RSpec.describe Eel do 5 | it 'exists' do 6 | eel = Eel.new("Earl") 7 | 8 | expect(eel).to be_an_instance_of(Eel) 9 | end 10 | 11 | it 'is anonymous' do 12 | eel = Eel.new("Earl") 13 | 14 | expect(eel.name).to eq("just another eel") 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /initialize/spec/ferret_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/ferret' 3 | 4 | RSpec.describe Ferret do 5 | it 'exists' do 6 | ferret = Ferret.new 7 | 8 | expect(ferret).to be_an_instance_of(Ferret) 9 | end 10 | 11 | it 'has no name' do 12 | ferret = Ferret.new 13 | 14 | expect(ferret.name).to eq(nil) 15 | end 16 | 17 | it 'can be given a name' do 18 | ferret = Ferret.new 19 | 20 | ferret.give_name('Felix') 21 | 22 | expect(ferret.name).to eq('Felix') 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /initialize/spec/groundhog_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/groundhog' 3 | 4 | RSpec.describe Groundhog do 5 | it 'exists' do 6 | gabby = Groundhog.new("Gabby") 7 | 8 | expect(gabby).to be_an_instance_of(Groundhog) 9 | end 10 | 11 | it 'has a name' do 12 | gabby = Groundhog.new("Gabby") 13 | 14 | expect(gabby.name).to eq("Gabby") 15 | end 16 | 17 | it 'can change name' do 18 | groundhog = Groundhog.new("Gabby") 19 | groundhog.name = "Gary" 20 | 21 | expect(groundhog.name).to eq("Gary") 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /initialize/spec/horse_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/horse' 3 | 4 | RSpec.describe Horse do 5 | it 'exists' do 6 | harry = Horse.new("Harry") 7 | 8 | expect(harry).to be_an_instance_of(Horse) 9 | end 10 | 11 | it 'has a name' do 12 | harry = Horse.new("Harry") 13 | 14 | expect(harry.name).to eq("Harry") 15 | end 16 | 17 | it 'starts with empty diet' do 18 | harry = Horse.new("Harry") 19 | 20 | expect(harry.diet).to eq([]) 21 | end 22 | 23 | it 'can add to diet' do 24 | harry = Horse.new("Harry") 25 | 26 | harry.add_to_diet("Hay") 27 | harry.add_to_diet("Sugar Cubes") 28 | 29 | expect(harry.diet).to eq(["Hay", "Sugar Cubes"]) 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /initialize/spec/iguana_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/iguana' 3 | 4 | RSpec.describe Iguana do 5 | it 'exists' do 6 | iguana = Iguana.new 7 | 8 | expect(iguana).to be_an_instance_of(Iguana) 9 | end 10 | 11 | it 'starts with no colors' do 12 | iguana = Iguana.new 13 | 14 | expect(iguana.colors).to eq([]) 15 | end 16 | 17 | it 'can set colors' do 18 | iguana = Iguana.new 19 | 20 | iguana.colors = ["Green", "Red", "White"] 21 | 22 | expect(iguana.colors).to eq(["Green", "Red", "White"]) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /initialize/spec/jackalope_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/jackalope' 3 | 4 | RSpec.describe Jackalope do 5 | it 'exists' do 6 | jorge = Jackalope.new("Jorge") 7 | 8 | expect(jorge).to be_an_instance_of(Jackalope) 9 | end 10 | 11 | it 'has a name' do 12 | jorge = Jackalope.new("Jorge") 13 | 14 | expect(jorge.name).to eq("Jorge") 15 | end 16 | 17 | it 'starts with an array of etymology' do 18 | jorge = Jackalope.new("Jorge") 19 | 20 | expect(jorge.etymology).to eq(["Jackrabbit", "Antelope"]) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /initialize/spec/kangaroo_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/kangaroo' 3 | 4 | RSpec.describe Kangaroo do 5 | it 'exists' do 6 | kerrie = Kangaroo.new("Kerrie", ["Kat", "Kyle"]) 7 | 8 | expect(kerrie).to be_an_instance_of(Kangaroo) 9 | end 10 | 11 | it 'has children' do 12 | kerrie = Kangaroo.new("Kerrie", ["Kat", "Kyle"]) 13 | 14 | expect(kerrie.children).to eq(["Kat", "Kyle"]) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /initialize/spec/lion_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/lion' 3 | 4 | RSpec.describe Lion do 5 | it 'exists' do 6 | lionel = Lion.new({name: "Lionel", sound: "roar"}) 7 | 8 | expect(lionel).to be_an_instance_of(Lion) 9 | end 10 | 11 | it 'has a name' do 12 | lionel = Lion.new({name: "Lionel", sound: "roar"}) 13 | 14 | expect(lionel.name).to eq("Lionel") 15 | end 16 | 17 | it 'has a sound' do 18 | lionel = Lion.new({name: "Lionel", sound: "roar"}) 19 | 20 | expect(lionel.sound).to eq("roar") 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /initialize/spec/monkey_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/monkey' 3 | 4 | RSpec.describe Monkey do 5 | it 'exists' do 6 | margaret = Monkey.new(["Margaret", "Bonobo", "Bananas"]) 7 | 8 | expect(margaret).to be_an_instance_of(Monkey) 9 | end 10 | 11 | it 'has a name' do 12 | margaret = Monkey.new(["Margaret", "Bonobo", "Bananas"]) 13 | 14 | expect(margaret.name).to eq("Margaret") 15 | end 16 | 17 | it 'has a type' do 18 | margaret = Monkey.new(["Margaret", "Bonobo", "Bananas"]) 19 | 20 | expect(margaret.type).to eq("Bonobo") 21 | end 22 | 23 | it 'has a favorite_food' do 24 | margaret = Monkey.new(["Margaret", "Bonobo", "Bananas"]) 25 | 26 | expect(margaret.favorite_food).to eq("Bananas") 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /initialize/spec/narwhal_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/narwhal' 3 | 4 | RSpec.describe Narwhal do 5 | it 'exists' do 6 | nancy = Narwhal.new({cute: true, weight: 500, name: "Nancy"}) 7 | 8 | expect(nancy).to be_an_instance_of(Narwhal) 9 | end 10 | 11 | it 'has a name' do 12 | nancy = Narwhal.new({cute: true, weight: 500, name: "Nancy"}) 13 | 14 | expect(nancy.name).to eq("Nancy") 15 | end 16 | 17 | it 'has a weight' do 18 | nancy = Narwhal.new({cute: true, weight: 500, name: "Nancy"}) 19 | 20 | expect(nancy.weight).to eq(500) 21 | end 22 | 23 | it 'has is cute' do 24 | nancy = Narwhal.new({cute: true, weight: 500, name: "Nancy"}) 25 | 26 | expect(nancy.cute?).to eq(true) 27 | end 28 | 29 | it 'can be a different narwhal' do 30 | neville = Narwhal.new({cute: false, weight: 499, name: "Neville"}) 31 | 32 | expect(neville.name).to eq("Neville") 33 | expect(neville.weight).to eq(499) 34 | expect(neville.cute?).to eq(false) 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /initialize/spec/octopus_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/eel' 3 | require_relative '../lib/narwhal' 4 | require_relative '../lib/octopus' 5 | 6 | RSpec.describe Octopus do 7 | it 'exists' do 8 | nancy = Narwhal.new({cute: true, weight: 500, name: "Nancy"}) 9 | orville = Octopus.new("Orville", nancy) 10 | 11 | expect(orville).to be_an_instance_of(Octopus) 12 | end 13 | 14 | it 'has a name' do 15 | nancy = Narwhal.new({cute: true, weight: 500, name: "Nancy"}) 16 | orville = Octopus.new("Orville", nancy) 17 | 18 | expect(orville.name).to eq("Orville") 19 | end 20 | 21 | it 'has an animal as a friend' do 22 | nancy = Narwhal.new({cute: true, weight: 500, name: "Nancy"}) 23 | orville = Octopus.new("Orville", nancy) 24 | 25 | expect(orville.friend).to eq(nancy) 26 | end 27 | 28 | it 'can have any animal as a friend' do 29 | eel = Eel.new("Earl") 30 | orville = Octopus.new("Orville", eel) 31 | 32 | expect(orville.friend).to eq(eel) 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /initialize/spec/parrot_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/parrot' 3 | 4 | RSpec.describe Parrot do 5 | it 'exists' do 6 | words = ["Sugar", "Flashy", "Giant", "Whisper"] 7 | perry = Parrot.new({name: "Perry", known_words: words}) 8 | 9 | expect(perry).to be_an_instance_of(Parrot) 10 | end 11 | 12 | it 'has a name' do 13 | words = ["Sugar", "Flashy", "Giant", "Whisper"] 14 | perry = Parrot.new({name: "Perry", known_words: words}) 15 | 16 | expect(perry.name).to eq("Perry") 17 | end 18 | 19 | it 'has a sound' do 20 | words = ["Sugar", "Flashy", "Giant", "Whisper"] 21 | perry = Parrot.new({name: "Perry", known_words: words}) 22 | 23 | expect(perry.sound).to eq('Squawk!') 24 | end 25 | 26 | it 'knows words' do 27 | words = ["Sugar", "Flashy", "Giant", "Whisper"] 28 | perry = Parrot.new({name: "Perry", known_words: words}) 29 | 30 | expect(perry.known_words).to eq(["Sugar", "Flashy", "Giant", "Whisper"]) 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /initialize/spec/quail_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/quail' 3 | 4 | RSpec.describe Quail do 5 | it 'exists' do 6 | quinn = Quail.new("Quinn") 7 | 8 | expect(quinn).to be_an_instance_of(Quail) 9 | end 10 | 11 | it 'has a name' do 12 | quinn = Quail.new("Quinn") 13 | 14 | expect(quinn.name).to eq("Quinn") 15 | end 16 | 17 | it 'will capitalize its name' do 18 | quinn = Quail.new("quinn") 19 | 20 | expect(quinn.name).to eq("Quinn") 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /initialize/spec/rabbit_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/rabbit' 3 | 4 | RSpec.describe Rabbit do 5 | it 'exists' do 6 | ralph = Rabbit.new({name: "Ralph", num_syllables: 1}) 7 | 8 | expect(ralph).to be_an_instance_of(Rabbit) 9 | end 10 | 11 | it 'has a name' do 12 | ralph = Rabbit.new({name: "Ralph", num_syllables: 1}) 13 | 14 | expect(ralph.name).to eq("Ralph") 15 | end 16 | 17 | it "name ends with rabbit if 2 syllables" do 18 | ralph = Rabbit.new({name: "Ralph", num_syllables: 1}) 19 | rita = Rabbit.new({name: "Rita", num_syllables: 2}) 20 | roberto = Rabbit.new({name: "Roberto", num_syllables: 3}) 21 | 22 | expect(ralph.name).to eq("Ralph") 23 | expect(rita.name).to eq("Rita Rabbit") 24 | expect(roberto.name).to eq("Roberto") 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /iteration/README.md: -------------------------------------------------------------------------------- 1 | In Ruby, #each is an incredibly powerful tool that can be used to help solve many of the programming challenges you will come across. The test files in the exercise folder will help you expand your knowledge of what #each can do and push you to think about the power of simple iteration. 2 | 3 | The exercises can be completed in any order, but if you would like a suggestion, see below: 4 | 5 | Suggested Order: 6 | 7 | * map_pattern_spec.rb 8 | * find_pattern_spec.rb 9 | * select_pattern_spec.rb 10 | * count_pattern_spec.rb 11 | * all_pattern_spec.rb 12 | * max_and_min_by_pattern_spec.rb 13 | * inject_pattern_spec.rb 14 | * group_by_pattern_spec.rb 15 | -------------------------------------------------------------------------------- /iteration/exercises/.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /iteration/exercises/spec/all_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'all pattern' do 2 | it 'test 1' do 3 | account_balances = [0, 0, 0, 0, 0, 0, 0] 4 | all_zeros = true 5 | account_balances.each do |balance| 6 | all_zeros = false unless balance.zero? 7 | end 8 | expect(all_zeros).to be true 9 | end 10 | 11 | it 'test 2' do 12 | account_balances = { 13 | checking: 0, 14 | saving: 0, 15 | retirement_401k: 0, 16 | retirement_ira: 0, 17 | } 18 | all_zeros = true 19 | account_balances.each do |account, balance| 20 | if !balance.zero? 21 | all_zeros = false 22 | end 23 | end 24 | expect(all_zeros).to be true 25 | end 26 | 27 | xit 'test 3' do 28 | words = ["love", "hate", "fire", "bird", "call"] 29 | all_four_letters = true 30 | words.each do |word| 31 | # Your Code Here 32 | end 33 | expect(all_four_letters).to be true 34 | end 35 | 36 | xit 'test 4' do 37 | words = { 38 | one: "love", 39 | two: "hate", 40 | three: "fire", 41 | four: "bird", 42 | five: "call" 43 | } 44 | all_four_letters = true 45 | words.each do |position, word| 46 | # Your Code Here 47 | end 48 | expect(all_four_letters).to be true 49 | end 50 | 51 | xit 'test 5' do 52 | statuses = [:busy, :busy, :busy] 53 | all_busy = true 54 | # Your Code Here 55 | 56 | expect(all_busy).to be true 57 | end 58 | 59 | xit 'test 6' do 60 | friend_status = { 61 | "Megan" => :busy, 62 | "Sarah" => :busy, 63 | "Duncan" => :busy, 64 | } 65 | all_busy = true 66 | # Your Code Here 67 | 68 | expect(all_busy).to be true 69 | end 70 | 71 | xit 'test 7' do 72 | zip_codes = [94381, 831, 50009, 36232, 8992, 89999, 11110] 73 | # Your Code Here 74 | 75 | expect(all_five_digits).to be false 76 | end 77 | 78 | xit 'test 8' do 79 | zip_codes = { 80 | "Megan" => 94381, 81 | "Sarah" => 831, 82 | "Duncan" => 50009, 83 | "Raymart" => 36232, 84 | "Alec" => 89092, 85 | "Cameron" => 89999, 86 | "Joshua" => 11110 87 | } 88 | # Your Code Here 89 | 90 | expect(all_five_digits).to be false 91 | end 92 | 93 | xit 'test 9' do 94 | snacks = ["GARLIC PLANTAINS", "SNICKERDOODLES", "Pretzels"] 95 | # Your Code Here 96 | 97 | expect(all_caps).to be false 98 | end 99 | 100 | xit 'test 10' do 101 | snacks = { 102 | savory: "GARLIC PLANTAINS", 103 | sweet: "SNICKERDOODLES", 104 | salty: "Pretzels" 105 | } 106 | # Your Code Here 107 | 108 | expect(all_caps).to be false 109 | end 110 | end 111 | -------------------------------------------------------------------------------- /iteration/exercises/spec/count_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'count pattern' do 2 | 3 | it 'test 1' do 4 | ages = [39, 45, 29, 24, 50] 5 | younger_than_thirty = 0 6 | ages.each do |age| 7 | if age < 30 8 | younger_than_thirty += 1 9 | end 10 | end 11 | expect(younger_than_thirty).to eq(2) 12 | end 13 | 14 | xit 'test 2' do 15 | ages = { 16 | abdi: 39, 17 | hassan: 45, 18 | ladonna: 29, 19 | margaret: 24, 20 | miguel: 50 21 | } 22 | younger_than_thirty = 0 23 | ages.each do |name, age| 24 | if age < 30 25 | younger_than_thirty += 1 26 | end 27 | end 28 | expect(younger_than_thirty).to eq(2) 29 | end 30 | 31 | xit 'test 3' do 32 | ages = [39, 45, 29, 24, 50] 33 | older_than_fifty = 0 34 | ages.each do |age| 35 | # Your Code Here 36 | end 37 | 38 | expect(older_than_fifty).to eq(0) 39 | end 40 | 41 | xit 'test 4' do 42 | ages = { 43 | abdi: 39, 44 | hassan: 45, 45 | ladonna: 29, 46 | margaret: 24, 47 | miguel: 50 48 | } 49 | older_than_fifty = 0 50 | ages.each do |name, age| 51 | # Your Code Here 52 | end 53 | 54 | expect(older_than_fifty).to eq(0) 55 | end 56 | 57 | xit 'test 5' do 58 | ages = [39, 45, 29, 24, 50] 59 | multiple_of_three = 0 60 | # Your Code Here 61 | 62 | expect(multiple_of_three).to eq(3) 63 | end 64 | 65 | xit 'test 6' do 66 | ages = { 67 | abdi: 39, 68 | hassan: 45, 69 | ladonna: 29, 70 | margaret: 24, 71 | miguel: 50 72 | } 73 | multiple_of_three = 0 74 | # Your Code Here 75 | 76 | expect(multiple_of_three).to eq(3) 77 | end 78 | 79 | xit 'test 7' do 80 | family = ["alice", "bob", "charlie", "david", "eve"] 81 | # Your Code Here 82 | 83 | expect(names_with_three_letters).to eq(2) 84 | end 85 | 86 | xit 'test 8' do 87 | family = { 88 | mother: "alice", 89 | father: "bob", 90 | brother: "charlie", 91 | uncle: "david", 92 | sister: "eve" 93 | } 94 | # Your Code Here 95 | 96 | expect(names_with_three_letters).to eq(2) 97 | end 98 | 99 | xit 'test 9' do 100 | prices = [1.0, 3.9, 5.99, 18.5, 20.0] 101 | # Your code goes here 102 | 103 | expect(whole_numbers).to eq(2) 104 | end 105 | 106 | xit 'test 10' do 107 | prices = { 108 | "shoes" => 1.0, 109 | "backpack" => 3.9, 110 | "books" => 5.99, 111 | "posters" => 18.5, 112 | "food" => 20.0 113 | } 114 | # Your Code Here 115 | 116 | expect(whole_numbers).to eq(2) 117 | end 118 | end 119 | -------------------------------------------------------------------------------- /iteration/exercises/spec/find_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'find pattern' do 2 | 3 | it 'test 1' do 4 | ages = [39, 45, 29, 24, 50] 5 | younger_than_thirty = nil 6 | ages.each do |age| 7 | if age < 30 8 | younger_than_thirty = age 9 | break 10 | end 11 | end 12 | expect(younger_than_thirty).to eq(29) 13 | end 14 | 15 | it 'test 2' do 16 | ages = { 17 | abdi: 39, 18 | hassan: 45, 19 | ladonna: 29, 20 | margaret: 24, 21 | miguel: 50 22 | } 23 | younger_than_thirty = nil 24 | ages.each do |name, age| 25 | if age < 30 26 | younger_than_thirty = name 27 | break 28 | end 29 | end 30 | expect(younger_than_thirty).to eq(:ladonna) 31 | end 32 | 33 | xit 'test 3' do 34 | ages = [39, 45, 29, 24, 50] 35 | older_than_fifty = nil 36 | ages.each do |age| 37 | # Your Code Here 38 | end 39 | 40 | expect(older_than_fifty).to be_nil 41 | end 42 | 43 | xit 'test 4' do 44 | ages = { 45 | abdi: 39, 46 | hassan: 45, 47 | ladonna: 29, 48 | margaret: 24, 49 | miguel: 50 50 | } 51 | older_than_fifty = nil 52 | ages.each do |name, age| 53 | # Your Code Here 54 | end 55 | 56 | expect(older_than_fifty).to be_nil 57 | end 58 | 59 | xit 'test 5' do 60 | ages = [39, 45, 29, 24, 50] 61 | multiple_of_three = nil 62 | # Your Code Here 63 | 64 | expect(multiple_of_three).to eq(39) 65 | end 66 | 67 | xit 'test 6' do 68 | ages = { 69 | abdi: 39, 70 | hassan: 45, 71 | ladonna: 29, 72 | margaret: 24, 73 | miguel: 50 74 | } 75 | multiple_of_three = nil 76 | # Your Code Here 77 | 78 | expect(multiple_of_three).to eq(:abdi) 79 | end 80 | 81 | xit 'test 7' do 82 | people = ["Willie", "Carmen Sandiego", "Bryan", "Faith", "Zac"] 83 | # Your Code Here 84 | 85 | expect(carmen).to eq("Carmen Sandiego") 86 | end 87 | 88 | xit 'test 8' do 89 | places = { 90 | Bangkok: "Willie", 91 | Santa_Fe: "Carmen Sandiego", 92 | Rome: "Bryan", 93 | Munich: "Faith", 94 | Mogudishu: "Zac" 95 | } 96 | # Your Code Here 97 | 98 | expect(where_is_carmen_sandiego).to eq(:Santa_Fe) 99 | end 100 | 101 | xit 'test 9' do 102 | numbers = [3, 7, 13, 11, 10, 2, 17] 103 | # Your Code Here 104 | 105 | expect(even).to eq(10) 106 | end 107 | 108 | xit 'test 10' do 109 | purchases = { 110 | "shoes" => :paid, 111 | "backpack" => :paid, 112 | "books" => :pending, 113 | "posters" => :paid, 114 | "food" => :pending 115 | } 116 | # Your Code Here 117 | 118 | expect(pending).to eq(:books) 119 | end 120 | 121 | xit 'test 11' do 122 | purchases = { 123 | "shoes" => :paid, 124 | "backpack" => :paid, 125 | "books" => :pending, 126 | "posters" => :paid, 127 | "food" => :pending 128 | } 129 | # Your Code Here 130 | 131 | expect(starts_with_b).to eq("backpack") 132 | end 133 | end 134 | -------------------------------------------------------------------------------- /iteration/exercises/spec/group_by_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'group by pattern' do 2 | 3 | it 'by word length' do 4 | words = ["sue", "alice", "steve", "sally", "adam", "fort", "tops", "dog", "cat"] 5 | grouped = Hash.new {|hash, key| hash[key] = []} 6 | words.each do |word| 7 | grouped[word.length] << word 8 | end 9 | expected = {3=>["sue", "dog", "cat"], 4=>["adam", "fort", "tops"], 5=>["alice", "steve", "sally"]} 10 | expect(grouped).to eq(expected) 11 | end 12 | 13 | xit 'by odd and even' do 14 | numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 15 | odd_and_even = Hash.new {|hash, key| hash[key] = []} 16 | numbers.each do |number| 17 | # Your code goes here 18 | end 19 | expected = {1=>[1, 1, 3, 5, 13, 21, 55], 0=>[2, 8, 34]} 20 | expected(odd_and_even).to eq(expected) 21 | end 22 | 23 | xit 'by first letter' do 24 | words = ["ant", "axis", "albatross", "bolt", "badge", "butter", "car", "cdr", "column"] 25 | words_by_first_letter = Hash.new {|hash, key| hash[key] = []} 26 | # Your code goes here 27 | expected = {"a"=>["ant", "axis", "albatross"], "b"=>["bolt", "badge", "butter"], "c"=>["car", "cdr", "column"]} 28 | expect(words_by_first_letter).to eq(expected) 29 | end 30 | 31 | xit 'by uniqueness' do 32 | words = ["one", "two", "one", "TWO", "three", "one", "three", "three", "three"] 33 | # Your code goes here 34 | expected = {"one"=>["one", "one", "one"], "two"=>["two", "TWO"], "three"=>["three", "three", "three", "three"]} 35 | expect(grouped).to eq(expected) 36 | end 37 | 38 | xit 'by number of zeroes' do 39 | numbers = [1, 3, 500, 200, 4000, 3000, 10000, 90, 20, 500000] 40 | # Your code goes here 41 | expected = {0=>[1, 3], 2=>[500, 200], 3=>[4000, 3000], 4=>[10000], 1=>[90, 20], 5=>[500000]} 42 | expect(grouped).to eq(expected) 43 | end 44 | 45 | xit 'by order of magnitude' do 46 | numbers = [1, 3, 503, 239, 4938, 3932, 19982, 93, 21, 501787] 47 | # Your code goes here 48 | expected = {1=>[1, 3], 2=>[93, 21], 3=>[503, 239], 4=>[4938, 3932], 5=>[19982], 6=>[501787]} 49 | expect(grouped).to eq(expected) 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /iteration/exercises/spec/inject_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'inject pattern test' do 2 | 3 | it 'test 1' do 4 | numbers = [28, 12, 38, 1, 91] 5 | # Iterate over the numbers array defined above, 6 | # to find the difference of all the numbers 7 | difference = 0 8 | numbers.each do |number| 9 | difference = difference - number 10 | end 11 | expect(difference).to eq(-170) 12 | end 13 | 14 | xit 'test 2' do 15 | bills = { 16 | rent: 800, 17 | car: 240, 18 | insurance: 110, 19 | medical: 1112 20 | } 21 | # Iterate over the bills hash defined above 22 | # to find the difference of all the values 23 | 24 | difference = 0 25 | bills.each do |(category, amount)| 26 | difference -= amount 27 | end 28 | expect(difference).to eq(-2262) 29 | end 30 | 31 | xit 'test 3' do 32 | numbers = [2, 3, 5, 7] 33 | # Iterate over the numbers array defined above 34 | # to find the product of all the numbers 35 | 36 | product = 1 37 | numbers.each do |number| 38 | # Your Code Here 39 | end 40 | expect(product).to eq(210) 41 | end 42 | 43 | xit 'test 4' do 44 | scrabble_score = { 45 | letter_total: 23, 46 | word_muliplier: 3, 47 | bonus: 2 48 | } 49 | # Iterate over the scarbble_score hash defined above 50 | # to find the product of all the values 51 | 52 | product = 1 53 | scrabble_score.each do |(key, value)| 54 | # Your Code Here 55 | end 56 | expect(product).to eq(138) 57 | end 58 | 59 | xit 'test 5' do 60 | airlines = ["Southwest", "Delta", "United", "Frontier"] 61 | # Iterate over the airlines array defined above to 62 | # create a hash with the name of the airline as the 63 | # key and the length of the name as the value 64 | 65 | number_of_letters = {} 66 | # Your Code Here 67 | 68 | expected = { 69 | "Southwest" => 9, 70 | "Delta" => 5, 71 | "United" => 6, 72 | "Frontier" => 8 73 | } 74 | expect(number_of_letters).to eq(expected) 75 | end 76 | 77 | xit 'test 6' do 78 | topping_calories = { 79 | pepperoni: 430, 80 | sausage: 400, 81 | olives: 10, 82 | peppers: 10, 83 | onions: 20 84 | } 85 | # Iterate over the topping_calories hash defined above 86 | # to create an array of all the toppings 87 | 88 | toppings = [] 89 | # Your Code Here 90 | 91 | expect(toppings).to eq(["pepperoni", "sausage", "olives", "peppers", "onions"]) 92 | end 93 | 94 | xit 'test 7' do 95 | elements = [["a", 1], ["b", 9], ["c", 21]] 96 | # Iterate over the elements array defined above 97 | # to find the sum of all the integers 98 | 99 | # Your Code Here 100 | 101 | expect(sum_of_second_values).to eq(31) 102 | end 103 | 104 | xit 'test 8' do 105 | toppings = { 106 | pepperoni: { 107 | calories: 430, 108 | quantity: 5 109 | }, 110 | sausage: { 111 | calories: 400, 112 | quantity: 10 113 | }, 114 | olives: { 115 | calories: 10, 116 | quantity: 20 117 | }, 118 | peppers: { 119 | calories: 10, 120 | quantity: 20 121 | }, 122 | onions: { 123 | calories: 20, 124 | quantity: 20 125 | } 126 | } 127 | # Iterate over the toppings array defined above to find 128 | # total calories. You will need to multiply each topping's 129 | # calorie count by the quantity 130 | 131 | # Your Code Here 132 | 133 | expect(total_calories).to eq(6950) 134 | end 135 | 136 | xit 'test 9' do 137 | grades = { 138 | quizzes: [8, 5, 3, 6, 5], 139 | tests: [23, 21, 24], 140 | essays: [10, 11, 10], 141 | final: [47] 142 | } 143 | # Iterate over the elements array defined above 144 | # to calculate the final grade. The final grade is 145 | # calculated by averaging each category together and 146 | # summing all of the averages 147 | 148 | # Your code goes here 149 | 150 | expect(final_grade).to eq(85.40) 151 | end 152 | 153 | xit 'test 10' do 154 | menu = { 155 | empanadas: { 156 | flavors: ["chicken", "potato", "steak", "veggie"], 157 | gluten_free: false 158 | }, 159 | scones: { 160 | flavors: ["blueberry", "vanilla"], 161 | gluten_free: false 162 | }, 163 | parfaits: { 164 | flavors: ["blueberry", "strawberry", "cherry"], 165 | gluten_free: true 166 | } 167 | } 168 | 169 | # Iterate over the menu hash above to create a printable 170 | # version of the menu 171 | 172 | # Your Code Here 173 | 174 | expected = "Menu:\n"\ 175 | "- chicken, potato, steak, and veggie empanadas (non gluten free)\n"\ 176 | "- blueberry, and vanilla scones (non gluten free)\n"\ 177 | "- blueberry, strawberry, and cherry parfaits (gluten free)\n" 178 | 179 | expect(printable_menu).to eq(expected) 180 | end 181 | end 182 | -------------------------------------------------------------------------------- /iteration/exercises/spec/map_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'map pattern' do 2 | 3 | it 'test 1' do 4 | names = ["alice", "bob", "charlie"] 5 | capitalized_names = [] 6 | names.each do |name| 7 | capitalized_names << name.capitalize 8 | end 9 | expect(capitalized_names).to eq(["Alice", "Bob", "Charlie"]) 10 | end 11 | 12 | xit 'test 2' do 13 | family = { 14 | mother: "alice", 15 | father: "bob", 16 | brother: "charlie" 17 | } 18 | capitalized_family = {} 19 | family.each do |relationship, name| 20 | capitalized_family[relationship] = name.capitalize 21 | end 22 | expected = { 23 | mother: "Alice", 24 | father: "Bob", 25 | brother: "Charlie" 26 | } 27 | expect(capitalized_family).to eq(expected) 28 | end 29 | 30 | xit 'test 3' do 31 | numbers = [1, 2, 3, 4, 5] 32 | doubles = [] 33 | numbers.each do |number| 34 | # Your Code Here 35 | end 36 | expect(doubles).to eq([2, 4, 6, 8, 10]) 37 | end 38 | 39 | xit 'test 4' do 40 | numbers = { 41 | one: 1, 42 | two: 2, 43 | three: 3, 44 | four: 4, 45 | five: 5 46 | } 47 | doubles = {} 48 | numbers.each do |name, number| 49 | # Your Code Here 50 | end 51 | expected = { 52 | one: 2, 53 | two: 4, 54 | three: 6, 55 | four: 8, 56 | five: 10 57 | } 58 | expect(doubles).to eq(expected) 59 | end 60 | 61 | xit 'test 5' do 62 | numbers = [1, 2, 3, 4, 5] 63 | squares = [] 64 | # Your Code Here 65 | 66 | expect(squares).to eq([1, 4, 9, 16, 25]) 67 | end 68 | 69 | xit 'test 6' do 70 | numbers = { 71 | one: 1, 72 | two: 2, 73 | three: 3, 74 | four: 4, 75 | five: 5 76 | } 77 | squares = {} 78 | # Your Code Here 79 | 80 | expected = { 81 | one: 1, 82 | two: 4, 83 | three: 9, 84 | four: 16, 85 | five: 25 86 | } 87 | expect(squares).to eq(expected) 88 | end 89 | 90 | xit 'test 7' do 91 | names = ["alice", "bob", "charlie", "david", "eve"] 92 | #Your Code Here 93 | 94 | expect(lengths).to eq([5, 3, 7, 5, 3]) 95 | end 96 | 97 | xit 'test 8' do 98 | family = { 99 | mother: "alice", 100 | father: "bob", 101 | brother: "charlie", 102 | uncle: "david", 103 | sister: "eve" 104 | } 105 | #Your Code Here 106 | 107 | expected = { 108 | mother: 5, 109 | father: 3, 110 | brother: 7, 111 | uncle: 5, 112 | sister: 3 113 | } 114 | expect(lengths).to eq(expected) 115 | end 116 | 117 | xit 'test 9' do 118 | names = ["alice", "bob", "charlie", "david", "eve"] 119 | #Your Code Here 120 | 121 | expect(backwards).to eq(["ecila", "bob", "eilrahc", "divad", "eve"]) 122 | end 123 | 124 | xit 'test 10' do 125 | family = { 126 | mother: "alice", 127 | father: "bob", 128 | brother: "charlie", 129 | uncle: "david", 130 | sister: "eve" 131 | } 132 | #Your Code Here 133 | 134 | expected = { 135 | mother: "ecila", 136 | father: "bob", 137 | brother: "eilrahc", 138 | uncle: "divad", 139 | sister: "eve" 140 | } 141 | expect(backwards).to eq(expected) 142 | end 143 | end 144 | 145 | -------------------------------------------------------------------------------- /iteration/exercises/spec/max_and_min_by_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'max and min by pattern' do 2 | it 'test 1' do 3 | numbers = [1, 100, 1000, 1000000] 4 | greatest = numbers[0] 5 | numbers.each do |number| 6 | if number > greatest 7 | greatest = number 8 | end 9 | end 10 | expect(greatest).to eq(1000000) 11 | end 12 | 13 | it 'test 2' do 14 | magnitudes = { 15 | ones: 1, 16 | hundreds: 100, 17 | thousands: 1000, 18 | millions: 1000000 19 | } 20 | greatest = magnitudes[magnitudes.keys[0]] 21 | magnitudes.each do |name, value| 22 | if value > greatest 23 | greatest = value 24 | end 25 | end 26 | expect(greatest).to eq(1000000) 27 | end 28 | 29 | xit 'test 3' do 30 | meals = ["banana", "nuts", "salad", "steak", "cake"] 31 | shortest_word = meals[0] 32 | meals.each do |meal| 33 | # Your Code Here 34 | end 35 | 36 | expect(shortest_word).to eq("nuts") 37 | end 38 | 39 | xit 'test 4' do 40 | meals = { 41 | breakfast: "banana", 42 | snack: "nuts", 43 | lunch: "salad", 44 | dinner: "steak", 45 | dessert: "cake" 46 | } 47 | shortest_word = meals[meals.keys.first] 48 | meals.each do |meal, dish| 49 | # Your Code Here 50 | end 51 | 52 | expect(shortest_word).to eq("nuts") 53 | end 54 | 55 | xit 'test 5' do 56 | stats = [3001, 431, 1695, 0.27601, 0.340] 57 | most_digits = stats[0] 58 | # Your Code Here 59 | 60 | expect(most_digits).to eq(0.27601) 61 | end 62 | 63 | xit 'test 6' do 64 | stats = { 65 | games_played: 3001, 66 | home_runs: 431, 67 | rbi: 1695, 68 | batting_average: 0.27601, 69 | on_base_percentage: 0.340 70 | } 71 | most_digits = stats[stats.keys.first] 72 | # Your Code Here 73 | 74 | expect(most_digits).to eq(0.27601) 75 | end 76 | 77 | xit 'test 7' do 78 | ages = [39, 45, 29, 24, 50] 79 | # Your Code Here 80 | 81 | expect(oldest).to eq(50) 82 | end 83 | 84 | xit 'test 8' do 85 | ages = { 86 | abdi: 39, 87 | hassan: 45, 88 | ladonna: 29, 89 | margaret: 24, 90 | miguel: 50 91 | } 92 | # Your Code Here 93 | 94 | expected = {name: "miguel", age: 50} 95 | expect(oldest).to eq(expected) 96 | end 97 | 98 | xit 'test 9' do 99 | programmers = [["katrina", "sandi", "jim", "aaron", "desi"], ["abby", "jon", "susan"]] 100 | # Your Code Here 101 | 102 | expect(fewest_programmers).to eq(["abby", "jon", "susan"]) 103 | end 104 | 105 | xit 'test 10' do 106 | programmers = {ruby: ["katrina", "sandi", "jim", "aaron", "desi"], java: ["abby", "jon", "susan"]} 107 | # Your Code Here 108 | 109 | expect(fewest_programmers).to eq(:java) 110 | end 111 | end 112 | -------------------------------------------------------------------------------- /iteration/exercises/spec/select_pattern_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'select pattern' do 2 | 3 | it 'test 1' do 4 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 5 | evens = [] 6 | numbers.each do |number| 7 | evens << number if number.even? 8 | end 9 | expect(evens).to eq([2, 4, 6, 8, 10]) 10 | end 11 | 12 | it 'test 2' do 13 | numbers = { 14 | one: 1, 15 | two: 2, 16 | three: 3, 17 | four: 4, 18 | five: 5, 19 | } 20 | evens = {} 21 | numbers.each do |name, number| 22 | if number.even? 23 | evens[name] = number 24 | end 25 | end 26 | 27 | expected = { 28 | two: 2, 29 | four: 4 30 | } 31 | expect(evens).to eq(expected) 32 | end 33 | 34 | xit 'test 3' do 35 | rainbow = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] 36 | greater_than_four = [] 37 | rainbow.each do |color| 38 | #Your Code Here 39 | end 40 | 41 | expect(greater_than_four).to eq(["orange", "yellow", "green", "indigo", "violet"]) 42 | end 43 | 44 | xit 'test 4' do 45 | rainbow = { 46 | first: "red", 47 | second: "orange", 48 | third: "yellow", 49 | fourth: "green", 50 | fifth: "blue", 51 | sixth: "indigo", 52 | seventh: "violet" 53 | } 54 | greater_than_four = {} 55 | rainbow.each do |position, color| 56 | #Your Code Here 57 | end 58 | 59 | expected = { 60 | second: "orange", 61 | third: "yellow", 62 | fourth: "green", 63 | sixth: "indigo", 64 | seventh: "violet" 65 | } 66 | expect(greater_than_four).to eq(expected) 67 | end 68 | 69 | xit 'test 5' do 70 | furniture = ["dining table", "bed", "coffee table", "deck chairs"] 71 | words_with_c = [] 72 | #Your Code Here 73 | 74 | expect(words_with_c).to eq(["coffee table", "deck chairs"]) 75 | end 76 | 77 | xit 'test 6' do 78 | furniture = { 79 | dining_room: "dining table", 80 | bedroom: "bed", 81 | living_room: "coffee table", 82 | patio: "deck chairs" 83 | } 84 | words_with_c = {} 85 | #Your Code Here 86 | 87 | expected = { 88 | living_room: "coffee table", 89 | patio: "deck chairs" 90 | } 91 | expect(words_with_c).to eq(expected) 92 | end 93 | 94 | xit 'test 7' do 95 | meals = ["chips and salsa", "chicken alfredo", "banana pudding"] 96 | #Your Code Here 97 | 98 | expect(two_words).to eq(["chicken alfredo", "banana pudding"]) 99 | end 100 | 101 | xit 'test 8' do 102 | meal = { 103 | appetizer: "chips and salsa", 104 | entre: "chicken alfredo", 105 | dessert: "banana pudding" 106 | } 107 | #Your Code Here 108 | 109 | expected = { 110 | entre: "chicken alfredo", 111 | dessert: "banana pudding" 112 | } 113 | expect(two_words).to eq(expected) 114 | end 115 | 116 | 117 | xit 'test 9' do 118 | prices = [3, 1.4, 3.5, 2, 4.9, 9.1, 8.0] 119 | #Your Code Here 120 | 121 | expect(floats).to eq([1.4, 3.5, 4.9, 9.1, 8.0]) 122 | end 123 | 124 | xit 'test 10' do 125 | items = { 126 | tv: 3, 127 | toaster: 1.4, 128 | basketball: 3.5, 129 | bucket: 2, 130 | lint_roller: 4.9, 131 | sack_o_potatoes: 9.1, 132 | tonka_truck: 8.0 133 | } 134 | #Your Code Here 135 | 136 | expected = { 137 | toaster: 1.4, 138 | basketball: 3.5, 139 | lint_roller: 4.9, 140 | sack_o_potatoes: 9.1, 141 | tonka_truck: 8.0 142 | } 143 | expect(floats).to eq(expected) 144 | end 145 | end 146 | 147 | -------------------------------------------------------------------------------- /iteration/exercises/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause 4 | # this file to always be loaded, without a need to explicitly require it in any 5 | # files. 6 | # 7 | # Given that it is always loaded, you are encouraged to keep this file as 8 | # light-weight as possible. Requiring heavyweight dependencies from this file 9 | # will add to the boot time of your test suite on EVERY test run, even for an 10 | # individual file that may not need all of that loaded. Instead, consider making 11 | # a separate helper file that requires the additional dependencies and performs 12 | # the additional setup, and require it from the spec files that actually need 13 | # it. 14 | # 15 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 16 | RSpec.configure do |config| 17 | # rspec-expectations config goes here. You can use an alternate 18 | # assertion/expectation library such as wrong or the stdlib/minitest 19 | # assertions if you prefer. 20 | config.expect_with :rspec do |expectations| 21 | # This option will default to `true` in RSpec 4. It makes the `description` 22 | # and `failure_message` of custom matchers include text for helper methods 23 | # defined using `chain`, e.g.: 24 | # be_bigger_than(2).and_smaller_than(4).description 25 | # # => "be bigger than 2 and smaller than 4" 26 | # ...rather than: 27 | # # => "be bigger than 2" 28 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 29 | end 30 | 31 | # rspec-mocks config goes here. You can use an alternate test double 32 | # library (such as bogus or mocha) by changing the `mock_with` option here. 33 | config.mock_with :rspec do |mocks| 34 | # Prevents you from mocking or stubbing a method that does not exist on 35 | # a real object. This is generally recommended, and will default to 36 | # `true` in RSpec 4. 37 | mocks.verify_partial_doubles = true 38 | end 39 | 40 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 41 | # have no way to turn it off -- the option exists only for backwards 42 | # compatibility in RSpec 3). It causes shared context metadata to be 43 | # inherited by the metadata hash of host groups and examples, rather than 44 | # triggering implicit auto-inclusion in groups with matching metadata. 45 | config.shared_context_metadata_behavior = :apply_to_host_groups 46 | 47 | # The settings below are suggested to provide a good initial experience 48 | # with RSpec, but feel free to customize to your heart's content. 49 | =begin 50 | # This allows you to limit a spec run to individual examples or groups 51 | # you care about by tagging them with `:focus` metadata. When nothing 52 | # is tagged with `:focus`, all examples get run. RSpec also provides 53 | # aliases for `it`, `describe`, and `context` that include `:focus` 54 | # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 55 | config.filter_run_when_matching :focus 56 | 57 | # Allows RSpec to persist some state between runs in order to support 58 | # the `--only-failures` and `--next-failure` CLI options. We recommend 59 | # you configure your source control system to ignore this file. 60 | config.example_status_persistence_file_path = "spec/examples.txt" 61 | 62 | # Limits the available syntax to the non-monkey patched syntax that is 63 | # recommended. For more details, see: 64 | # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ 65 | # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 66 | # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode 67 | config.disable_monkey_patching! 68 | 69 | # This setting enables warnings. It's recommended, but in some cases may 70 | # be too noisy due to issues in dependencies. 71 | config.warnings = true 72 | 73 | # Many RSpec users commonly either run the entire suite or an individual 74 | # file, and it's useful to allow more verbose output when running an 75 | # individual spec file. 76 | if config.files_to_run.one? 77 | # Use the documentation formatter for detailed output, 78 | # unless a formatter has already been configured 79 | # (e.g. via a command-line flag). 80 | config.default_formatter = "doc" 81 | end 82 | 83 | # Print the 10 slowest examples and example groups at the 84 | # end of the spec run, to help surface which specs are running 85 | # particularly slow. 86 | config.profile_examples = 10 87 | 88 | # Run specs in random order to surface order dependencies. If you find an 89 | # order dependency and want to debug it, you can fix the order by providing 90 | # the seed, which is printed after each run. 91 | # --seed 1234 92 | config.order = :random 93 | 94 | # Seed global randomization in this process using the `--seed` CLI option. 95 | # Setting this allows you to use `--seed` to deterministically reproduce 96 | # test failures related to randomization by passing the same `--seed` value 97 | # as the one that triggered the failure. 98 | Kernel.srand config.seed 99 | =end 100 | end 101 | -------------------------------------------------------------------------------- /mythical-creatures/README.md: -------------------------------------------------------------------------------- 1 | # Mythical Creatures 2 | 3 | A collection of exercises to practice Ruby, and test-driven design (TDD). 4 | 5 | Here's a guide that might be helpful in understanding the principles covered in these tests: [josh.works/turing-backend-prep-mythical-creatures](https://josh.works/turing-backend-prep-mythical-creatures). 6 | 7 | ## How to Run 8 | 9 | In order to complete these exercises create a class for each of the mythical creatures in the `spec` directory. 10 | 11 | Navigate to the `mythical-creatures` directory in your terminal, and then run your first test: 12 | 13 | ``` 14 | rspec spec/unicorn_spec.rb 15 | ``` 16 | 17 | If you get an error regarding a certain gem not being installed, you may need to run the following command from your terminal: 18 | 19 | ``` 20 | bundle install 21 | ``` 22 | 23 | There's a video walk-through of the setup and the whole `unicorn_spec.rb` file here: [https://youtu.be/mocwGsu41yw](https://youtu.be/mocwGsu41yw). 24 | 25 | Continue to follow the errors that your test provides until the test passes. Then, unskip the next test by removing the line that says `skip`. Continue until all tests pass for all creatures. 26 | 27 | ## Suggested Order 28 | 29 | * `unicorn_spec.rb` [video walk-through](https://youtu.be/mocwGsu41yw) (watch this first) 30 | * `vampire_spec.rb` 31 | * `dragon_spec.rb` [video walk-through](https://youtu.be/NIPerY-xuCk) 32 | * `hobbit_spec.rb` [video walk-through](https://youtu.be/uYGS-DCNR-0) 33 | * `pirate_spec.rb` 34 | * `wizard_spec.rb` 35 | * `medusa_spec.rb` 36 | * `werewolf_spec.rb` 37 | * `centaur_spec.rb` 38 | * `ogre_spec.rb` 39 | * `direwolf_spec.rb` 40 | * `the_journey_spec.rb` (see below) 41 | 42 | ## Extra Challenges 43 | 44 | ### Testing with RSpec 45 | 46 | 47 | * [How to use RSpec without Rails](https://gist.github.com/ap2322/d8081e38d448acccf2cdc25308be565f) for these exercises. 48 | * [You can check out one of our tutorials about RSpec](http://tutorials.jumpstartlab.com/topics/internal_testing/rspec_and_bdd.html) 49 | or [this blog post](http://gregelizondo.github.io/2014/03/03/getting-started-with-rspec-and-unit-testing.html) 50 | * Pick three of the creatures and implement RSpec tests for each 51 | * For each creature, add at least three additional tests / pieces of functionality 52 | * Submit pull requests adding the RSpec tests to the repository 53 | 54 | ### The Dreaded `if` Statement 55 | 56 | Can you complete implementations of each of the creatures without using `if` 57 | statements? Think about how removing them affects your code. Remember that 58 | a `case` is just a different form of `if`, so don't use it. 59 | 60 | ### Imagine Two Creatures 61 | 62 | Can you add two new creatures to the repository? How about a Hydra? Add rspec 63 | tests exercising some of the following concepts: 64 | 65 | * Passing data into `initialize` 66 | * Using methods to change the internal state of an instance 67 | * Using methods to query the internal state of an instance 68 | * Functionality that necessitates the internal use of an Array 69 | 70 | ### So you feel like going on a journey? 71 | 72 | Are you up for an adventure traveller? This test involves the creation of new creatures as well as calling upon 73 | creatures from times once past. 74 | 75 | To get gold you are required to complete quests that act as mini exercisms; you will see three kinds: 76 | 1. The number of times a letter is different. 77 | 2. Adding up the sum of a number and then squaring it i.e. for 5 => 225 = (1 + 2 + 3 + 4 + 5)** 78 | 3. Turning a num into a roman numeral. 79 | -------------------------------------------------------------------------------- /mythical-creatures/lib/unicorn.rb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turingschool-examples/ruby-exercises/82b46b10008821c252a13bcb948ee5d26be98680/mythical-creatures/lib/unicorn.rb -------------------------------------------------------------------------------- /mythical-creatures/spec/centaur_spec.rb: -------------------------------------------------------------------------------- 1 | require './spec/spec_helper' 2 | require './lib/centaur' 3 | 4 | RSpec.describe Centaur do 5 | it 'has a name' do 6 | centaur = Centaur.new('George', 'Palomino') 7 | expect(centaur.name).to eq('George') 8 | end 9 | 10 | it 'has a horse breed' do 11 | centaur = Centaur.new('George', 'Palomino') 12 | expect(centaur.breed).to eq('Palomino') 13 | end 14 | 15 | it 'has excellent bow skills' do 16 | centaur = Centaur.new('George', 'Palomino') 17 | expect(centaur.shoot).to eq('Twang!!!') 18 | end 19 | 20 | it 'makes a horse sound when it runs' do 21 | centaur = Centaur.new('George', 'Palomino') 22 | expect(centaur.run).to eq('Clop clop clop clop!') 23 | end 24 | 25 | it 'when first created, it is not cranky' do 26 | centaur = Centaur.new('George', 'Palomino') 27 | expect(centaur.cranky?).to be false 28 | end 29 | 30 | it 'when first created, it is standing up' do 31 | centaur = Centaur.new('George', 'Palomino') 32 | expect(centaur.standing?).to be true 33 | end 34 | 35 | it 'gets tired after running or shooting a bow thrice' do 36 | centaur = Centaur.new('George', 'Palomino') 37 | expect(centaur.cranky?).to be false 38 | 39 | centaur.run 40 | centaur.shoot 41 | centaur.run 42 | 43 | expect(centaur.cranky?).to be true 44 | end 45 | 46 | it 'will not shoot a bow when cranky' do 47 | centaur = Centaur.new('George', 'Palomino') 48 | 49 | expect(centaur.cranky?).to be false 50 | 51 | 3.times { centaur.shoot } 52 | 53 | expect(centaur.shoot).to eq('NO!') 54 | end 55 | 56 | it 'will not sleep when it is standing' do 57 | centaur = Centaur.new('George', 'Palomino') 58 | 59 | expect(centaur.sleep).to eq('NO!') 60 | end 61 | 62 | it 'is not standing after laying down' do 63 | centaur = Centaur.new('George', 'Palomino') 64 | centaur.lay_down 65 | 66 | expect(centaur.standing?).to be false 67 | expect(centaur.laying?).to be true 68 | end 69 | 70 | it 'can sleep when laying down' do 71 | centaur = Centaur.new('George', 'Palomino') 72 | centaur.lay_down 73 | expect(centaur.sleep).to_not eq('NO!') 74 | end 75 | 76 | it 'cannot shoot a bow when laying down' do 77 | centaur = Centaur.new('George', 'Palomino') 78 | centaur.lay_down 79 | expect(centaur.shoot).to eq('NO!') 80 | end 81 | 82 | it 'cannot run while laying down' do 83 | centaur = Centaur.new('George', 'Palomino') 84 | centaur.lay_down 85 | expect(centaur.run).to eq('NO!') 86 | end 87 | 88 | it 'can stand up' do 89 | centaur = Centaur.new('George', 'Palomino') 90 | centaur.lay_down 91 | centaur.stand_up 92 | expect(centaur.standing?).to be true 93 | end 94 | 95 | it 'is no longer cranky after sleeping' do 96 | centaur = Centaur.new('George', 'Palomino') 97 | 98 | centaur.shoot 99 | centaur.run 100 | centaur.shoot 101 | 102 | expect(centaur.cranky?).to be true 103 | 104 | centaur.lay_down 105 | centaur.sleep 106 | 107 | expect(centaur.cranky?).to be false 108 | 109 | centaur.stand_up 110 | 111 | expect(centaur.shoot).to eq('Twang!!!') 112 | expect(centaur.run).to eq('Clop clop clop clop!') 113 | end 114 | 115 | it 'becomes rested after drinking a potion' do 116 | # your code here 117 | end 118 | 119 | it 'can only drink a potion whilst standing' do 120 | # your code here 121 | end 122 | 123 | it 'gets stick if a potion is drunk while rested' do 124 | # your code here 125 | end 126 | end 127 | -------------------------------------------------------------------------------- /mythical-creatures/spec/direwolf_spec.rb: -------------------------------------------------------------------------------- 1 | require './spec/spec_helper' 2 | require './lib/direwolf' 3 | 4 | RSpec.describe Direwolf do 5 | it 'has a name' do 6 | wolf = Direwolf.new('Nymeria') 7 | 8 | expect(wolf.name).to eq('Nymeria') 9 | end 10 | 11 | it 'can have a different name and can have a home' do 12 | wolf = Direwolf.new('Lady') 13 | 14 | expect(wolf.home).to eq('Beyond the Wall') 15 | expect(wolf.name).to eq('Lady') 16 | end 17 | 18 | it 'is massive by default' do 19 | wolf = Direwolf.new('Ghost') 20 | 21 | expect(wolf.size).to eq('Massive') 22 | expect(wolf.name).to eq('Ghost') 23 | end 24 | 25 | it 'can have another home and be another size' do 26 | wolf = Direwolf.new('Shaggydog', "Winterfell", "Smol Pupper") 27 | 28 | expect(wolf.name).to eq('Shaggydog') 29 | expect(wolf.home).to eq('Winterfell') 30 | expect(wolf.size).to eq('Smol Pupper') 31 | end 32 | 33 | it 'the Starks are in Winterfell by default' do 34 | wolf = Direwolf.new('Summer', 'Winterfell') 35 | stark = Stark.new('Bran') 36 | 37 | expect(wolf.home).to eq('Winterfell') 38 | expect(stark.location).to eq('Winterfell') 39 | end 40 | 41 | it 'starts off with no Starks to protect' do 42 | wolf = Direwolf.new('Nymeria') 43 | stark = Stark.new('Arya') 44 | 45 | expect(wolf.starks_to_protect).to be_empty 46 | end 47 | 48 | it 'protects the Stark children' do 49 | wolf = Direwolf.new('Nymeria', 'Riverlands') 50 | stark = Stark.new('Arya', 'Riverlands') 51 | 52 | wolf.protects(stark) 53 | 54 | expect(wolf.starks_to_protect.first.name).to eq('Arya') 55 | end 56 | 57 | it 'can only protect the Stark Children if they are in the same location' do 58 | wolf = Direwolf.new('Ghost') 59 | stark = Stark.new('Jon', 'Kings Landing') 60 | 61 | wolf.protects(stark) 62 | 63 | expect(wolf.starks_to_protect).to be_empty 64 | end 65 | 66 | it 'can only protect two Starks at a time' do 67 | summer_wolf = Direwolf.new('Summer', "Winterfell") 68 | lady_wolf = Direwolf.new('Lady', "Winterfell") 69 | sansa_stark = Stark.new('Sansa') 70 | jon_stark = Stark.new('Jon') 71 | rob_stark = Stark.new('Rob') 72 | bran_stark = Stark.new('Bran') 73 | arya_stark = Stark.new('Arya') 74 | 75 | summer_wolf.protects(sansa_stark) 76 | summer_wolf.protects(jon_stark) 77 | lady_wolf.protects(rob_stark) 78 | lady_wolf.protects(bran_stark) 79 | lady_wolf.protects(arya_stark) 80 | 81 | expect(summer_wolf.starks_to_protect).to include(sansa_stark) 82 | expect(summer_wolf.starks_to_protect).to include(jon_stark) 83 | expect(lady_wolf.starks_to_protect).to include(rob_stark) 84 | expect(lady_wolf.starks_to_protect).to include(bran_stark) 85 | expect(lady_wolf.starks_to_protect).to_not include(arya_stark) 86 | end 87 | 88 | it 'the Starks are unsafe by default' do 89 | stark = Stark.new('Jon', 'The Wall') 90 | 91 | expect(stark.safe?).to be false 92 | expect(stark.house_words).to eq('Winter is Coming') 93 | end 94 | 95 | it 'protects the Starks' do 96 | wolf = Direwolf.new('Nymeria', "Winterfell") 97 | arya_stark = Stark.new('Arya') 98 | sansa_stark = Stark.new('Sansa') 99 | 100 | wolf.protects(arya_stark) 101 | 102 | expect(arya_stark.safe?).to be true 103 | expect(sansa_stark.safe?).to be false 104 | end 105 | 106 | it 'hunts white walkers' do 107 | wolf = Direwolf.new('Nymeria', 'Winterfell') 108 | 109 | expect(wolf.hunts_white_walkers?).to be true 110 | end 111 | 112 | it 'will not hunt white walkers when protecting Starks' do 113 | wolf = Direwolf.new('Nymeria', "Winterfell") 114 | arya_stark = Stark.new('Arya') 115 | 116 | wolf.protects(arya_stark) 117 | 118 | expect(wolf.hunts_white_walkers?).to be false 119 | end 120 | 121 | it 'can leave and stop protecting Starks' do 122 | summer_wolf = Direwolf.new('Summer', "Winterfell") 123 | lady_wolf = Direwolf.new('Lady', "Winterfell") 124 | sansa_stark = Stark.new('Sansa') 125 | arya_stark = Stark.new('Arya') 126 | 127 | summer_wolf.protects(arya_stark) 128 | lady_wolf.protects(sansa_stark) 129 | summer_wolf.leaves(arya_stark) 130 | 131 | expect(summer_wolf.starks_to_protect).to be_empty 132 | expect(lady_wolf.starks_to_protect.first.name).to eq('Sansa') 133 | expect(arya_stark.safe?).to be false 134 | end 135 | 136 | it 'returns the Stark object when it leaves' do 137 | summer_wolf = Direwolf.new('Summer', "Winterfell") 138 | lady_wolf = Direwolf.new('Lady', "Winterfell") 139 | sansa_stark = Stark.new('Sansa') 140 | arya_stark = Stark.new('Arya') 141 | rickon_stark = Stark.new('Rickon') 142 | 143 | summer_wolf.protects(arya_stark) 144 | lady_wolf.protects(sansa_stark) 145 | summer_wolf.leaves(arya_stark) 146 | 147 | expected = lady_wolf.leaves(rickon_stark) 148 | 149 | expect(expected.name).to eq('Rickon') 150 | end 151 | 152 | end 153 | -------------------------------------------------------------------------------- /mythical-creatures/spec/dragon_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require './lib/dragon' 3 | 4 | RSpec.describe Dragon do 5 | it 'has a name' do 6 | dragon = Dragon.new('Ramoth', :gold, 'Lessa') 7 | expect(dragon.name).to eq('Ramoth') 8 | end 9 | 10 | it 'has a rider' do 11 | dragon = Dragon.new('Ramoth', :gold, 'Lessa') 12 | expect(dragon.rider).to eq('Lessa') 13 | end 14 | 15 | it 'has a color' do 16 | dragon = Dragon.new('Ramoth', :gold, 'Lessa') 17 | expect(dragon.color).to eq(:gold) 18 | end 19 | 20 | it 'is a different dragon' do 21 | dragon = Dragon.new('Mnementh', :bronze, 'Flar') 22 | expect(dragon.name).to eq('Mnementh') 23 | end 24 | 25 | it 'has a different rider' do 26 | dragon = Dragon.new('Mnementh', :bronze, 'Flar') 27 | expect(dragon.rider).to eq('Flar') 28 | end 29 | 30 | it 'has a different color' do 31 | dragon = Dragon.new('Mnementh', :bronze, 'Flar') 32 | expect(dragon.color).to eq(:bronze) 33 | end 34 | 35 | it 'was born hungry' do 36 | dragon = Dragon.new('Mnementh', :bronze, 'Flar') 37 | expect(dragon.hungry?).to be true 38 | end 39 | 40 | it 'eats a lot' do 41 | dragon = Dragon.new('Mnementh', :bronze, 'Flar') 42 | expect(dragon.hungry?).to be true 43 | dragon.eat 44 | expect(dragon.hungry?).to be true 45 | dragon.eat 46 | expect(dragon.hungry?).to be true 47 | dragon.eat 48 | expect(dragon.hungry?).to be false 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /mythical-creatures/spec/hobbit_spec.rb: -------------------------------------------------------------------------------- 1 | require './spec/spec_helper' 2 | require './lib/hobbit' 3 | 4 | RSpec.describe Hobbit do 5 | it 'has a name' do 6 | hobbit = Hobbit.new('Bilbo') 7 | expect(hobbit.name).to eq('Bilbo') 8 | end 9 | 10 | it 'can have another name' do 11 | hobbit = Hobbit.new('Peregrin') 12 | expect(hobbit.name).to eq('Peregrin') 13 | end 14 | 15 | it 'has an unadventurous disposition' do 16 | hobbit = Hobbit.new('Samwise') 17 | expect(hobbit.disposition).to eq('homebody') 18 | end 19 | 20 | it 'can have a different disposition' do 21 | hobbit = Hobbit.new('Frodo', 'adventurous') 22 | expect(hobbit.disposition).to eq('adventurous') 23 | end 24 | 25 | it 'can grow older when celebrating birthdays' do 26 | hobbit = Hobbit.new('Meriadoc') 27 | expect(hobbit.age).to eq(0) 28 | 29 | 5.times do 30 | hobbit.celebrate_birthday 31 | end 32 | 33 | expect(hobbit.age).to eq(5) 34 | end 35 | 36 | it 'is considered a child at 32' do 37 | hobbit = Hobbit.new('Gerontius') 38 | 39 | 32.times do 40 | hobbit.celebrate_birthday 41 | end 42 | 43 | expect(hobbit.adult?).to be false 44 | end 45 | 46 | it 'comes of age at 33' do 47 | hobbit = Hobbit.new('Otho') 48 | 49 | 33.times do 50 | hobbit.celebrate_birthday 51 | end 52 | 53 | expect(hobbit.adult?).to be true 54 | 55 | # still an adult one year later 56 | hobbit.celebrate_birthday 57 | 58 | expect(hobbit.adult?).to be true 59 | end 60 | 61 | xit 'is old at the age of 101' do 62 | # create a hobbit 63 | # have hobbit age 101 years 64 | # check that hobbit.old? returns true 65 | end 66 | 67 | xit 'it has the ring if its name is Frodo' do 68 | # create a hobbit named Frodo 69 | # create a second hobbit named Sam 70 | # check that .has_ring? for Frodo returns true 71 | # check that .has_ring? for Sam returns false 72 | end 73 | 74 | xit 'they are short' do 75 | # create a hobbit 76 | # check that is_short? returns true 77 | end 78 | end 79 | -------------------------------------------------------------------------------- /mythical-creatures/spec/lovisa_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require './lib/lovisa' 3 | 4 | RSpec.describe Lovisa do 5 | it 'she has a title' do 6 | lovisa = Lovisa.new('Lovisa the Swedish Goddess') 7 | expect(lovisa.title).to eq('Lovisa the Swedish Goddess') 8 | end 9 | 10 | it 'she is brilliant by default' do 11 | lovisa = Lovisa.new('Lovisa the Mentor') 12 | expect(lovisa.characteristics).to eq(['brilliant']) 13 | expect(lovisa.brilliant?).to eq(true) 14 | expect(lovisa.brilliant?).to be true 15 | end 16 | 17 | it "she is more than brilliant" do 18 | loivsa = Lovisa.new('Lovisa the friend', ['brilliant', 'kind']) 19 | expect(loivsa.characteristics).to eq(['brilliant', 'kind']) 20 | expect(loivsa.brilliant?).to eq(true) 21 | expect(loivsa.brilliant?).to be true 22 | expect(loivsa.kind?).to eq(true) 23 | expect(loivsa.kind?).to be true 24 | end 25 | 26 | it 'she says sparkly stuff' do 27 | loivsa = Lovisa.new('Lovisa the Loved') 28 | expect(loivsa.say('Wonderful!')).to eq('**;* Wonderful! **;*') 29 | expect(loivsa.say('You are doing great!')).to eq('**;* You are doing great! **;*') 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /mythical-creatures/spec/medusa_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require './lib/medusa' 3 | 4 | RSpec.describe Medusa do 5 | it 'has a name' do 6 | medusa = Medusa.new('Cassiopeia') 7 | expect(medusa.name).to eq('Cassiopeia') 8 | end 9 | 10 | it 'has no statues when created' do 11 | medusa = Medusa.new('Cassiopeia') 12 | expect(medusa.statues).to be_empty 13 | end 14 | 15 | it 'gains a statue when staring at a person' do 16 | medusa = Medusa.new('Cassiopeia') 17 | victim = Person.new('Perseus') 18 | 19 | medusa.stare(victim) 20 | expect(medusa.statues.count).to eq(1) 21 | expect(medusa.statues.first.name).to eq('Perseus') 22 | expect(medusa.statues.first).to be_an_instance_of(Person) 23 | end 24 | 25 | it 'turns a person to stone when staring at them' do 26 | medusa = Medusa.new('Cassiopeia') 27 | victim = Person.new('Perseus') 28 | 29 | expect(victim.stoned?).to be false 30 | medusa.stare(victim) 31 | expect(victim.stoned?).to be true 32 | end 33 | 34 | it 'can only have three victims' do 35 | # your code here 36 | end 37 | 38 | it 'if a fourth victim is stoned the first is unstoned' do 39 | # your code here 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /mythical-creatures/spec/ogre_spec.rb: -------------------------------------------------------------------------------- 1 | require './spec/spec_helper' 2 | require './lib/ogre' 3 | 4 | RSpec.describe Ogre do 5 | it 'has a name' do 6 | ogre = Ogre.new('Brak') 7 | expect(ogre.name).to eq('Brak') 8 | end 9 | 10 | it 'lives somewhere by default' do 11 | ogre = Ogre.new('Brak') 12 | expect(ogre.home).to eq('Swamp') 13 | end 14 | 15 | it 'doesnt have to live in a swamp' do 16 | ogre = Ogre.new('Brak', 'Castle') 17 | 18 | expect(ogre.home).to eq('Castle') 19 | end 20 | 21 | it 'can meets humans' do 22 | ogre = Ogre.new('Brak') 23 | human = Human.new 24 | expect(human.name).to eq('Jane') 25 | 26 | ogre.encounter(human) 27 | 28 | expect(human.encounter_counter).to eq(1) 29 | end 30 | 31 | it 'is noticed by humans every third encounter' do 32 | ogre = Ogre.new('Brak') 33 | human = Human.new 34 | 35 | ogre.encounter(human) 36 | ogre.encounter(human) 37 | expect(human.notices_ogre?).to be false 38 | 39 | ogre.encounter(human) 40 | 41 | expect(human.notices_ogre?).to be true 42 | end 43 | 44 | it 'is noticed by humans the sixth time' do 45 | ogre = Ogre.new('Brak') 46 | human = Human.new 47 | 48 | 6.times { ogre.encounter(human) } 49 | 50 | expect(human.notices_ogre?).to be true 51 | end 52 | 53 | it 'can swing a club' do 54 | ogre = Ogre.new('Brak') 55 | human = Human.new 56 | 57 | ogre.swing_at(human) 58 | 59 | expect(ogre.swings).to eq(1) 60 | end 61 | 62 | it 'swings its club when noticed by a human' do 63 | ogre = Ogre.new('Brak') 64 | human = Human.new 65 | ogre.encounter(human) 66 | 67 | expect(ogre.swings).to eq(0) 68 | 69 | ogre.encounter(human) 70 | ogre.encounter(human) 71 | 72 | expect(ogre.swings).to eq(1) 73 | expect(human.notices_ogre?).to be true 74 | end 75 | 76 | it 'hits the human every second time it swings' do 77 | ogre = Ogre.new('Brak') 78 | human = Human.new 79 | 80 | 6.times { ogre.encounter(human) } 81 | 82 | expect(human.encounter_counter).to eq(6) 83 | expect(ogre.swings).to eq(2) 84 | expect(human.knocked_out?).to be true 85 | end 86 | 87 | it 'apologizes and the human wakes up' do 88 | ogre = Ogre.new('Brak') 89 | human = Human.new 90 | 91 | 6.times { ogre.encounter(human) } 92 | 93 | expect(human.knocked_out?).to be true 94 | 95 | ogre.apologize(human) 96 | expect(human.knocked_out?).to be false 97 | end 98 | end 99 | -------------------------------------------------------------------------------- /mythical-creatures/spec/pirate_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require './lib/pirate' 3 | 4 | RSpec.describe Pirate do 5 | it 'has a name' do 6 | pirate = Pirate.new('Jane') 7 | expect(pirate.name).to eq('Jane') 8 | end 9 | 10 | it 'can have a different name' do 11 | pirate = Pirate.new('Blackbeard') 12 | expect(pirate.name).to eq('Blackbeard') 13 | end 14 | 15 | it 'is a scallywag by default' do 16 | pirate = Pirate.new('Jane') 17 | expect(pirate.job).to eq('Scallywag') 18 | end 19 | 20 | it 'is not always a scallywag' do 21 | pirate = Pirate.new('Jack', 'cook') 22 | expect(pirate.job).to eq('cook') 23 | end 24 | 25 | it 'is not cursed by default' do 26 | pirate = Pirate.new('Jack') 27 | 28 | expect(pirate.cursed?).to be false 29 | 30 | pirate.commit_heinous_act 31 | expect(pirate.cursed?).to be false 32 | 33 | pirate.commit_heinous_act 34 | expect(pirate.cursed?).to be false 35 | 36 | pirate.commit_heinous_act 37 | expect(pirate.cursed?).to be true 38 | end 39 | 40 | it 'has a booty' do 41 | # create a pirate 42 | # check that the pirate starts with 0 booty 43 | end 44 | 45 | it 'gets 100 booty for robbing a ship' do 46 | # create a pirate 47 | # rob some ships 48 | # check that the pirate got 100 booty for each ship it robbed 49 | end 50 | 51 | end 52 | -------------------------------------------------------------------------------- /mythical-creatures/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause 4 | # this file to always be loaded, without a need to explicitly require it in any 5 | # files. 6 | # 7 | # Given that it is always loaded, you are encouraged to keep this file as 8 | # light-weight as possible. Requiring heavyweight dependencies from this file 9 | # will add to the boot time of your test suite on EVERY test run, even for an 10 | # individual file that may not need all of that loaded. Instead, consider making 11 | # a separate helper file that requires the additional dependencies and performs 12 | # the additional setup, and require it from the spec files that actually need 13 | # it. 14 | # 15 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 16 | RSpec.configure do |config| 17 | # rspec-expectations config goes here. You can use an alternate 18 | # assertion/expectation library such as wrong or the stdlib/minitest 19 | # assertions if you prefer. 20 | config.expect_with :rspec do |expectations| 21 | # This option will default to `true` in RSpec 4. It makes the `description` 22 | # and `failure_message` of custom matchers include text for helper methods 23 | # defined using `chain`, e.g.: 24 | # be_bigger_than(2).and_smaller_than(4).description 25 | # # => "be bigger than 2 and smaller than 4" 26 | # ...rather than: 27 | # # => "be bigger than 2" 28 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 29 | end 30 | 31 | # rspec-mocks config goes here. You can use an alternate test double 32 | # library (such as bogus or mocha) by changing the `mock_with` option here. 33 | config.mock_with :rspec do |mocks| 34 | # Prevents you from mocking or stubbing a method that does not exist on 35 | # a real object. This is generally recommended, and will default to 36 | # `true` in RSpec 4. 37 | mocks.verify_partial_doubles = true 38 | end 39 | 40 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 41 | # have no way to turn it off -- the option exists only for backwards 42 | # compatibility in RSpec 3). It causes shared context metadata to be 43 | # inherited by the metadata hash of host groups and examples, rather than 44 | # triggering implicit auto-inclusion in groups with matching metadata. 45 | config.shared_context_metadata_behavior = :apply_to_host_groups 46 | 47 | # The settings below are suggested to provide a good initial experience 48 | # with RSpec, but feel free to customize to your heart's content. 49 | =begin 50 | # This allows you to limit a spec run to individual examples or groups 51 | # you care about by tagging them with `:focus` metadata. When nothing 52 | # is tagged with `:focus`, all examples get run. RSpec also provides 53 | # aliases for `it`, `describe`, and `context` that include `:focus` 54 | # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 55 | config.filter_run_when_matching :focus 56 | 57 | # Allows RSpec to persist some state between runs in order to support 58 | # the `--only-failures` and `--next-failure` CLI options. We recommend 59 | # you configure your source control system to ignore this file. 60 | config.example_status_persistence_file_path = "spec/examples.txt" 61 | 62 | # Limits the available syntax to the non-monkey patched syntax that is 63 | # recommended. For more details, see: 64 | # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ 65 | # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 66 | # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode 67 | config.disable_monkey_patching! 68 | 69 | # This setting enables warnings. It's recommended, but in some cases may 70 | # be too noisy due to issues in dependencies. 71 | config.warnings = true 72 | 73 | # Many RSpec users commonly either run the entire suite or an individual 74 | # file, and it's useful to allow more verbose output when running an 75 | # individual spec file. 76 | if config.files_to_run.one? 77 | # Use the documentation formatter for detailed output, 78 | # unless a formatter has already been configured 79 | # (e.g. via a command-line flag). 80 | config.default_formatter = "doc" 81 | end 82 | 83 | # Print the 10 slowest examples and example groups at the 84 | # end of the spec run, to help surface which specs are running 85 | # particularly slow. 86 | config.profile_examples = 10 87 | 88 | # Run specs in random order to surface order dependencies. If you find an 89 | # order dependency and want to debug it, you can fix the order by providing 90 | # the seed, which is printed after each run. 91 | # --seed 1234 92 | config.order = :random 93 | 94 | # Seed global randomization in this process using the `--seed` CLI option. 95 | # Setting this allows you to use `--seed` to deterministically reproduce 96 | # test failures related to randomization by passing the same `--seed` value 97 | # as the one that triggered the failure. 98 | Kernel.srand config.seed 99 | =end 100 | end 101 | -------------------------------------------------------------------------------- /mythical-creatures/spec/unicorn_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require './lib/unicorn' 3 | 4 | RSpec.describe Unicorn do 5 | it 'has a name' do 6 | unicorn = Unicorn.new('Robert') 7 | expect(unicorn.name).to eq('Robert') 8 | end 9 | 10 | it 'is silver by default' do 11 | unicorn = Unicorn.new('Margaret') 12 | expect(unicorn.color).to eq('silver') 13 | expect(unicorn.silver?).to eq(true) 14 | expect(unicorn.silver?).to be true 15 | end 16 | 17 | it 'doesnt have to be silver' do 18 | unicorn = Unicorn.new('Barbara', 'purple') 19 | expect(unicorn.color).to eq('purple') 20 | expect(unicorn.silver?).to eq(false) 21 | expect(unicorn.silver?).to be false 22 | end 23 | 24 | it 'says sparkly stuff' do 25 | unicorn = Unicorn.new('Johnny') 26 | expect(unicorn.say('Wonderful!')).to eq('**;* Wonderful! **;*') 27 | expect(unicorn.say('I dont like you very much.')).to eq('**;* I dont like you very much. **;*') 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /mythical-creatures/spec/vampire_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require './lib/vampire' 3 | 4 | RSpec.describe Vampire do 5 | it 'has a name' do 6 | vampire = Vampire.new('Dracula') 7 | expect(vampire.name).to eq('Dracula') 8 | end 9 | 10 | it 'it can be named something else' do 11 | vampire = Vampire.new('Vladimir') 12 | expect(vampire.name).to eq('Vladimir') 13 | end 14 | 15 | it 'keeps a pet bat by default' do 16 | vampire = Vampire.new('Ruthven') 17 | expect(vampire.pet).to eq('bat') 18 | end 19 | 20 | it 'can keep other pets' do 21 | vampire = Vampire.new('Varney', 'fox') 22 | expect(vampire.pet).to eq('fox') 23 | end 24 | 25 | it 'is thirsty by default' do 26 | vampire = Vampire.new('The Count') 27 | 28 | expect(vampire.thirsty).to be true 29 | end 30 | 31 | it 'is not thirsty after drinking' do 32 | vampire = Vampire.new('Elizabeth Bathory') 33 | 34 | vampire.drink 35 | expect(vampire.thirsty).to be false 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /mythical-creatures/spec/werewolf_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require './lib/werewolf' 3 | 4 | RSpec.describe Werewolf do 5 | it 'has a name' do 6 | werewolf = Werewolf.new('David') 7 | expect(werewolf.name).to eq('David') 8 | end 9 | 10 | it 'has a location' do 11 | werewolf = Werewolf.new('David', 'London') 12 | expect(werewolf.location).to eq('London') 13 | end 14 | 15 | it 'is by default human' do 16 | werewolf = Werewolf.new('David', 'London') 17 | expect(werewolf.human?).to be true 18 | end 19 | 20 | it 'when starting as a human, changing makes it turn into a werewolf' do 21 | werewolf = Werewolf.new('David', 'London') 22 | werewolf.change! 23 | expect(werewolf.wolf?).to be true 24 | expect(werewolf.human?).to be false 25 | end 26 | 27 | it 'when starting as a human, changing again makes it be human again' do 28 | werewolf = Werewolf.new('David', 'London') 29 | expect(werewolf.human?).to be true 30 | 31 | werewolf.change! 32 | 33 | expect(werewolf.human?).to be false 34 | 35 | werewolf.change! 36 | 37 | expect(werewolf.human?).to be true 38 | end 39 | 40 | it 'when starting as a werewolf, changing a second time makes it a werewolf' do 41 | werewolf = Werewolf.new('David', 'London') 42 | 43 | werewolf.change! 44 | expect(werewolf.wolf?).to be true 45 | 46 | werewolf.change! 47 | werewolf.change! 48 | 49 | expect(werewolf.wolf?).to be true 50 | end 51 | 52 | it 'is not hungry by default' do 53 | # your code here 54 | end 55 | 56 | it 'becomes hungry after changing to a werewolf' do 57 | # your code here 58 | end 59 | 60 | class Victim 61 | attr_accessor :status 62 | 63 | def initialize 64 | @status = :alive 65 | end 66 | end 67 | 68 | it 'consumes a victim' do 69 | # your code here 70 | end 71 | 72 | it 'cannot consume a victim if it is in human form' do 73 | # your code here 74 | end 75 | 76 | it 'a werewolf that has consumed a human being is no longer hungry' do 77 | # your code here 78 | end 79 | 80 | it 'a werewolf who has consumed a victim makes the victim dead' do 81 | # your code here 82 | end 83 | 84 | end 85 | -------------------------------------------------------------------------------- /mythical-creatures/spec/wizard_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require './lib/wizard' 3 | 4 | RSpec.describe Wizard do 5 | it 'has a name' do 6 | wizard = Wizard.new('Eric') 7 | expect(wizard.name).to eq('Eric') 8 | end 9 | 10 | it 'has a different name' do 11 | wizard = Wizard.new('Alex') 12 | expect(wizard.name).to eq('Alex') 13 | end 14 | 15 | it 'is bearded by default' do 16 | wizard = Wizard.new('Ben') 17 | expect(wizard.bearded?).to be true 18 | end 19 | 20 | it 'is not always bearded' do 21 | wizard = Wizard.new('Valerie', bearded: false) 22 | expect(wizard.bearded?).to be false 23 | end 24 | 25 | it 'has root powers' do 26 | wizard = Wizard.new('Stella', bearded: false) 27 | expect(wizard.incantation('chown ~/bin')).to eq('sudo chown ~/bin') 28 | end 29 | 30 | it 'has many root powers' do 31 | wizard = Wizard.new('Sal', bearded: true) 32 | expect(wizard.incantation('rm -rf /home/mirandax')).to eq('sudo rm -rf /home/mirandax') 33 | end 34 | 35 | it 'starts rested' do 36 | # create wizard 37 | # .rested? returns true 38 | end 39 | 40 | it 'can cast spells' do 41 | # create wizard 42 | # .cast returns "MAGIC MISSILE!" 43 | end 44 | 45 | it 'gets tired after casting three spells' do 46 | # create wizard 47 | # casts spell twice 48 | # check if wizard is rested 49 | # casts spell 50 | # check wizard is not rested 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /objects-and-methods/README.md: -------------------------------------------------------------------------------- 1 | # Objects and Methods 2 | 3 | ## Exercise 1 4 | 5 | If you'd like to be walked through the exercise, check out the [Objects and Methods](http://tutorials.jumpstartlab.com/academy/workshops/objects_and_methods.html) workshop on the Jumpstart Lab tutorials site. 6 | 7 | Make the tests pass in the following sequence: 8 | 9 | * `spec/candy_spec.rb` 10 | * `spec/bag_spec.rb` 11 | * `spec/costume_spec.rb` 12 | * `spec/trick_or_treater_spec.rb` 13 | 14 | Run a test file by calling it with `ruby`: 15 | 16 | ```bash 17 | $ rspec spec/bag_spec.rb 18 | ``` 19 | 20 | ## Exercise 2 21 | 22 | Each object has become a little bit more complex. 23 | 24 | Make the tests pass in the following sequence: 25 | 26 | * `spec/candy_spec.rb` 27 | * `spec/bag_spec.rb` 28 | * `spec/costume_spec.rb` 29 | * `spec/trick_or_treater_spec.rb` 30 | 31 | -------------------------------------------------------------------------------- /objects-and-methods/exercise-1/lib/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turingschool-examples/ruby-exercises/82b46b10008821c252a13bcb948ee5d26be98680/objects-and-methods/exercise-1/lib/.keep -------------------------------------------------------------------------------- /objects-and-methods/exercise-1/spec/bag_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/bag' 3 | require_relative '../lib/candy' 4 | 5 | RSpec.describe Bag do 6 | it 'is empty' do 7 | expect(Bag.new.empty?).to be true 8 | end 9 | 10 | xit 'can count the candy in an empty bag' do 11 | expect(Bag.new.count).to eq(0) 12 | end 13 | 14 | xit 'has no candies when it is empty' do 15 | expect(Bag.new.candies).to eq([]) 16 | end 17 | 18 | xit 'can put a candy in a bag' do 19 | bag = Bag.new 20 | 21 | candy = Candy.new('Sour frogs') 22 | 23 | bag << candy 24 | 25 | expect(bag.candies).to eq([candy]) 26 | end 27 | 28 | xit 'is not empty when it has candies' do 29 | bag = Bag.new 30 | bag << Candy.new("Nerds") 31 | 32 | expect(bag.empty?).to be false 33 | end 34 | 35 | xit 'can count candies' do 36 | bag = Bag.new 37 | bag << Candy.new("Caramelized Almonds") 38 | 39 | expect(bag.count).to eq(1) 40 | end 41 | 42 | xit 'contains candies and candies have a type' do 43 | bag = Bag.new 44 | bag << Candy.new("Hershey's Kisses") 45 | # You usually don't want to chain a bunch of different 46 | # types of things together like this. 47 | # We'll talk about it more in a few weeks. 48 | # It's important to understand how these methods work, though. 49 | type = bag.candies.first.type 50 | 51 | expect(type).to eq("Hershey's Kisses") 52 | end 53 | 54 | xit 'can be asked if it has a particular kind of candy' do 55 | bag = Bag.new 56 | bag << Candy.new("Lindt chocolate") 57 | 58 | expect(bag.contains?('Lindt chocolate')).to be true 59 | expect(bag.contains?('Nerds')).to be false 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /objects-and-methods/exercise-1/spec/candy_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/candy' 3 | 4 | RSpec.describe Candy do 5 | it 'has a type' do 6 | candy = Candy.new('Skittles') 7 | 8 | expect(candy.type).to eq('Skittles') 9 | end 10 | 11 | xit 'has a different kind of candy' do 12 | candy = Candy.new('Snickers') 13 | 14 | expect(candy.type).to eq('Snickers') 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /objects-and-methods/exercise-1/spec/costume_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/costume' 3 | 4 | RSpec.describe Costume do 5 | it 'has a style' do 6 | costume = Costume.new('Dragon') 7 | 8 | expect(costume.style).to eq('Dragon') 9 | end 10 | 11 | xit 'has a different style of costume' do 12 | costume = Costume.new('Princess') 13 | 14 | expect(costume.style).to eq('Princess') 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /objects-and-methods/exercise-1/spec/trick_or_treater_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/costume' 3 | require_relative '../lib/bag' 4 | require_relative '../lib/candy' 5 | require_relative '../lib/trick_or_treater' 6 | 7 | RSpec.describe TrickOrTreater do 8 | it 'wears a costume' do 9 | costume = Costume.new('Cowboy') 10 | trick_or_treater = TrickOrTreater.new(costume) 11 | expect(trick_or_treater.dressed_up_as).to eq('Cowboy') 12 | end 13 | 14 | xit 'can have a different costume' do 15 | trick_or_treater = TrickOrTreater.new(Costume.new('Alien')) 16 | 17 | expect(trick_or_treater.dressed_up_as).to eq('Alien') 18 | end 19 | 20 | xit 'has an empty bag by default' do 21 | trick_or_treater = TrickOrTreater.new(Costume.new("Alien")) 22 | 23 | expect(trick_or_treater.bag.empty?).to be true 24 | end 25 | 26 | xit 'has an empty bag, so no candies' do 27 | trick_or_treater = TrickOrTreater.new(Costume.new("Knight")) 28 | 29 | expect(trick_or_treater.has_candy?).to be false 30 | end 31 | 32 | xit 'can get candies' do 33 | trick_or_treater = TrickOrTreater.new(Costume.new('Spaceship Mechanic')) 34 | trick_or_treater.bag << Candy.new('Gummy bears') 35 | 36 | expect(trick_or_treater.has_candy?).to be true 37 | end 38 | 39 | xit 'it can count candies' do 40 | trick_or_treater = TrickOrTreater.new(Costume.new('Spaceship Mechanic')) 41 | 42 | expect(trick_or_treater.candy_count).to eq(0) 43 | 44 | trick_or_treater.bag << Candy.new('Gummy bears') 45 | 46 | expect(trick_or_treater.candy_count).to eq(1) 47 | end 48 | 49 | xit 'can eat candies' do 50 | trick_or_treater = TrickOrTreater.new(Costume.new("Baron")) 51 | trick_or_treater.bag << Candy.new("Gummy worms") 52 | trick_or_treater.bag << Candy.new("Liquorice") 53 | trick_or_treater.bag << Candy.new("Salty Serpents") 54 | 55 | expect(trick_or_treater.candy_count).to eq(3) 56 | trick_or_treater.eat 57 | 58 | expect(trick_or_treater.candy_count).to eq(2) 59 | trick_or_treater.eat 60 | 61 | expect(trick_or_treater.candy_count).to eq(1) 62 | trick_or_treater.eat 63 | 64 | expect(trick_or_treater.candy_count).to eq(0) 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /objects-and-methods/exercise-2/lib/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/turingschool-examples/ruby-exercises/82b46b10008821c252a13bcb948ee5d26be98680/objects-and-methods/exercise-2/lib/.keep -------------------------------------------------------------------------------- /objects-and-methods/exercise-2/spec/bag_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/bag' 3 | require_relative '../lib/candy' 4 | 5 | RSpec.describe Bag do 6 | it 'is empty' do 7 | expect(Bag.new.empty?).to be true 8 | end 9 | 10 | xit 'can count the candy in an empty bag' do 11 | expect(Bag.new.count).to eq(0) 12 | end 13 | 14 | xit 'has no candies when it is empty' do 15 | expect(Bag.new.candies).to eq([]) 16 | end 17 | 18 | xit 'can put a candy in a bag' do 19 | bag = Bag.new 20 | 21 | candy = Candy.new('Sour frogs') 22 | 23 | bag << candy 24 | 25 | expect(bag.candies).to eq([candy]) 26 | end 27 | 28 | xit 'is not empty when it has candies' do 29 | bag = Bag.new 30 | bag << Candy.new('Nerds') 31 | 32 | expect(bag.empty?).to be false 33 | end 34 | 35 | xit 'can count candies' do 36 | bag = Bag.new 37 | bag << Candy.new('Caramelized Almonds') 38 | 39 | expect(bag.count).to eq(1) 40 | end 41 | 42 | xit 'contains candies and candies have a type' do 43 | bag = Bag.new 44 | bag << Candy.new('Hersheys Kisses') 45 | # You usually don't want to chain a bunch of different 46 | # types of things together like this. 47 | # We'll talk about it more in a few weeks. 48 | # It's important to understand how these methods work, though. 49 | type = bag.candies.first.type 50 | 51 | expect(type).to eq('Hersheys Kisses') 52 | end 53 | 54 | xit 'can be asked if it has a particular kind of candy' do 55 | bag = Bag.new 56 | bag << Candy.new('Lindt chocolate') 57 | 58 | expect(bag.contains?('Lindt chocolate')).to be true 59 | expect(bag.contains?('Nerds')).to be false 60 | end 61 | 62 | xit 'can get a particular type of candy' do 63 | bag = Bag.new 64 | bag << Candy.new('Jawbreaker') 65 | bag << Candy.new('Jawbreaker') 66 | bag << Candy.new('Jolly Ranchers') 67 | 68 | candy = bag.grab "Jawbreaker" 69 | expect(candy.type).to eq('Jawbreaker') 70 | end 71 | 72 | xit 'removes candy from the bag when you grab it' do 73 | bag = Bag.new 74 | bag << Candy.new('Reeses Pieces') 75 | bag << Candy.new('Junior Mints') 76 | bag << Candy.new('Reeses Pieces') 77 | 78 | expect(bag.count).to eq(3) 79 | 80 | bag.grab('Junior Mints') 81 | 82 | expect(bag.count).to eq(2) 83 | end 84 | 85 | xit 'can take a number of candies from the bag' do 86 | bag = Bag.new 87 | bag << Candy.new('Swedish Fish') 88 | bag << Candy.new('Milky Way') 89 | bag << Candy.new('Cotton Candy') 90 | 91 | expect(bag.count).to eq(3) 92 | 93 | taken = bag.take(2) 94 | 95 | expect(taken.size).to eq(2) 96 | expect(bag.count).to eq(1) 97 | end 98 | 99 | xit 'can take one candy' do 100 | bag = Bag.new 101 | bag << Candy.new('Lifesavers') 102 | 103 | candy = bag.take(1) 104 | expect(candy.type).to eq('Lifesavers') 105 | end 106 | end 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /objects-and-methods/exercise-2/spec/candy_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/candy' 3 | 4 | RSpec.describe Candy do 5 | it 'has a type' do 6 | candy = Candy.new('Skittles') 7 | 8 | expect(candy.type).to eq('Skittles') 9 | end 10 | 11 | xit 'can have a different type' do 12 | candy = Candy.new('Mars') 13 | 14 | expect(candy.type).to eq('Mars') 15 | end 16 | 17 | xit 'has 100g of sugar by default' do 18 | candy = Candy.new('Smarties') 19 | 20 | expect(candy.sugar).to eq(100) 21 | end 22 | 23 | xit 'can be created with a different amount of sugar' do 24 | candy = Candy.new('Pop Rocks', 75) 25 | 26 | expect(candy.sugar).to eq(75) 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /objects-and-methods/exercise-2/spec/costume_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/costume' 3 | 4 | RSpec.describe Costume do 5 | it 'has a style' do 6 | costume = Costume.new('Dragon') 7 | 8 | expect(costume.style).to eq('Dragon') 9 | end 10 | 11 | xit 'has a different style of costume' do 12 | costume = Costume.new('Princess') 13 | 14 | expect(costume.style).to eq('Princess') 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /objects-and-methods/exercise-2/spec/trick_or_treater_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative '../lib/costume' 3 | require_relative '../lib/bag' 4 | require_relative '../lib/candy' 5 | require_relative '../lib/trick_or_treater' 6 | 7 | RSpec.describe TrickOrTreater do 8 | it 'wears a costume' do 9 | costume = Costume.new('Cowboy') 10 | trick_or_treater = TrickOrTreater.new(costume) 11 | expect(trick_or_treater.dressed_up_as).to eq('Cowboy') 12 | end 13 | 14 | xit 'can have a different costume' do 15 | trick_or_treater = TrickOrTreater.new(Costume.new('Alien')) 16 | 17 | expect(trick_or_treater.dressed_up_as).to eq('Alien') 18 | end 19 | 20 | xit 'has an empty bag by default' do 21 | trick_or_treater = TrickOrTreater.new(Costume.new("Alien")) 22 | 23 | expect(trick_or_treater.bag.empty?).to be true 24 | end 25 | 26 | xit 'has an empty bag, so no candies' do 27 | trick_or_treater = TrickOrTreater.new(Costume.new("Knight")) 28 | 29 | expect(trick_or_treater.has_candy?).to be false 30 | end 31 | 32 | xit 'can get candies' do 33 | trick_or_treater = TrickOrTreater.new(Costume.new('Spaceship Mechanic')) 34 | trick_or_treater.bag << Candy.new('Gummy bears') 35 | 36 | expect(trick_or_treater.has_candy?).to be true 37 | end 38 | 39 | xit 'it can count candies' do 40 | trick_or_treater = TrickOrTreater.new(Costume.new('Spaceship Mechanic')) 41 | 42 | expect(trick_or_treater.candy_count).to eq(0) 43 | 44 | trick_or_treater.bag << Candy.new('Gummy bears') 45 | 46 | expect(trick_or_treater.candy_count).to eq(1) 47 | end 48 | 49 | xit 'can eat candies' do 50 | trick_or_treater = TrickOrTreater.new(Costume.new('Baron')) 51 | trick_or_treater.bag << Candy.new('Gummy worms') 52 | trick_or_treater.bag << Candy.new('Liquorice') 53 | trick_or_treater.bag << Candy.new('Salty Serpents') 54 | 55 | expect(trick_or_treater.candy_count).to eq(3) 56 | trick_or_treater.eat 57 | 58 | expect(trick_or_treater.candy_count).to eq(2) 59 | trick_or_treater.eat 60 | 61 | expect(trick_or_treater.candy_count).to eq(1) 62 | trick_or_treater.eat 63 | 64 | expect(trick_or_treater.candy_count).to eq(0) 65 | end 66 | 67 | xit 'has a sugar level that starts at 0' do 68 | trick_or_treater = TrickOrTreater.new(Costume.new('Hobbit')) 69 | 70 | expect(trick_or_treater.sugar_level).to eq(0) 71 | end 72 | 73 | xit 'increases the sugar level when it eats candies' do 74 | trick_or_treater = TrickOrTreater.new(Costume.new('Hobbit')) 75 | 76 | trick_or_treater.bag << Candy.new('Gummy worms', 88) 77 | trick_or_treater.bag << Candy.new('Liquorice', 83) 78 | trick_or_treater.bag << Candy.new('Salty Serpents', 71) 79 | 80 | trick_or_treater.eat 81 | trick_or_treater.eat 82 | trick_or_treater.eat 83 | 84 | expect(trick_or_treater.sugar_level).to eq(242) 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /problem-solving/.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /problem-solving/README.md: -------------------------------------------------------------------------------- 1 | Suggested Order of Completion 2 | 3 | * `hello_spec.rb` 4 | * `character_count_spec.rb` 5 | * `escape_characters_spec.rb` 6 | * `mad_lib_spec.rb` 7 | * `simple_math_spec.rb` 8 | * `retirement_spec.rb` 9 | -------------------------------------------------------------------------------- /problem-solving/spec/character_count_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'character count' do 2 | 3 | it 'can count a small word' do 4 | cc = CharacterCount.new 5 | 6 | result = cc.count("test") 7 | expected = 4 8 | 9 | expect(result).to eq(expected) 10 | end 11 | 12 | it 'can count another word' do 13 | cc = CharacterCount.new 14 | 15 | result = cc.count("pterodactyl") 16 | expected = 11 17 | 18 | expect(result).to eq(expected) 19 | end 20 | 21 | it 'can count spaces' do 22 | cc = CharacterCount.new 23 | 24 | result = cc.count(" ") 25 | expected = 1 26 | 27 | expect(result).to eq(expected) 28 | end 29 | 30 | it 'can handle sentences' do 31 | cc = CharacterCount.new 32 | 33 | result = cc.count("hello world") 34 | expected = 11 35 | 36 | expect(result).to eq(expected) 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /problem-solving/spec/escape_characters_spec.rb: -------------------------------------------------------------------------------- 1 | require "./lib/escape_characters" 2 | 3 | RSpec.describe EscapeCharacters do 4 | 5 | it 'can add quotation marks' do 6 | ec = EscapeCharacters.new 7 | 8 | result = ec.quote("These aren't the droids you're looking for.") 9 | expected = "\"These aren't the droids you're looking for.\"" 10 | 11 | expect(result).to eq(expected) 12 | end 13 | 14 | it 'can add quotation marks to something else' do 15 | ec = EscapeCharacters.new 16 | 17 | result = ec.quote("By Horace's Beard!") 18 | expected = "\"By Horace's Beard!\"" 19 | 20 | expect(result).to eq(expected) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /problem-solving/spec/hello_spec.rb: -------------------------------------------------------------------------------- 1 | require "./lib/hello" 2 | 3 | RSpec.describe Hello do 4 | 5 | it 'says hello to Brian' do 6 | hello = Hello.new 7 | 8 | result = hello.greet("Brian") 9 | expected = "Hello, Brian, nice to meet you!" 10 | 11 | expect(result).to eq(expected) 12 | end 13 | 14 | it 'says hello to Jeff' do 15 | hello = Hello.new 16 | 17 | result = hello.greet("Jeff") 18 | expected = "Hello, Jeff, nice to meet you!" 19 | 20 | expect(result).to eq(expected) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /problem-solving/spec/mad_lib_spec.rb: -------------------------------------------------------------------------------- 1 | require "./lib/mad_lib" 2 | 3 | RSpec.describe MadLib do 4 | 5 | it 'can create a new mad lib' do 6 | madlib = MadLib.new 7 | 8 | madlib.noun("dog") 9 | madlib.verb("walk") 10 | madlib.adjective("blue") 11 | madlib.adverb("quickly") 12 | result = madlib.result 13 | expected = "Do you walk your blue dog quickly? That's hilarious!" 14 | 15 | expect(result).to eq(expected) 16 | end 17 | 18 | it 'can create another mad lib' do 19 | madlib = MadLib.new 20 | 21 | madlib.noun("capybara") 22 | madlib.verb("pet") 23 | madlib.adjective("purple") 24 | madlib.adverb("intensely") 25 | result = madlib.result 26 | expected = "Do you pet your purple capybara intensely? That's hilarious!" 27 | 28 | expect(result).to eq(expected) 29 | end 30 | end 31 | 32 | -------------------------------------------------------------------------------- /problem-solving/spec/retirement_spec.rb: -------------------------------------------------------------------------------- 1 | require "./lib/retirement" 2 | 3 | RSpec.describe Retirement do 4 | 5 | it 'can tell me when I should retire' do 6 | retire = Retirement.new 7 | 8 | result = retire.calculate(25, 65) 9 | expected = "You have 40 years left until you can retire. It is 2015, so you can retire in 2055." 10 | 11 | expect(result).to eq(expected) 12 | end 13 | 14 | it 'tells me when to retire with different ages' do 15 | retire = Retirement.new 16 | 17 | result = retire.calculate(39, 70) 18 | expected = "You have 31 years left until you can retire. It is 2015, so you can retire in 2046." 19 | 20 | expect(result).to eq(expected) 21 | end 22 | 23 | it 'errors with a negative age' do 24 | retire = Retirement.new 25 | 26 | result = retire.calculate(-25, 65) 27 | expected = "Error. Age cannot be negative." 28 | 29 | expect(result).to eq(expected) 30 | end 31 | 32 | it 'errors with a negative retirement age' do 33 | # write your test here 34 | end 35 | end 36 | 37 | -------------------------------------------------------------------------------- /problem-solving/spec/simple_math_spec.rb: -------------------------------------------------------------------------------- 1 | require "./lib/simple_math" 2 | 3 | RSpec.describe SimpleMath do 4 | 5 | it 'can add two numbers' do 6 | sm = SimpleMath.new 7 | 8 | result = sm.add(2,2) 9 | expected = 4 10 | 11 | expect(result).to eq(expected) 12 | end 13 | 14 | it 'can add two different numbers' do 15 | sm = SimpleMath.new 16 | 17 | result = sm.add(5,3) 18 | expected = 8 19 | 20 | expect(result).to eq(expected) 21 | end 22 | 23 | it 'can subtract two numbers' do 24 | sm = SimpleMath.new 25 | 26 | result = sm.subtract(10,2) 27 | expected = 8 28 | 29 | expect(result).to eq(expected) 30 | end 31 | 32 | it 'can subtract two other numbers' do 33 | sm = SimpleMath.new 34 | 35 | result = sm.subtract(50,27) 36 | expected = 23 37 | 38 | expect(result).to eq(expected) 39 | end 40 | 41 | it 'can multiply two numbers' do 42 | 43 | # instantiate the class 44 | # 45 | # call the method being tested 46 | # determine your expected result 47 | # 48 | # check results 49 | end 50 | 51 | 52 | it 'can multiply two other numbers' do 53 | 54 | # instantiate the class 55 | # 56 | # call the method being tested 57 | # determine your expected result 58 | # 59 | # check results 60 | end 61 | 62 | it 'can divide two numbers' do 63 | # your code goes here 64 | end 65 | 66 | it 'can divide two other numbers' do 67 | # # your code goes here 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /problem-solving/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause 4 | # this file to always be loaded, without a need to explicitly require it in any 5 | # files. 6 | # 7 | # Given that it is always loaded, you are encouraged to keep this file as 8 | # light-weight as possible. Requiring heavyweight dependencies from this file 9 | # will add to the boot time of your test suite on EVERY test run, even for an 10 | # individual file that may not need all of that loaded. Instead, consider making 11 | # a separate helper file that requires the additional dependencies and performs 12 | # the additional setup, and require it from the spec files that actually need 13 | # it. 14 | # 15 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 16 | RSpec.configure do |config| 17 | # rspec-expectations config goes here. You can use an alternate 18 | # assertion/expectation library such as wrong or the stdlib/minitest 19 | # assertions if you prefer. 20 | config.expect_with :rspec do |expectations| 21 | # This option will default to `true` in RSpec 4. It makes the `description` 22 | # and `failure_message` of custom matchers include text for helper methods 23 | # defined using `chain`, e.g.: 24 | # be_bigger_than(2).and_smaller_than(4).description 25 | # # => "be bigger than 2 and smaller than 4" 26 | # ...rather than: 27 | # # => "be bigger than 2" 28 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 29 | end 30 | 31 | # rspec-mocks config goes here. You can use an alternate test double 32 | # library (such as bogus or mocha) by changing the `mock_with` option here. 33 | config.mock_with :rspec do |mocks| 34 | # Prevents you from mocking or stubbing a method that does not exist on 35 | # a real object. This is generally recommended, and will default to 36 | # `true` in RSpec 4. 37 | mocks.verify_partial_doubles = true 38 | end 39 | 40 | # This option will default to `:apply_to_host_groups` in RSpec 4 (and will 41 | # have no way to turn it off -- the option exists only for backwards 42 | # compatibility in RSpec 3). It causes shared context metadata to be 43 | # inherited by the metadata hash of host groups and examples, rather than 44 | # triggering implicit auto-inclusion in groups with matching metadata. 45 | config.shared_context_metadata_behavior = :apply_to_host_groups 46 | 47 | # The settings below are suggested to provide a good initial experience 48 | # with RSpec, but feel free to customize to your heart's content. 49 | =begin 50 | # This allows you to limit a spec run to individual examples or groups 51 | # you care about by tagging them with `:focus` metadata. When nothing 52 | # is tagged with `:focus`, all examples get run. RSpec also provides 53 | # aliases for `it`, `describe`, and `context` that include `:focus` 54 | # metadata: `fit`, `fdescribe` and `fcontext`, respectively. 55 | config.filter_run_when_matching :focus 56 | 57 | # Allows RSpec to persist some state between runs in order to support 58 | # the `--only-failures` and `--next-failure` CLI options. We recommend 59 | # you configure your source control system to ignore this file. 60 | config.example_status_persistence_file_path = "spec/examples.txt" 61 | 62 | # Limits the available syntax to the non-monkey patched syntax that is 63 | # recommended. For more details, see: 64 | # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ 65 | # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 66 | # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode 67 | config.disable_monkey_patching! 68 | 69 | # This setting enables warnings. It's recommended, but in some cases may 70 | # be too noisy due to issues in dependencies. 71 | config.warnings = true 72 | 73 | # Many RSpec users commonly either run the entire suite or an individual 74 | # file, and it's useful to allow more verbose output when running an 75 | # individual spec file. 76 | if config.files_to_run.one? 77 | # Use the documentation formatter for detailed output, 78 | # unless a formatter has already been configured 79 | # (e.g. via a command-line flag). 80 | config.default_formatter = "doc" 81 | end 82 | 83 | # Print the 10 slowest examples and example groups at the 84 | # end of the spec run, to help surface which specs are running 85 | # particularly slow. 86 | config.profile_examples = 10 87 | 88 | # Run specs in random order to surface order dependencies. If you find an 89 | # order dependency and want to debug it, you can fix the order by providing 90 | # the seed, which is printed after each run. 91 | # --seed 1234 92 | config.order = :random 93 | 94 | # Seed global randomization in this process using the `--seed` CLI option. 95 | # Setting this allows you to use `--seed` to deterministically reproduce 96 | # test failures related to randomization by passing the same `--seed` value 97 | # as the one that triggered the failure. 98 | Kernel.srand config.seed 99 | =end 100 | end 101 | --------------------------------------------------------------------------------