对什么“<<"感兴趣做 [英] Interested in what the "<<" does

查看:52
本文介绍了对什么“<<"感兴趣做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到此代码.

def setup(&block)
  @setups << block
end

这条线有什么作用?

@setups << block

对<<"的作用感兴趣.

Interested in what the does "<<".

说明书上说是双班制的操作员,但他在这里?

The manual says that it is the operator of double shift, but he is here with?

推荐答案

对于数组 << 是 append 方法.它将一个项目添加到数组的末尾.

For an array << is the append method. It adds an item to the end of the array.

因此,在您的特定情况下,当您使用块调用 setup 时,由块生成的 Proc 对象存储在 @setups 中.

So in your specific case when you call setup with a block the Proc object made from the block is stored in @setups.

注意:正如 sbeam 在他的评论中指出的那样,因为 << 是一个方法,它可以根据被调用的对象类型做不同的事情,例如字符串连接,整数位移等

Note: as sbeam points out in his comment, because << is a method, it can do different things depending on the type of object it is called on e.g. concatenation on strings, bit shifting on integers etc.

请参阅ary << obj → ary" 文档.

这篇关于对什么“&lt;&lt;"感兴趣做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆