Completely Lost and Stuck

So far, I was able to go through lessons/exercises even though I have few problems I wasn’t able to solve and want to go over when office hours start (fixnum from RSPEC Practice 3, perf_monitor/bubble_sort from RSPEC Practice 2, and my_merge from Class Extension Debuggin Exercises).

But now, I’m at RSPEC Practice 4 and I’m completely lost. From the timer question to my_hash_set question, I don’t even get what the problem is asking me to do. Looking at the solution doesn’t help and I’ve been trying to figure out the problems by looking at what other people wrote about them on the forum, but I still am completely lost.

I just want to know what I’m supposed to do in a situation like this. I went over Classes lessons as well to try to better understand the concept but it wasn’t too helpful because the concepts/examples in the lessons were pretty basic/straight forward compared to the problems in RSPEC Practice 4. Was I supposed to take Introduction To Ruby Programming course before starting the Alpha Course? Is there any supplementary material I can look into to help me understand concepts better? Please help😕

Hi,

The first thing to understand is that it’s totally normal to feel overwhelmed at this point. There isn’t a whole lot of direction and it can be tough to know what to do or where to start. Not to worry!

Let’s start with the timer. Here’s what you’re given:

class Timer
  # TODO: your code goes here!
end 

So obviously you’ll be making a Timer class. The next step is always to look at the specs. Reading the specs is super important and will guide you on how to pass them. Here’s the first one:

it "should initialize to 0 seconds" do
  expect(@timer.seconds).to eq(0)
end

We can see that this test expects an instance of the Timer class to receive the method seconds and return 0. So it’s a good bet that you should create an initialize method, create an @seconds instance variable, set the variable to 0, and add an attr_accessor for seconds.

Here’s the next spec:

it "should display 0 seconds as 00:00:00" do
  @timer.seconds = 0
  expect(@timer.time_string).to eq("00:00:00")
end

First, we can see that the instance of the Timer class (@timer) receives the method time_string, so you’ll need to write a method with that name. Second, time_string should return “00:00:00” when seconds is set to 0. So your time_string method needs to convert a number of seconds into a formatted string.

Just go through each spec one by one, see what methods the spec calls and what the spec expects the methods to return, and you’ll get a pretty good idea of what code you need to write.

Hope this helps!

-Jason

1 Like

Hi Jason,

breaking down the problem to each spec is really helpful, thank you.
I was able to naviage through timer.

But now I’m at temparature_object question and now I’m really stuck. spec file just looks like gibberish to me and even looking at the solution doesn’t really help me grasp what each the overall function of three different classes are.

Hi,

Let’s follow the same pattern here that we did for the timer problem. The first spec says:

expect(Temperature.new(:f => 50).in_fahrenheit).to eq(50)

We can learn a great deal just from this first spec. First, we see that Temperature.new is called with an argument of :f => 50. Right away, we know that we’ll need an initialize method for the Temperature class (because initialize is what allows you to call Temperature.new to create new Temperature objects). We also know that the initialize method must take a hash as an argument (since :f => 50 is a hash). Lastly, if you look further down in the specs, you can see that each hash argument has a key of either :f or :c and the value is always an integer. Thus, it stands to reason that the hash argument will represent a number of degrees in either fahrenheit or celsius.

Next, still looking at that first spec, we see that the method in_fahrenheit is called on the new Temperature object. Further down in the specs, you can see there is an in_celsius method as well. Each method returns an integer.

So, your plan to tackle the first series of specs:

  1. Create an initialize method for the Temeprature class that takes a hash as an argument.
  2. Set instance variables as you deem necessary.
  3. Create two methods, in_fahrenheit and in_celsius that return the number of degrees of the Temperature object in fahrenheit or celsius respectively.

Once you are done, examine the next series of specs, take note of how objects are created, take note of what methods are called and what they return, and that should give you a decent idea of what you need to write.

Good luck!

-Jason

Thank you so much! I was able to come up with a solution that passed all the specs!!! BUT I don’t know if my solution is following all the directions. This is the solution I came up with but it looks very different from the solution that’s given to us

class Temperature
  def initialize(options)
    @temperature = options
  end

  def in_fahrenheit
    if @temperature[:f]
      @temperature[:f]
    else
      @temperature[:c].to_f * 9 / 5 + 32
    end
  end

  def in_celsius
    if @temperature[:f]
      (@temperature[:f] - 32) * 5 / 9
    else
      @temperature[:c]
    end
  end

  def self.from_celsius(temp)
    Temperature.new(:c => temp)
  end

  def self.from_fahrenheit(temp)
    Temperature.new(:f => temp)
  end
end


# Subclasses/Inheritance
class Celsius < Temperature
  def initialize(temp)
    @temperature = {:c => temp}
  end

  def in_celsius
    @temperature[:c]
  end
end

class Fahrenheit < Temperature
  def initialize(temp)
    @temperature = {:f => temp}
  end

  def in_fahrenheit
    @temperature[:f]
  end
end

I feel like I’m not using the Subclasses/Inheritance properly.

Also I was able to navigate through the dictionary problem but I couldn’t figure out why my printable method wasn’t working. My solution looks like this:

class Dictionary
  attr_reader :entries

  def initialize
    @entries = {}
  end

  def add(new_entries)
    if new_entries.is_a?(String)
      @entries[new_entries] = nil
    elsif new_entries.is_a?(Hash)
      @entries.merge!(new_entries)
    end
  end

  def find(fragment)
    @entries.select do |word, definition|
      word.match(fragment)
    end
  end

  def keywords
    @entries.keys.sort { |x, y| x <=> y }
  end

  def include?(word)
    @entries.has_key?(word)
  end

  def printable
    entries = keywords.map do |keyword|
      %Q{[#{keyword}] "#{@entries[keyword]}"}
    end

    entries.join("\n")
  end
end

Again, thank you so much for the help. I was super frustrated and didn’t even know what to do but now I’m able to go thorugh problems and understand what’s happening way more!!

Hey,

Great job with the temperature specs. Frequently, there are multiple ways to solve a problem or pass a spec. For now, as long as you can pass all the specs and understand why they pass, that is great. In the main course you will delve into why some solutions are superior to others.

Re: your printable method, it looks identical to the solution to me, so it should work. If it doesn’t, what is the error you are getting?

-Jason

Thank you! and that’s good to hear :smiley: I’ll focus on just passing specs for now.

for printable method, this is the error message and the screenshot directly from Terminal.

Failure/Error: expect(@d.printable).to eq(%Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"})
       
       expected: "[apple] \"fruit\"\n[fish] \"aquatic animal\"\n[zebra] \"African land animal with stripes\""
            got: "[apple] \"fruit\" [fish] \"aquatic animal\" [zebra] \"African land animal with stripes\""
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -[apple] "fruit"
       -[fish] "aquatic animal"
       -[zebra] "African land animal with stripes"
       +[apple] "fruit" [fish] "aquatic animal" [zebra] "African land animal with stripes"
       
     # ./spec/04_dictionary_spec.rb:95:in `block (3 levels) in <top (required)>'

Hi Josh,

The important thing to take a look at here is our expected and got lines:

expected: "[apple] \"fruit\"\n[fish] \"aquatic animal\"\n[zebra] \"African land animal with stripes\""
got: "[apple] \"fruit\" [fish] \"aquatic animal\" [zebra] \"African land animal with stripes\""

We can see the difference is a couple of \n's sprinkled throughout. These are special characters meaning new lines. All you need to do is update your code so that the output is on more than one line!

-Matthew

OHHH I see, I missed the \n part.
Thank you :grinning::grinning: