Ruby 中的空格规则是什么?
a = [2,4,5]
a.count-1 => 2
a.count - 1 => 2
但是
a.count -1 => 0
什么导致了这种行为呢?如果 a 是整数(而不是数组),为什么不会发生这种情况?
另外,我注意到方法名称和后面的括号(用于参数)之间不能有空格。这是为什么?
红宝石 1.9.2
a = [2,4,5]
a.count-1 => 2
a.count - 1 => 2
But
a.count -1 => 0
What causes this behavior? Why doesn't it happen if a is an integer (and not an array)?
Also, I have noticed that there must not be a space between a method name and the parenthesis that follows (for parameters). Why is that?
Ruby 1.9.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于可以在不使用括号的情况下调用方法,因此:
Means 从
a.count
中减去 1,而Means 使用
-1
调用方法a.count
> 作为参数。当a
是整数时,不会发生这种情况,因为整数没有count
方法。您只需在打字时小心即可。Because methods can be called with no parentheses, this:
Means subtract 1 from
a.count
, whereasMeans call the method
a.count
with-1
as an argument. It doesn't happen whena
is an integer because integers don't have thecount
method. You just have to be careful as you type.您确定不能在方法名称和括号之间添加空格吗?我刚刚在 ruby 1.9.2 中做到了,效果很好。例如
Are you sure you can't put a space between a method name and the parens? I just did it in ruby 1.9.2 and it works fine. For example
a.count -1
相当于a.count(-1)
。它返回数组中包含 -1 的次数。
a.count -1
is equivalent toa.count(-1)
.It returns the number of times -1 is included in your array.