Temporay variables tend to be a code smell. They make methods longer and harder to reason about the method itself. Eventually they map logic, which itself is not necessarily important for the meaning at first. Further going refactoring steps (e.g. Extract Method) can be hindered by temporary variables.
Fir instance the class Person contains a public method adult?, determining if the person is an adult or not:
class Person attr_accessor :birthday ADULT_AGE = 18 def adult? return false if birthday.nil? years_since_birthday = Date.today.year - birthday.year years_since_birthday >= ADULT_AGE end end
Extracting the temporary variable years_since_birthday into a dedicated method makes totally sense. The method adult? becomes more concise. Another benefit is the possible reuse of the method in different context:
class Person attr_accessor :birthday ADULT_AGE = 18 def adult? return false if birthday.nil? years_since_birthday >= ADULT_AGE end private def years_since_birthday Date.today - birthday.year end end
However the Query Method approach should not be exercised at all costs. At least temporary variables make sense, if the their logic is applied multiple times in the same method and its repeated calculation would drive a performance loss.