math - ruby diagonal of a rectangle -
i trying create method returns length of rectangle/square diagonal float. however, method diagonal
doesn't seem work intended. guess have hit road block , see if of had idea on how approach 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
the trouble seems here:
def diagonal # measure = math.hypot(@length, @width) measure = (@length.to_f ** 2) + (@width.to_f ** 2) measure.hypot(@length, @width) end
you seem begin try calculate length using pythagoras' method, attempt call hypot
method on float. have 2 options:
def diagonal math.hypot(@length, @width) end
or
def diagonal math.sqrt((@length.to_f ** 2) + (@width.to_f ** 2)) end
Comments
Post a Comment