RSPEC test and Ruby Object -
so i'm trying write code make rspec test pass ruby. i'm having trouble getting first test pass. think if can have little this, i'll able rest. can post rest of tests if makes easier offer advice.
so it's constructing fahrenheit/celsius converter, using objects , classes instead of defining couple of methods conversions.
the first part looks
require "temperature" describe temperature describe "can constructed options hash" describe "in degrees fahrenheit" "at 50 degrees" temperature.new(:f => 50).in_fahrenheit.should == 50 end one of hints in instructions says temperature object constructer should accept options hash either :celsius or :fahrenheit entry.
any or hints appreciated. i've been stuck on test last few weeks. in advance.
i think temperature class needs work. why not have 'scale' attribute can set base value of temperature object?
here's modified version of you've posted:
class temperature attr_accessor :value, :scale def initialize(value, options={}) @value = value if options.has_key?(:scale) @scale = options[:scale] else @scale = :c end end def in_fahrenheit() if @scale == :c ( @value * 9.0/5.0 ) + 32.0 else @value end end end there's no need create new temperature object when call #in_fahrenheit. can make current object convert number (stored in attribute) fahrenheit. can add temperature information when create object:
t1=temperature.new(68, :scale =>:f) t2=temperature.new(0) 2.0.0dev :199 > t1.in_fahrenheit => 68 2.0.0dev :200 > t2.in_fahrenheit => 32.0
Comments
Post a Comment