提问人:readyready15728 提问时间:10/12/2019 更新时间:10/12/2019 访问量:521
如何修复有关结束关键字的Ruby语法错误?
How can I fix this Ruby syntax error concerning the end keyword?
问:
这是来自Exercism的一个问题。到目前为止,将每个解决方案都变成类方法似乎是一种奇怪的处理方式,但这不是我的想法。无论如何,这是代码:
class ResistorColorDuo
@@colors = %w(
black
brown
red
orange
yellow
green
blue
violet
grey
white)
def self.value(array)
@@colors.index array[0] * 10 + @@colors.index array[1]
end
end
错误消息:
Traceback (most recent call last):
1: from resistor_color_duo_test.rb:2:in `<main>'
resistor_color_duo_test.rb:2:in `require_relative': /home/muhammad/exercism/ruby/resistor-color-duo/resistor_color_duo.rb:15: syntax error, unexpected tIDENTIFIER, expecting keyword_end (SyntaxError)
...0]) * 10 + @@colors.index array[1]
我看不出问题,因为关键字都正确平衡了。我应该如何解决这个问题?end
答:
评论部分@dinjas答案的补充
如果有两个数组:
colors = [1,10]
array = [1]
并执行示例中所示的操作:
colors.index array[0] * 10
您将获得数组的第二个元素的索引,即 .这是因为 Ruby 将首先计算(即 ),然后检查数组中相等的第一个元素的索引。#=> 1
colors
10
array[0] * 10
1 * 10 = 10
10
colors
但是,如果添加括号并执行此操作:
colors.index(array[0]) * 10
你会得到,因为数组中等于 () 的第一个元素的索引是 和 。#=> 0
1
array[0] = 1
colors
0
0 * 10 = 0
几天前也有一个类似的问题,你可能想在这里看看。
我的印象是,在这种情况下,括号是可选的。
是的,也不是。括号确实是可选的,但这并不意味着不使用它们不会改变代码的行为,只是不会出现语法错误。
如果 Ruby 在方法后面遇到空格,它会将该行的其余部分解析为其参数。因此,如果行上没有任何不属于方法参数的内容,则括号是可选的。如果这样做,则必须将行的哪一部分括在括号中来指定构成参数列表的部分。
下面是一个需要注意的相关潜在问题。请考虑以下行,它们仅相差一个空格:
f(3+2)+1
f (3+2)+1
其中第一个传递给函数并添加到结果中。第二个传递给函数 。出于同样的原因:如果 Ruby 在方法调用后立即遇到空格,则该行的其余部分将被解析为参数列表。5
f
1
6
f
我从Matz的书中借用了这个例子,他在书中雄辩地称之为“有害的空白依赖”。您必须小心方法调用行上的空格。
这也是您遇到错误的原因。Ruby 无法解析你的行,因为你有两个方法调用。因此,第二个导致了错误:Ruby 需要关键字,但它遇到了另一个方法调用。end
在第二个括号中添加括号可以消除语法错误:
@@colors.index array[0] * 10 + @@colors.index(array[1])
但它可能不会给你你想要的结果。它将像这样解析它:
@@colors.index(array[0] * 10 + @@colors.index(array[1]))
因此,请为它们使用括号。我不确定你想做什么(不确定你想乘以 10 ),但像这样:
@@colors.index(array[0]) * 10 + @@colors.index(array[1])
通常,我更喜欢将参数括起来以方法调用。几乎唯一不使用它们的时候是 和 (编辑:以及我忘记了 Jörg 在下面的评论中添加的所有其他那些)。puts
print
p
请注意不要在方法调用和打开的 paren 之间放置空格。
评论
puts
print
p
require
require_relative
load
include
extend
prepend
attr_reader
attr_writer
attr_accessor
评论
@@colors.index(array[0]) * 10 + @@colors.index(array[1])
@@colors.index array[1]
@@colors.index(array[1])
@@colors.index((array[0] * 10) + @@colors.index(array[1]))
if array.include? element
index
end
index
index
tIDENTIFIER
end
end