RSPEC 4 - Temp Object and MyHashSet

1. For Temperature, I’m having some issues getting started. I looked online and I found the way others initialized was quite confusing. I’m sure this is very wrong, but I have no idea how to find the value of this hash that was created? Am I thinking about this question all wrong? => 50)

class Temperature

      attr_accessor :f, :c
      def initialize(fahr, celsius) 
        @f = fahr
        @c = celsius

      def self.in_fahrenheit(fahr, celsius)
        if fahr['f'].nil?
          @f = @c * (9/5).to_f * (c + 32).to_f
          @f = fahr['f']

2. Accessing set1 and set 2 for the union and intersection in My_Hash_Set? How do I do that? I read on the Discussion board it might be in MyHashSet, but I’m not sure how to pull it? I thought either @store = set1 or @store = set2
class MyHashSet
attr_accessor :store, :a, :union,:intersect, :set2, :set1

  def initialize
    @store ={})

  def insert(ele)
    @store[ele] = true

  def include?(word)

  def delete(ele)
    if @store.include?(ele)
      return true
    return false

  def to_a
    @a = @store.keys

  # def union(set2)
  #   @union ={})
  #   @union = self.each do |k,v|
  #     @union[k] = v
  #   end
  #   @union = set2.each do |k,v|
  #     @union[k] = v
  #   end
  #   return @union
  # end
  # def intersect(set2, MyHashSet)
  #   @intersect = do |k,v|
  #     key_set2 = set2.keys
  #     key_set2.include?(k)
  #   end
  # end


Errors for My_Hash_Set is
16) MyHashSet set interaction methods #union returns a new set
Failure/Error: expect(set1.union(set2)).to be_a(MyHashSet)
_ ArgumentError:_
_ wrong number of arguments (given 1, expected 0)_

  1. MyHashSet set interaction methods #union returns a set containing elements in EITHER set
    Failure/Error: els = set1.union(set2).to_a
    _ ArgumentError:_
    _ wrong number of arguments (given 1, expected 0)_

Thank you very much!


It’s weird, the alpha course does cover this, but not until later. Read this section.


set1 is self, set2 is the method’s argument

Hi @hsusteph5 :slight_smile:

For your Tempurature class, you’re working in the right direction, but you should be writing your initialize to take in an options hash as an argument. This is a specific type of hash that sets ‘options’ in the method. You want to work from the idea that if the hash you pass in has a key of “:f”, you want to do one set of logic and data storage with its temperature value, and if it has a “:c” in it, you’ll do another set of things. That way, your key in the Hash dictates how your class knows what kind of temperature it is starting with. Hope that helps!

P.S. if you have a chance to go back over the specs, take another look at the ‘expect’ lines. They’ll have a lot of information about what kind of argument the method takes in and what kind of output it should have.
For the line “expect( => 50).in_fahrenheit).to eq(50)” we can see that when we call with “:f => 50” as its argument and then call in_farenheit, we should expect the result to equal 50


Thank you very much. I got a better idea and fixed my problems!


Hi, I’ve got too many questions, so let’s start with these:

  1. I’m wondering what it means when there is a class defined with a “<” or “>” symbol, such as class Celsius < Temperature and class Fahrenheit < Temperature. Couldn’t find this on Google like I do with most of this stuff.

  2. Why are the setter methods for Celsius and Fahrenheit different? The Fahrenheit one has @temperature = self.class.ftoc(temp) and the Celsius one has @temperature = temp

  3. In the setter method for Fahrenheit, it calls ftoc on self.class, and I thought that the only reason for using self.class was to find out which class an object belongs to. I don’t understand when and why we would call methods on the class name.

I just realized the answer to my first question. Celsius and Fahrenheit are subclasses that can use all of the methods defined for the Temperature class.

  1. It looks like @temperature is always stored internally in celsius. So for Celsius no conversion was necessary, but for Fahrenheit it needed to be converted into celsius.

  2. self.class isn’t just the name of the class, it’s the actual class object so inside the Fahrenheit class self.class.ftoc is the same as Fahrenheit.ftoc

Edit: accidentally submitted before finishing