← Back to Upcase

Refactoring - Explaining Variable


(Upcase ) #1

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]: https://exercises.upcase.com/exercises/explaining-variable
[Refactoring]: https://thoughtbot.com/upcase/refactoring


(Willis Plummer) #3

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
  end

(Chris Toomey) #4

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?


(Jean) #5

@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:)


(David) #6

@chicjean,

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?


(Jean) #7

@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.


(Gabriel Lafontant) #8

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 git@git.upcase.com:TechnoAlchemist/explaining-variable.git
cd explaining-variable
bin/setup

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!


(Andy Waite) #9

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


(Marouen Bousnina) #10

Hi,

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:


(Andy Waite) #11

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)
end

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
end

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)
end

def days_in_year(leap)
  leap ? 366 : 365
end

Hope this helps.


(Marouen Bousnina) #12

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


(Chris Hutchinson) #13

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?