ruby 基础教程 P125 point.rb
class Point
attr_reader :x, :y
def initialize(x=0, y=0)
@x, @y = x, y
end
def inspect
"(#{x}, #{y})"
end
def [](index)
case index
when 0
x
when 1
y
else
raise ArgumentError ,"out of range '#{index}'"
end
end
def [](index,val)
case index
when 0
self.x = val
when 1
self.y = val
else
raise ArgumentError ,"out of range '#{index}'"
end
end
end
point = Point.new(1,2)
p point[0]
p point[1] = 2
运行后报出:
C:\Ruby200-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/Administrator/RubymineProjects/learning/point
C:/Users/Administrator/RubymineProjects/learning/point:23:in []': wrong number of arguments (1 for 2) (ArgumentError)
from C:/Users/Administrator/RubymineProjects/learning/point:39:in
'
from -e:1:in load'
from -e:1:in
Process finished with exit code 1
这是什么问题导致的?请高手帮忙看看。