什么是Hash[x] << "string"
做什么?
符号是什么<<
它是如何工作的?
最佳答案
真正的问题是, Hash[x]
做了什么? 评估到?
因为它是 那个对象(一个 Array,也许?),<<
就在上面运算符(实际上是一种方法)正在被调用。即 Hash[x] << "string"
是,不包括临时变量,相当于t = Hash[x]; t << "string"
.
喜欢所有overridable Ruby 运算符1,<<
实际上只是一个方法调用。它通常被视为 Array#<<
, 但对于所讨论的对象可能有所不同(见上文):
[On an Array object, the << operator] Append—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.
一旦知道实际对象,就可以在适当的文档中轻松查找运算符。
1 参见 list of ruby operators that can be overridden/implemented一份 list ; “纯”运算符,如 =
(非索引分配)和 ,
不能被覆盖并且不能以相同的方式工作。
关于ruby - Hash[x] << "string"是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21925510/