gpt4 book ai didi

ruby - Hash[x] << "string"是做什么的?

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:34 25 4
gpt4 key购买 nike

什么是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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com