-1

I am trying to create a method that returns the length of a rectangle/square diagonal as a float. However, my method diagonal doesn't seem to work as intended. I guess I have hit a road block and would like to see if any of you had an idea on how I could approach this problem.

class Rectangle

  def initialize(width, length)
    @width  = width
    @length = length
  end

  def perimeter
    2*(@length + @width)
  end

  def area
      @length * @width
  end

  def diagonal
    # measure = Math.hypot(@length, @width)
    measure = (@length.to_f ** 2) + (@width.to_f ** 2)
    measure.hypot(@length, @width)
  end

end
Nappstir
  • 995
  • 2
  • 20
  • 38
  • As this is obviously a homework problem... you state "However, my method diagonal doesn't seem to work as intended". Start with explaining, in detail, what the diagonal method should do, step by step. And what about what you have written is not working as you expect? – Charlie May 12 '15 at 03:01
  • @Charlie I'm kinda new to all of this. I am trying to learn how to program on my own and found this practice problem on the internet. So no, it is not a 'homework' problem per se. Thus, my stackoverflow questions I'm sure will get better as I go. Sure hope you weren't the down vote. =( – Nappstir May 12 '15 at 05:48
  • My point is, self assigned or not it's still essentially homework, a problem ideally designed to teach or reinforce something you learned. But still, describing the difference between what you expect and what is happening, and either why you feel your code should do what you expect Or the specific gap your code needs to get to a desired state... not only helps make for a better question, but being level of detail (rubber duck debugging) sometimes helps you solve your own questions as well. – Charlie May 12 '15 at 11:38

1 Answers1

2

The trouble seems to be here:

def diagonal
    # measure = Math.hypot(@length, @width)
    measure = (@length.to_f ** 2) + (@width.to_f ** 2)
    measure.hypot(@length, @width)
end

You seem to begin to try to calculate the length yourself using Pythagoras' method, but then attempt to call the hypot method on a float. You have two options:

def diagonal
  Math.hypot(@length, @width)
end

or

def diagonal
    Math.sqrt((@length.to_f ** 2) + (@width.to_f ** 2))
end
David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
  • Thank you @David, for w/e reason I just didn't quite understand the `hypot` method. Not realizing that I didn't have to square length and width. – Nappstir May 12 '15 at 06:25