在ruby 1.8.7
语法错误而不是当+ =
为什么这是确定:
string += method "value"
但是,这提出一个语法错误:
string << method "remove reviewer"
是一样的更新版本的红宝石的行为?
在ruby 1.8.7
语法错误而不是当+ =
为什么这是确定:
string += method "value"
但是,这提出一个语法错误:
string << method "remove reviewer"
是一样的更新版本的红宝石的行为?
您可以用不同的Operator Precedence的<<
和=+
和方法调用来解释此行为。
红宝石读取你的第一个例子是:
string += (method "value")
,但第二个是:
(string << method) "remove reviewer"
IMO是使用圆括号的方法调用,即使Ruby没有需要他们一个很好的做法在许多情况下。这使得代码更具可读性并且更少出错:
string += method("value")
string << method("remove reviewer")
是的,在更高版本的Ruby中也是如此。 (我在Ruby 2.2上测试过)。
这是因为Ruby的operator precedence。
要解决这个问题,你可以在使用的情况下的<<
括号:
string << method("remove reviewer")
然后,它应该工作,也就不会出现语法错误。
或者,使其保持一致,可以使用括号对他们俩的:
string += method("value")
string << method("remove reviewer")
逸岸,强烈建议使用括号()
的方法调用,以避免这种情况就像一个你”再问一次。查询this post了解更多信息。
是的,在更高版本的Ruby中也是如此。 (我在Ruby 2.2上测试过) –