Lesson 11. Feeling included


#1

June 8, 2016 - There seems to be a problem in lesson 11 (Feeling included for Ruby Object-Oriented Programming II)

The exercise outlines to add "include Math" in line two to use method cos without scope resolution operator. This however, will raise an issue in the interpreter box which stated something like that method "cos" cannot be resolved.

As a work-around I added a module Math prior to the class Angle which includes it. My own module Math contains a dummy method cos which takes radius as parameter.

https://www.codecademy.com/courses/ruby-beginner-en-zfe3o/1/5?curriculum_id=5059f8619189a5000201fbcb

module Math
  def cos(radius); end
end

class Angle
  include Math
  attr_accessor :radians
  
  def initialize(radians)
    @radians = radians
  end
  
  def cosine
    cos(@radians)
  end
end

acute = Angle.new(1)
acute.cosine

#2

A hint: The instructions say that:

Any class that includes a certain module can use those module's methods!

Also, reset the code and then read the instructions carefully:

Please include Math on line 2.


#3

The following code:

class Angle
  include Math
  attr_accessor :radians
  
  def initialize(radians)
    @radians = radians
  end
  
  def cosine
    cos(@radians)
  end
end

acute = Angle.new(1)
acute.cosine

returns:

undefined method `cos' for #<Context::Angle:0x00000001bfde60 @radians=1>

and the additional help box displays:

Oops, try again. Did you include Math on line 2?

#4

That code doesn't produce either of those error messages!
I think you must be doing something more that you're not telling/noticing.
Refreshing the page (to reset the environment in case you've done something fishy) and then pasting exactly the code you just posted and running it passes.

I think you did this:

class Angle
  include Math
  attr_accessor :radians
  
  def initialize(radians)
    @radians = radians
  end
  
  def cosine
    cos(@radians)
  end
end

acute = Angle.new(1)
acute.cos

(Last line is different.) But that's not what you posted.

So essentially the whole case here, from what I can tell, is that you made some less-than-perfect observations and kinda chased your own tail for a bit.

Actually, this is more likely:

module Math
end

class Angle
  include Math
  attr_accessor :radians
  
  def initialize(radians)
    @radians = radians
  end
  
  def cosine
    cos(@radians)
  end
end

acute = Angle.new(1)
acute.cosine

Where Math is redefined to something that doesn't have a cos method


#5

@robert.v.simon
Try it with

class Angle
  include Math
  attr_accessor :radians
  
  def initialize(radians)
    @radians = radians
  end
  
  def cosine
    Math.cos(@radians)
  end
end

#6

I went back to the page and reset the code in the editor a couple of times and now it works as described.

However, I can assure that I copied the code as it was in the editor and the error message came as described. Before it started working I also removed the last two lines as suggested by Leonhard which made it work too. After that I reset the editor again and it works now by just placing the include Math in line 2.

I think we can close here.


#7

#8