class Point attr_accessor:x,:y protected :x=,:y=
def initialize(x=0.0,y=0.0) @x,@y = x,y end
def swap(other) temp_x,temp_y=@x,@y
other.x,other.y=temp_x,temp_y return self
end
end
p0=Point.new p1=Point.new(1.0,2.0)
p0.swap(p1)
在 Mac 运行的时候,提示:
Traceback (most recent call last):
1: from point.rb:24:in <main>'
point.rb:9:in
swap': wrong number of arguments (given 0, expected 1) (ArgumentError)
指出传入的参数为 0,但是@x,@y=other.x,other.y 中,other.x,other.y 这样就不算参数了?需要如何去修改这个代码?
新手不懂,麻烦告知。谢谢