我正在尝试计算 0 华氏度的冰点。我的代码返回零。我正在尝试解决http://testfirst.org/中的一个问题。所以我创建了一个温度类。创建了一个哈希。我遍历每个哈希。使用我迭代的值并进行计算
我的代码
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)>'