0

So I'm trying to write some code to make an RSPEC test pass for Ruby. But I'm having some trouble even getting the first test to pass. I think if I can have a little help with this, I'll be able to get the rest. But I can post the rest of the tests if that makes it easier to offer advice.

So it's constructing a fahrenheit/celsius converter, but using objects and classes instead of just defining a couple of methods to do the conversions.

The first part looks like this

   require "temperature"

describe Temperature do

  describe "can be constructed with an options hash" do
    describe "in degrees fahrenheit" do
      it "at 50 degrees" do
        Temperature.new(:f => 50).in_fahrenheit.should == 50
      end

One of the hints in the instructions says that the Temperature object constructer should accept an options hash with either a :celsius or :fahrenheit entry.

Any help or hints would be greatly appreciated. I've been stuck on this test for the last few weeks. Thanks in advance.

  • This is what I have so far trying to get the first test to pass. class Temperature attr_accessor :fahrenheit def initialize options = {} @fahrenheit = :f end def in_fahrenheit({ :f}) Temperature.new({:f}) end end – PaperKraftMike Mar 01 '13 at 16:20
  • 1
    Your code snippet is incomplete. Please add the missing 'end' keywords. – LazyMonkey Mar 01 '13 at 16:22

1 Answers1

1

I think your Temperature class needs some work. Why not have a 'scale' attribute that can set the base value of your Temperature object?

Here's a modified version of what 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 to create a new Temperature object when you call #in_fahrenheit. You can make your current object convert a number (stored in an attribute) into fahrenheit. You can add the temperature information when you create your 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 
LazyMonkey
  • 517
  • 5
  • 8
  • Thanks @LazyMonkey, I see where you're going with this. I appreciate the tips, but when i try and run it pass the test I get: undefined method `*' for {:f=>50}:Hash # ./08_temperature_object/temperature.rb:13:in `in_fahrenheit' # ./08_temperature_object/temperature_object_spec.rb:27 Any idea what could be causing that? – PaperKraftMike Mar 02 '13 at 04:18
  • Not clear if you're referring to your original class or my mods. I'd guess your RSpec test is having trouble with your #in_fahrenheit method, because you define {:f} as a method parameter. That looks wrong (it's only a symbol inside braces, not a :key => value pair.) Not sure that's valid Ruby syntax (I can't get it to pass!) Please post & format your Temperature class. – LazyMonkey Mar 02 '13 at 17:09