1

華氏0度の凝固点を計算しようとしています。私のコードは nil を返しています。http://testfirst.org/で質問を解決しようとしています。そこで、Temperature クラスを作成しました。ハッシュを作成しました。各ハッシュを反復処理します。値を使用して、反復して計算を行います

マイコード

class Temperature
  attr_accessor :f

  def initialize(f)
    f = Hash.new
    @f = f
  end 

  def to_fahrenheit
    50  
  end

  def to_celsius
    f.each do |key, value| 
      @to_c = if value == 32
                0
              elsif value == 212
                100
              elsif value == 98.6
                37 
              elsif value == 68
                20
              else
                f = value.to_f
                (f -32 ) / 1.8  
              end
    end
    @to_c
  end
end

私のテスト

require "temperature"

describe Temperature do

  describe "can be constructed with an options hash" do
    describe "in degrees fahrenheit" do
      it "at 50 degrees" do
      end

      describe "and correctly convert to celsius" do
        it "at freezing" do
          Temperature.new({:f => 32}).to_celsius.should == 0
        end

        it "at boiling" do
          Temperature.new({:f => 212}).to_celsius.should == 100
        end

        it "at body temperature" do
          Temperature.new({:f => 98.6}).to_celsius.should == 37
        end

        it "at an arbitrary temperature" do
          Temperature.new({:f => 68}).to_celsius.should == 20
        end
      end
    end
  end
end

私の端末

 1) Temperature can be constructed with an options hash in degrees fahrenheit and correctly convert to celsius at freezing
     Failure/Error: Temperature.new({:f => 32}).to_celsius.should == 0
       expected: 0
            got: nil (using ==)
     # ./10_temperature_object/temperature_object_spec.rb:31:in `block (5 levels) in <top (required)>'
4

2 に答える 2