什么是<<在 Ruby 中是什么意思? [英] What does << mean in 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
<<
是什么意思?
推荐答案
它可以有 3 个不同的含义:
It can have 3 distinct meanings:
'<<'作为普通方法
在大多数情况下'<<'是一种与其余方法一样定义的方法,在您的情况下,它的意思是添加到此数组的末尾";(另见此处).
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 '<<'
- 字符串连接:a"<<"b"
- 将输出写入 IO:io <<一行文字\n"
- 将数据写入消息摘要、HMAC 或密码:sha <<要散列的文本"
- OpenSSL::BN 的左移:bn <<2
- ...
单例类定义
然后是程序流中当前作用域的神秘转移(=自身的改变):
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
神秘的class <<self
让我想知道并调查那里的内部结构.而在我提到的所有示例中,<<
实际上是在类中定义的方法,即
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)
类<
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
k_class
是class"关键字,其中 tLSHFT
是<<"token 和 expr
是任意表达式.也就是说,你实际上可以写
k_class
is the 'class' keyword, where tLSHFT
is a '<<' token and expr
is an arbitrary expression. That is, you can actually write
class << <any expression>
并且将转移到表达式结果的单例类中.tLSHFT
序列将被解析为NODE_SCLASS"表达式,称为单例类定义(参见 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;
这里的文档
而且,我怎么能忘记那些,此处的文档使用<<"以一种完全不同的方式.您可以通过声明
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_
为了区分here doc operator",任意字符串定界符必须紧跟在<<"之后.初始定界符和同一定界符第二次出现之间的所有内容都将成为最终字符串的一部分.也可以使用'<<-',区别在于使用后者将忽略任何前导或尾随空格.
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.
这篇关于什么是<<在 Ruby 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!