why is my code returning nil in ruby?
I am trying to calculate freezing point of 0 Fahrenheit. My code is
returning nil. I am trying to solve a question in http://testfirst.org/ .
So I created a Temperature class. created a Hash. I iterate over each
hash. With the value I iterate and do the calculation
My Code
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
My Test
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
My terminal
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)>'
No comments:
Post a Comment