问题描述
我有code:
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
什么呢&LT;&LT;
的意思是
推荐答案
'&LT;&LT;'作为一个普通的方法
在大多数情况下,&所述;&下;'就像是他们的休息定义的方法,在你的情况下,它的意思是添加到这个数组的结尾(见的)。
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).
这就是你的具体情况,但也有很多其他的场合,你会遇到&LT;&LT;方法。因为它真的是可以由你重写或你自己的对象实施了一些对象上定义的方法,我不会把它称为运算符。其他案件';&LT;&LT
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 '<<'
- 字符串连接:一个&LT;&LT; B
- 写输出到IO:IO&LT;&LT; 文字\\ n的行
- 将数据写入到一个消息摘要,HMAC或密码:SHA&LT;&LT; 文本被散列
- 左移一个OpenSSL的:: BN的:BN&LT;&LT; 2
- ...
Singleton类定义
再有就是当前范围的程序流程中的神秘移(=自我变革):
Then there is the mysterious shift of the current scope (=change of self) within the program flow:
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
之谜类&LT;&LT;自
让我不知道和调查有关的内部那里。而在所有的例子我提到&LT;&LT;
确实是在一个类中定义的方法,即
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.
obj << stuff
等同于
obj.<<(stuff)
在类&LT;&LT;自
(或代替自我的任何对象)构造是真正的不同。这确实是语言本身,在CRuby它定义的内置功能的 parse.y 的如
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 as
k_class tLSHFT expr
其中tLSHFT是一个'&LT;&LT;'令牌,k_class是'阶级'关键字和expr是任意的前pression。也就是说,你可以实际编写
where tLSHFT is a '<<' token, k_class is the 'class' keyword and expr is an arbitrary expression. That is, you can actually write
class << <any expression>
和将获得转移到单件类的前pression的结果。该tLSHFT序列将被解析为NODE_SCLASS前pression,这被称为的 Singleton类定义的(参见node.c)
and 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)
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;
这里的文件
和,我怎么能忘记那些,的这里的文件的使用'&LT;&LT;'的方式,是再次完全不同。您可以方便地通过声明定义跨越多行字符串
And, how could I forget those, Here Documents use '<<' 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_
要区分'在这里DOC运营商的任意字符串分隔符必须紧跟在'&LT;&LT;'。插图中的一切,最初的分隔符和相同的分隔符的第二次出现将是最终的字符串的一部分。也可以使用'&LT;&LT; - ,不同的是,使用后者将忽略任何开头或结尾的空格
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.
这篇关于&是什么LT;&LT;在Ruby中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!