├── .gitignore ├── .ruby-version ├── Gemfile ├── Gemfile.lock ├── LICENSE.md ├── README.md ├── Rakefile ├── bin └── setup ├── lib └── tipper.rb └── spec ├── lib └── tipper_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | !.keep 2 | *.DS_Store 3 | *.swo 4 | *.swp 5 | .bundle 6 | tags 7 | vendor 8 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.2.1 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gem "rake" 4 | gem "rspec" 5 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | diff-lcs (1.2.5) 5 | rake (10.3.2) 6 | rspec (3.1.0) 7 | rspec-core (~> 3.1.0) 8 | rspec-expectations (~> 3.1.0) 9 | rspec-mocks (~> 3.1.0) 10 | rspec-core (3.1.7) 11 | rspec-support (~> 3.1.0) 12 | rspec-expectations (3.1.2) 13 | diff-lcs (>= 1.2.0, < 2.0) 14 | rspec-support (~> 3.1.0) 15 | rspec-mocks (3.1.3) 16 | rspec-support (~> 3.1.0) 17 | rspec-support (3.1.2) 18 | 19 | PLATFORMS 20 | ruby 21 | 22 | DEPENDENCIES 23 | rake 24 | rspec 25 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2015-2018 [thoughtbot, inc.](http://thoughtbot.com) 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Refactoring / Explaining Variable 2 | 3 | Hey there! We're [thoughtbot](https://thoughtbot.com), a design and 4 | development consultancy that brings your digital product ideas to life. 5 | We also love to share what we learn. 6 | 7 | This coding exercise comes from [Upcase](https://thoughtbot.com/upcase), 8 | the online learning platform we run. It's part of the 9 | [Refactoring](https://thoughtbot.com/upcase/refactoring) course and is just one small sample of all 10 | the great material available on Upcase, so be sure to visit and check out the rest. 11 | 12 | ## Exercise Intro 13 | 14 | In this exercise, you'll be extracting explaining variables within the `total` method of `Tipper`. 15 | 16 | An explaining variable is a variable whose name describes the purpose of the expression you're assigning to it. It's used to help break down longer, more complex expressions, making the final expression more manageable and easier to understand. 17 | 18 | [Read more about explaining variables](http://refactoring.com/catalog/extractVariable.html). 19 | 20 | ## Instructions 21 | 22 | To start, you'll want to clone and run the setup script for the repo 23 | 24 | git clone git@github.com:thoughtbot-upcase-exercises/explaining-variable.git 25 | cd explaining-variable 26 | bin/setup 27 | 28 | Extract a few new variables whose names explain their purpose. 29 | 30 | Make sure the tests are passing by running: 31 | 32 | rake 33 | 34 | ## Featured Solution 35 | 36 | Check out the [featured solution branch](https://github.com/thoughtbot-upcase-exercises/explaining-variable/compare/featured-solution#toc) to 37 | see the approach we recommend for this exercise. 38 | 39 | ## Forum Discussion 40 | 41 | If you find yourself stuck, be sure to check out the associated 42 | [Upcase Forum discussion](https://forum.upcase.com/t/refactoring-explaining-variable/4636) 43 | for this exercise to see what other folks have said. 44 | 45 | ## Next Steps 46 | 47 | When you've finished the exercise, head on back to the 48 | [Refactoring](https://thoughtbot.com/upcase/refactoring) course to find the next exercise, 49 | or explore any of the other great content on 50 | [Upcase](https://thoughtbot.com/upcase). 51 | 52 | ## License 53 | 54 | explaining-variable is Copyright © 2015-2018 thoughtbot, inc. It is free software, 55 | and may be redistributed under the terms specified in the 56 | [LICENSE](/LICENSE.md) file. 57 | 58 | ## Credits 59 | 60 | ![thoughtbot](https://thoughtbot.com/thoughtbot-logo-for-readmes.svg) 61 | 62 | This exercise is maintained and funded by 63 | [thoughtbot, inc](http://thoughtbot.com/community). 64 | 65 | The names and logos for Upcase and thoughtbot are registered trademarks of 66 | thoughtbot, inc. 67 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | 3 | require 'rspec/core/rake_task' 4 | 5 | RSpec::Core::RakeTask.new(:spec) 6 | 7 | task default: :spec 8 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # Run this script immediately after cloning the codebase. 4 | # https://github.com/thoughtbot/guides/tree/master/protocol 5 | 6 | # Exit if any subcommand fails 7 | set -e 8 | 9 | # Set up Ruby dependencies via Bundler 10 | bundle install 11 | -------------------------------------------------------------------------------- /lib/tipper.rb: -------------------------------------------------------------------------------- 1 | class Tipper 2 | TAX = 0.05 3 | 4 | def initialize(amount:, discount_percentage: 0, tip_percentage:) 5 | @amount = amount 6 | @discount_percentage = discount_percentage 7 | @tip_percentage = tip_percentage 8 | end 9 | 10 | def total 11 | amount + (amount * TAX) - (amount * (discount_percentage / 100.0)) + (amount * (tip_percentage / 100.0)) 12 | end 13 | 14 | private 15 | 16 | attr_reader :amount, :discount_percentage, :tip_percentage 17 | end 18 | -------------------------------------------------------------------------------- /spec/lib/tipper_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Tipper do 4 | describe '#total' do 5 | it 'returns the total due with tax and tip' do 6 | tipper = Tipper.new(amount: 100, tip_percentage: 20) 7 | expect(tipper.total).to eq 125 8 | end 9 | 10 | it 'returns the total due with tax, discount, and tip' do 11 | tipper = Tipper.new(amount: 100, discount_percentage: 20, tip_percentage: 20) 12 | expect(tipper.total).to eq 105 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | PROJECT_ROOT = File.expand_path("../..", __FILE__) 2 | 3 | Dir.glob(File.join(PROJECT_ROOT, "lib", "*.rb")).each do |file| 4 | require file 5 | end 6 | --------------------------------------------------------------------------------