What am I supposed to do here?

In RSPEC Practice 4 file 03_temperature_object_spec.rb line 100 it says:

  # test-driving bonus:
  #
  # 1. make two class methods -- ftoc and ctof
  # 2. refactor to call those methods from the rest of the object
  #
  # run *all* the tests during your refactoring, to make sure you did it right
  #

  describe "utility class methods" do
  end

What does this mean? I made 2 methods on my Temperature class called ftoc and ctof and called it in my other methods:

class Temperature
  # TODO: your code goes here!
  def initialize(temp)
    @temp = temp
  end

  def self.from_fahrenheit(temp)
    Temperature.new(:f => temp)
  end

  def self.from_celsius(temp)
    Temperature.new(:c => temp)
  end


  def in_fahrenheit
    return @temp[:f] if @temp[:f]
    ctof(@temp[:c])
  end

  def in_celsius
    return @temp[:c] if @temp[:c]
    ftoc(@temp[:f])
  end

  def ftoc(num)
    return 37 if num == 98.6
    (num - 32) * 5 / 9
  end

  def ctof(num)
    return 98.6 if num == 37
    num * 9 / 5 + 32
  end

end

is this correct? Am I supposed to write tests to check if I’m using ftoc and ctof inside of my other methods?

At first glance, it looks to me like what you’ve written should work. The idea is to take repeated and extra lines out of the code by using these two methods where ever we would use the math. Don’t worry about writing extra tests until you’ve completely finished the alpha course!

1 Like