简体   繁体   English

无法理解`puts {}。class`和`puts({} .class)`之间的区别

[英]Couldn't understand the difference between `puts{}.class` and `puts({}.class)`

As the anonymous block and hash block looks like approximately same. 由于匿名块和哈希块看起来大致相同。 I was doing kind of playing with it. 我正在玩它。 And doing do I reached to some serious observations as below: 我这样做了一些严肃的观察,如下所示:

{}.class
#=> Hash

Okay,It's cool. 好的,很酷。 empty block is considered as Hash . 空块被视为Hash

print{}.class
#=> NilClass
puts {}.class

#=> NilClass

Now why the above code showing the same as NilClass ,but the below code shows the Hash again ? 现在为什么上面的代码显示与NilClass相同,但下面的代码再次显示了Hash

puts ({}.class)
#Hash
#=> nil
print({}.class)
#Hash=> nil

Could anyone help me here to understand that what's going one above? 谁能帮助我在这里了解上面会发生什么?

I completely disagree with the point of @Lindydancer 我完全不同意@Lindydancer的观点

How would you explain the below lines: 你会如何解释以下几行:

print {}.class
#NilClass
print [].class
#Array=> nil
print (1..2).class
#Range=> nil

Why not the same with the below print [].class and print (1..2).class ? 为什么不同以下print [].classprint (1..2).class

EDIT 编辑

When ambiguity happens with local variable and method call, Ruby throws an error about the fact as below : local variablemethod调用出现歧义时,Ruby会抛出关于以下事实的错误:

name
#NameError: undefined local variable or method `name' for main:Object
#        from (irb):1
#        from C:/Ruby193/bin/irb:12:in `<main>'

Now not the same happens with {} (as there is also an ambiguity between empty code block or Hash block). 现在与{}不一样(因为empty code blockHash块之间也存在歧义)。 As IRB also here not sure if it's a empty block or Hash . 因为IRB在这里也不确定它是empty block还是Hash Then why the error didn't throw up when IRB encountered print {}.class or {}.class ? 那么当IRB遇到print {}.class{}.class时,为什么错误没有出现?

The precedence rules of ruby makes print{}.class interpreted as (print{}).class . ruby的优先规则使得print{}.class解释为(print{}).class As print apparently returns a nil the class method returns #NilClass . 由于print显然返回nil因此class方法返回#NilClass

EDIT : As been discussed on other answers and in the updates to the question, print{} it of course interpreted as calling print with a block, not a hash. 编辑 :正如在其他答案和问题的更新中所讨论的那样, print{}当然被解释为使用块调用print ,而不是哈希。 However, this is still about precedence as {} binds stronger than [] and (1..2) (and stronger than do ... end for that matter). 然而,这仍然是优先级,因为{}[](1..2)更强大(并且比do ... end更强的do ... end )。

{} in this case is recognized as block passed to print , while [] unambiguously means empty array. 在这种情况下, {}被识别为传递给print块,而[]明确地表示空数组。

print {}.class                            # => NilClass
print do;end.class                        # => NilClass

You are running into some nuances of Ruby, where characters mean different things depending on context. 你正在遇到Ruby的一些细微差别,根据上下文,字符意味着不同的东西。 How the source code is interpreted follows rules, one of which is that {} is a closure block if it follows a method call , and otherwise a Hash constructor. 如何解释源代码遵循规则,其中之一是{}是闭包块, 如果它遵循方法调用 ,否则是Hash构造函数。

It's common throughout the language to see characters mean different things depending on context or position within the statement. 在整个语言中,根据语句中的上下文或位置来看字符意味着不同的事物是很常见的。


Examples: 例子:

Parens () used for method call or for precedence Parens ()用于方法调用或优先级

print(1..5).class => NilClass
print (1..5).class => Range <returns nil>

Square brackets [] used to call :[] method or for Array 方括号[]用于调用:[]方法或用于数组

print[].class => NoMethodError: undefined method `[]' for nil:NilClass
print([].class) => Array <returns nil>

Asterisk * used for multiplication or splatting 星号*用于乘法或喷溅

1 * 5 => 5
[*1..5] => [1, 2, 3, 4, 5]

Ampersand & used for symbol -> proc or logical and &符号&用于符号 - > proc或逻辑和

0 & 1 => 0
[1, 2, 3].map(&:to_s) => ["1", "2", "3"]

Or in your case, braces used for block closures or for a hash 或者在您的情况下,用于块闭包或哈希的大括号

... hope it makes sense now ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM