def [](index)
case index
when 0, -2: @x
when 1, -1: @y
when :x, "x": @x
when :y, "y": @y
else nil
end
end
Run Code Online (Sandbox Code Playgroud)
array.rb:3: syntax error, unexpected ':', expecting keyword_then or ',' or ';' or '\n'
when 0, -2: @x
^
array.rb:5: syntax error, unexpected ':', expecting keyword_end
when :x, "x": @x
^
array.rb:6: syntax error, unexpected ':', expecting keyword_end
when :y, "y": @y
^
array.rb:8: warning: else without rescue is useless
array.rb:9: syntax error, unexpected keyword_end, expecting $end
Run Code Online (Sandbox Code Playgroud)
我是按照Ruby编程语言这本书写的.
我的ruby版本是ruby 1.9.3p0
谁看过这个吗?
我建议你使用普通的情况......当时表格
def [](index)
case index
when 0, -2 then @x
when 1, -1 then @y
when :x, "x" then @x
when :y, "y" then @y
end
end
Run Code Online (Sandbox Code Playgroud)