Refactoring - Explaining Variable

This topic is for the [Explaining Variable][] exercise in the [Refactoring][] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Explaining Variable]:

Seems like there’s something wrong with the syntax of the initialize method? my tests kept failing until i changed it to:

  def initialize(amount, discount_percentage= 0, tip_percentage)
    @amount = amount
    @discount_percentage = discount_percentage
    @tip_percentage = tip_percentage

Hello @willisplummer, the example as written uses Ruby keyword arguments (with defaults) which were introduced in Ruby 2.0. My guess is you are running on an older ruby. Can you confirm the output of ruby -v, and likewise, are you running any Ruby version manager like rvm or rbenv?

@christoomey running into the same issue. i’m using rbenv, and have multiple ruby versions installed. my global version is set to 2.1.5, but the repo wants to use 2.0.0-p418, which is installed.

i’ve tried changing the ruby version of the project to something a little higher and it doesn’t seem to want to do anything besides throw a syntax error when I try to run the specs.

explaining-variable/lib/tipper.rb:4: syntax error, unexpected ',' (SyntaxError)def initialize(amount:, discount_percentage: 0, tip_percentage:)


In my explaining-variable repo there are commits from March 2015 that add a .ruby-version file specifying 2.2.1. Where are you seeing 2.0.0-p418?

@davidcpell i noticed that, too. turns out that at least for me, i was having some trouble with rbenv recognizing the correct version. rbenv rehash and bundle install seemed to fix it for me.

1 Like

Hi, I’m having issues running the setup script from the bin directory. I was able to clone the repo and cd into explaining-variable. However, each time I try to run the setup script, I receive this error:

bin/setup: line 10: bundle: command not found

The instructions are quite clear:

git clone
cd explaining-variable

Am I missing something? Is there a configuration issue with my .bash_profile? I’m a little confused as to what is the problem.

Thanks in advance!

1 Like

It sounds like you don’t have bundler installed for that particular Ruby version. Run gem install bundler and try run bin/setup again.


I have a question regarding explaining variable refactoring, is it like a preparation step before using extract method approach ?
and in which cases do you just use this refactoring approach instead of extracting to a method ?

Thx :slight_smile:

Introduce Explaining Variable is a refactoring by itself, not just a preparatory step for Extract Method, although it could be used as an intermediate step to Extract Method.

There’s usually a trade-off between reducing the complexity of a method vs adding an extra level of indirection.

For example, let’s say you were calculating the number of seconds in a year:

def seconds_in_year(leap)
  60 * 60 * 24 * (leap ? 366 : 365)

You could refactor this with Introduce Explaining Variable to:

def seconds_in_year(leap)
  seconds_in_minute = 60
  seconds_in_hour = 60
  hours_in_day = 24
  days_in_year = leap ? 366 : 365
  seconds_in_minute * seconds_in_hour * hours_in_day * days_in_year

You could stop there, but that method is starting to look a little dense. It’s not worth extracting out the simple assignments, but it might be worth applying Extract Method for days_in_year:

def seconds_in_year(leap)
  seconds_in_minute = 60
  seconds_in_hour = 60
  hours_in_day = 24
  seconds_in_minute * seconds_in_hour * hours_in_day * days_in_year(leap)

def days_in_year(leap)
  leap ? 366 : 365

Hope this helps.

1 Like

Thanks @andyw8 for this amazing example. It was very helpful. :thumbsup:

Completely lost on what I am trying to do, but can’t even push as the hint suggests - just says Everything up-to-date

Even if I just add whitespace, I can’t commit and push… does anyone know what is going wrong?

In the featured solution it is used a local variable named tax to hold the tax amount. Don’t you think that it can cause confusion with the TAX constant?