在我下面的Triangle
课程中,我有一个initialize
方法,它有三个参数。
class Triangle
attr_accessor :side1, :side2, :side3
def initalize(one, two, three)
@side1 = one
@side2 = two
@side3 = three
end
end
当我创建一个新类并使用三个参数初始化它时,
one = Triangle.new(1,2,3)
puts one.side1
puts one.side2
puts one.side3
我收到此错误:
"lib/triangle.rb:21:in `initialize': wrong number of arguments (given 3, expected 0) (ArgumentError)".
什么了?
答案 0 :(得分:3)
你有一个错字:
def initalize # typo
def initialize # correct
BTW,kind
中处理场景的手动方式较少:
def kind
:scalene if [@side1, @side2, @side3].uniq.length == 3
end