<< 是什么意思?在 Ruby 中是什么意思?
我有代码:
def make_all_thumbs(source)
sizes = ['1000','1100','1200','800','600']
threads = []
sizes.each do |s|
threads << Thread.new(s) {
create_thumbnail(source+'.png', source+'-'+s+'.png', s)
}
end
end
<<
是什么意思?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它可以有 3 个不同的含义:
'<<'作为普通方法
在大多数情况下 '<<'是一个像其他方法一样定义的方法,在您的情况下,它意味着“添加到该数组的末尾”(另请参见 此处)。
这是针对您的特定情况,但在很多其他情况下您也会遇到“<<”方法。我不会将其称为“运算符”,因为它实际上是在某个对象上定义的方法,可以由您覆盖或为您自己的对象实现。 '<<' 的其他情况
单例类定义
然后,程序流程中存在当前作用域的神秘转变(=自身的更改):
神秘的
class << self
让我想知道并调查那里的内部结构。而在我提到的所有示例中<<
实际上是在类中定义的方法,即相当于
class <<
self(或任何代替 self 的对象)构造确实不同。它实际上是语言本身的内置功能,在 CRuby 中,它在 parse.y 中定义为k_class
是“class”关键字,其中tLSHFT
是一个 '<<' token 和expr 是任意表达式。也就是说,您实际上可以编写并将转移到表达式结果的单例类中。
tLSHFT
序列将被解析为“NODE_SCLASS”表达式,称为单例类定义(参见node.c)此处文档
此处文档使用“<<”以一种完全不同的方式。您可以通过声明来方便地定义跨多行的字符串
。为了区分“here doc 运算符”,任意字符串分隔符必须紧跟在“<<”之后。初始分隔符和第二次出现的同一分隔符之间的所有内容都将成为最终字符串的一部分。也可以使用“<<-”,区别在于使用后者将忽略任何前导或尾随空格。
It can have 3 distinct meanings:
'<<' as an ordinary method
In most cases '<<' is a method defined like the rest of them, in your case it means "add to the end of this array" (see also here).
That's in your particular case, but there are also a lot of other occasions where you'll encounter the "<<" method. I won't call it 'operator' since it's really a method that is defined on some object that can be overridden by you or implemented for your own objects. Other cases of '<<'
Singleton class definition
Then there is the mysterious shift of the current scope (=change of self) within the program flow:
The mystery
class << self
made me wonder and investigate about the internals there. Whereas in all the examples I mentioned<<
is really a method defined in a class, i.e.is equivalent to
the
class << self
(or any object in place of self) construct is truly different. It is really a builtin feature of the language itself, in CRuby it's defined in parse.y ask_class
is the 'class' keyword, wheretLSHFT
is a '<<' token andexpr
is an arbitrary expression. That is, you can actually writeand will get shifted into the singleton class of the result of the expression. The
tLSHFT
sequence will be parsed as a 'NODE_SCLASS' expression, which is called a Singleton Class definition (cf. node.c)Here Documents
Here Documents use '<<' in a way that is again totally different. You can define a string that spans over multiple lines conveniently by declaring
To distinguish the 'here doc operator' an arbitrary String delimiter has to immediately follow the '<<'. Everything inbetween that initial delimiter and the second occurrence of that same delimiter will be part of the final string. It is also possible to use '<<-', the difference is that using the latter will ignore any leading or trailing whitespace.
主要用于数组中,将值附加到数组末尾。
给出这个
result.
Mostly used in arrays to append the value to the end of the array.
gives this
result.
'一<< b' 表示将 b 添加到 a 的末尾
'a << b' means append b to the end of a
该运算符允许您通过附加新项目来提供现有数组。
在上面的示例中,您只是用 5 个新线程填充空数组
threads
。It's the operator which allows you to feed existing arrays, by appending new items.
In the example above you are just populating the empty array
threads
with 5 new threads.在 Ruby 中,你总是有更多单一的方法来做事。因此,Ruby 对常用方法名称有一些不错的快捷方式。就像这个用于 .push 而不是键入 .push 方法名称,您可以简单地使用 <<(串联运算符)。事实上,在某些情况下,您可以将其中任何一个用于相同的操作 .push 和 + 以及 <<。
就像您在这个示例中看到的那样:
所以您看到的结果是:
您可以使用运算符 <<将一个元素推入数组或将一个字符串连接到另一个。
所以,它所做的就是创建一个新的元素/对象 Thread 类型并将其推入数组中。
In ruby you always have more the one way to do things. So, Ruby has some nice shortcuts for common method names. like this one is for .push instead of typing out the .push method name, you can simply use <<, the concatenation operator. in fact in some cases you can use any of these for the same operation .push and + with <<.
Like you can see in this example:
so you see the result is:
you can use the operator << to push a element into an array or to concatenate a string to another.
so, what this is this doing is creating a new element/object Thread type and pushing it into the array.
在红宝石中 '<<'运算符主要用于:
在数组中附加一个值(在最后一个位置)
[2,4,6]<< 8
它将给出 [2, 4, 6, 8]
它也用于 ruby 中的一些活动记录操作。例如,我们有一个 Cart 和 LineItem 模型,关联为 cart has_many line_items。 Cart.find(A).line_items 将返回 ActiveRecord::Associations 对象,其中包含属于购物车“A”的订单项。
现在,要将另一个 line_item (X) 添加(或关联)到购物车 (A),
现在将另一个 LineItem 添加到同一购物车“A”,但这次我们不会创建任何 line_item 对象(我的意思是不会手动创建 activerecord 对象)
Cart.find(A).line_items << LineItem.new
在上面的代码中<<将保存对象并将其附加到左侧活动记录关联数组。
上面的答案已经涵盖了许多其他内容。
In ruby '<<' operator is basically used for:
Appending a value in the array (at last position)
[2, 4, 6] << 8
It will give [2, 4, 6, 8]
It also used for some active record operations in ruby. For example we have a Cart and LineItem model associated as cart has_many line_items. Cart.find(A).line_items will return ActiveRecord::Associations object with line items that belongs to cart 'A'.
Now, to add (or say to associate) another line_item (X) to cart (A),
Now to add another LineItem to the same cart 'A', but this time we will not going to create any line_item object (I mean will not create activerecord object manually)
Cart.find(A).line_items << LineItem.new
In above code << will save object and append it to left side active record association array.
And many others which are already covered in above answers.
另外,从 Ruby 2.6 开始,
<<
方法也在Proc
上定义。Proc#<< 允许组成两个或多个过程。
Also, since Ruby 2.6, the
<<
method is defined also onProc
.Proc#<< allows to compose two or more procs.
意思是添加到末尾(追加)。
It means add to the end (append).