【发布时间】:2019-01-05 14:57:18
【问题描述】:
我正在寻找如何在 ruby 中调用 clone。
我认为它必须记录在这里 https://ruby-doc.org/core-2.1.2/Process.html 但它不是。
但是,在我的脚本中,如果我只是尝试print clone,它会给我类似 main 之类的东西。我找不到任何文档。
有什么想法吗?
【问题讨论】:
我正在寻找如何在 ruby 中调用 clone。
我认为它必须记录在这里 https://ruby-doc.org/core-2.1.2/Process.html 但它不是。
但是,在我的脚本中,如果我只是尝试print clone,它会给我类似 main 之类的东西。我找不到任何文档。
有什么想法吗?
【问题讨论】:
我从你的问题中得到的是你想要clone 文档。
为方便起见,请安装 pry gem
gem install pry pry-doc
然后从命令行输入pry --simple-prompt,然后显示克隆文档
>> show-doc clone
From: object.c (C Method):
Owner: Kernel
Visibility: public
Signature: clone(*arg1)
Number of lines: 19
Produces a shallow copy of obj---the instance variables of
obj are copied, but not the objects they reference.
clone copies the frozen (unless :freeze keyword argument
is given with a false value) and tainted state of obj.
See also the discussion under Object#dup.
class Klass
attr_accessor :str
end
s1 = Klass.new #=> #<Klass:0x401b3a38>
s1.str = "Hello" #=> "Hello"
s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello">
s2.str[1,4] = "i" #=> "i"
s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
This method may have class-specific behavior. If so, that
behavior will be documented under the #initialize_copy method of
the class.
HTH
【讨论】:
Ruby 有一个克隆方法,但它与分叉或进程无关。 Ruby 中的 clone 方法用于制作消息接收者的浅表副本。在上面的示例中,没有指定接收器,因此它可能是默认接收器,您猜对了,称为“main”
更多关于Ruby clone的信息,请查看:clone docs
【讨论】: