I have the following bit of code:
load 'Point.rb'
class Triangle
def initialize(x, y , z)
if !x.is_a?(Point) || !y.is_a?(Point) || !z.is_a?(Point)
puts "Invalid data, a Triangle can only be initialized through points"
else
@x=x
@y=y
@z=z
@type=type(x, y, z)
end
end
def type(x, y, z)
if x.distance(y) == y.distance(z) && y.distance(z) == z.distance(x)
return "equilateral"
elsif x.distance(y)== y.distance(z) || y.distance(z) == z.distance(x) || z.distance(x) == x.distance(y)
return "isosceles"
else
return "scalene"
end
end
attr_accessor :type
end
I'm calling the method like this:
load 'Triangle.rb'
x=Point.new(0,0)
y=Point.new(1,1)
z=Point.new(2,0)
triangle=Triangle.new x, y, z
puts triangle.type
The class Point is as follows:
class Point
def initialize (x=0, y=0)
@x=x.to_i
@y=y.to_i
end
def ==(point)
if @x==point.x && @y==point.y
return true
else
return false
end
end
def distance(point)
Math.hypot((@x-point.x),(@y-point.y))
end
attr_accessor :y
attr_accessor :x
end
The error is as follows:
Triangle.rb:11:in `initialize': wrong number of arguments (given 3, expected 0) (ArgumentError)
from Triangle_test.rb:6:in `new'
from Triangle_test.rb:6:in `<main>'
Please tell if the whole code is just garbage.
.new. Also, what is thistypemethod you're calling? - max pleanertypedefined? - spickermann