我有代码:
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
<<
是什么意思?
它可以有 3 个不同的含义:
'<<' as an ordinary method
在大多数情况下‘<<' is a method defined like the rest of them, in your case it means "add to the end of this array" (see also 这里)。
这是针对您的特定情况,但在很多其他情况下您也会遇到“<<" 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 '<<'
单例类定义
然后在程序流程中出现了当前作用域的神秘转变(=自我的改变):
class A
class << self
puts self # self is the singleton class of A
end
end
a = A.new
class << a
puts self # now it's the singleton class of object a
end
这个谜团
class << self
让我好奇并调查那里的内部结构。而在我提到的所有示例中,<<
实际上是类中定义的方法,即
obj << stuff
相当于
obj.<<(stuff)
class << self
(或任何代替自我的对象)构造确实不同。它实际上是语言本身的内置功能,在 CRuby 中,它在 parse.y as 中定义
k_class tLSHFT expr
k_class
是“class”关键字,其中 tLSHFT
是“<<' token and expr
”是任意表达式。也就是说,你实际上可以写
class << <any expression>
and 将 shifted 进入表达式结果的单例类。
tLSHFT
序列将被解析为“NODE_SCLASS”表达式,称为单例类定义(参见node.c)
case NODE_SCLASS:
ANN("singleton class definition");
ANN("format: class << [nd_recv]; [nd_body]; end");
ANN("example: class << obj; ..; end");
F_NODE(nd_recv, "receiver");
LAST_NODE;
F_NODE(nd_body, "singleton class definition");
break;
这里文件
此处文档使用 '<<' in a way that is again totally different. You can define a string that spans over multiple lines conveniently by declaring
here_doc = <<_EOS_
The quick brown fox jumps over the lazy dog.
...
_EOS_
为了区分“此处文档运算符”,任意字符串分隔符必须紧跟在“<<'. 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.
”之后主要用于数组中,将值追加到数组末尾。
a = ["orange"]
a << "apple"
puts a
给出了这个
[“橙色”,“苹果”]结果。
'a << b' means append b to the end of a
它是允许您通过appending新项目来提供现有数组的运算符。
在上面的示例中,您只是用 5 个新线程填充空数组
threads
。
在 Ruby 中,你总是有更多单一的方法来做事。因此,Ruby 对常用方法名称有一些不错的快捷方式。就像这个用于 .push 而不是键入 .push 方法名称,您可以简单地使用 <<, the concatenation operator. in fact in some cases you can use any of these for the same operation .push and + with <<.
就像您在这个示例中看到的那样:
alphabet = ["a", "b", "c"]
alphabet << "d" # Update me!
alphabet.push("e") # Update me!
print alphabet
caption = "the boy is surrounded by "
caption << "weezards!" # Me, too!
caption += " and more. " # Me, too!
# .push can no be uses for concatenate
print caption
所以你看到的结果是:
["a", "b", "c", "d", "e"]
the boy is surrounded by weezards! and more.
您可以使用运营商<< to push a element into an array or to concatenate a string to another.
所以,它所做的就是创建一个新的元素/对象 Thread 类型并将其推入数组中。
threads << Thread.new(s) {
create_thumbnail(source+'.png', source+'-'+s+'.png', s)
}
红宝石'<<' operator is basically used for:
在数组中追加一个值(在最后一个位置)
[2,4,6]<< 8 It will give [2, 4, 6, 8]
它还用于 ruby 中的一些活动记录操作。例如,我们有一个 Cart 和 LineItem 模型,关联为 cart has_many line_items。 Cart.find(A).line_items 将返回 ActiveRecord::Associations 对象,其中包含属于购物车“A”的订单项。
现在,将另一个 line_item (X) 添加(或关联)到购物车 (A),
Cart.find(A).line_items << LineItem.find(X)
现在将另一个 LineItem 添加到同一个购物车“A”中,但是这次我们不会创建任何 line_item 对象(我的意思是不会手动创建 activerecord 对象)
购物车.find(A).line_items << LineItem.new
在上面的代码中<< will save object and append it to left side active record association array.
以及上面的答案中已经涵盖的许多其他内容。
表示添加到末尾(append)。
a = [1,2,3]
a << 4
a = [1,2,3,4]