在Ruby的文档的dup
说:
在一般情况下,
clone
并dup
可能在派生类不同的语义.虽然clone
用于复制对象(包括其内部状态),但dup
通常使用后代对象的类来创建新实例.
但是当我做一些测试时,我发现它们实际上是相同的:
class Test
attr_accessor :x
end
x = Test.new
x.x = 7
y = x.dup
z = x.clone
y.x => 7
z.x => 7
Run Code Online (Sandbox Code Playgroud)
那么这两种方法有什么区别?
我有一个哈希:
h = {'name' => 'sayuj',
'age' => 22,
'project' => {'project_name' => 'abc',
'duration' => 'prq'}}
Run Code Online (Sandbox Code Playgroud)
我需要一个这个哈希的副本,更改不应该影响原始哈希.
当我尝试时,
d = h.dup # or d = h.clone
d['name'] = 'sayuj1'
d['project']['duration'] = 'xyz'
p d #=> {"name"=>"sayuj1", "project"=>{"duration"=>"xyz", "project_name"=>"abc"}, "age"=>22}
p h #=> {"name"=>"sayuj", "project"=>{"duration"=>"xyz", "project_name"=>"abc"}, "age"=>22}
Run Code Online (Sandbox Code Playgroud)
在这里,您可以看到project['duration']
原始哈希中的更改是因为project
是另一个哈希对象.
我想要散列duped
或cloned
递归.我怎样才能做到这一点?
我知道dup,dup2,dup3" 创建文件描述符oldfd的副本 "(来自手册页).但是我无法消化它.
据我所知文件描述符只是数字来跟踪文件的位置和它们的方向(输入/输出).是不是更容易
fd=fd2;
Run Code Online (Sandbox Code Playgroud)
每当我们想复制文件描述符?
还有别的......
dup()使用编号最小的未使用描述符作为新描述符.
这是否意味着它还可以作为值stdin,stdout或stderr,如果我们假设我们有close() -其中一个?
Ruby的dup和clone方法有什么区别?描述dup
和的行为的差异clone
.但是什么时候应该使用dup
,何时应该使用clone
?
实际项目的例子讨论了为什么他们使用dup而不是克隆,反之亦然,这对于这个问题来说是理想的.
或者,解释为什么存在两种不同的方法会有所帮助.这可能是指从红宝石的创造者,或类似的方法检查报表dup
,并clone
在影响Ruby等语言.
我正在研究一个Linux C项目,我在使用文件描述符时遇到了麻烦.
我有一个孤立文件描述符(文件是open()'然后取消链接()'但fd仍然很好)具有只写权限.原始后备文件具有完全权限(使用S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH创建),但是文件是使用O_WRONLY打开的.是否可以复制文件描述符并将副本更改为O_RDWR?
psudo代码:
//open orphan file
int fd = open(fname, O_WRONLY, ...)
unlink(fname)
//fd is still good, but I can't read from it
//...
//I want to be able to read from orphan file
int fd2 = dup(fd)
//----change fd2 to read/write???----
Run Code Online (Sandbox Code Playgroud)
提前致谢!-安德鲁
arr = ["red","green","yellow"]
arr2 = arr.clone
arr2[0].replace("blue")
puts arr.inspect
puts arr2.inspect
Run Code Online (Sandbox Code Playgroud)
生产:
["blue", "green", "yellow"]
["blue", "green", "yellow"]
Run Code Online (Sandbox Code Playgroud)
无论如何都要做一个字符串数组的深层副本,除了使用Marshal,因为我知道这是一个黑客.
我可以:
arr2 = []
arr.each do |e|
arr2 << e.clone
end
Run Code Online (Sandbox Code Playgroud)
但它似乎并不优雅或高效.
谢谢
我有一项工作,我正在努力完成它.想法是编写一个执行一个程序的程序if.c,如果成功,则执行第二个程序.我应该抑制第一个程序的标准输出,并取消第二个程序的标准输出.我在多次测试中收到错误消息.例如:"./ if echo no then echo yes"返回"echo:write error:Bad file descriptor".我试过找到我在网上做错了但没有运气.
这是我的代码:
#include <fcntl.h>
#include <sys/wait.h>
#include <stdio.h>
#include "tlpi_hdr.h"
int main(int argc, char *argv[])
{
if(argc < 4){
fprintf(stderr,"Incorrect number of arguments.\n");
exit(EXIT_FAILURE);
}
int thenArg = 0;
char then[4];
strcpy(then,"then");
for(int x=1; x<argc; x++){
if(strncmp(argv[x], then, 4) == 0) thenArg = x;
}
if(thenArg == 0){
fprintf(stderr,"No 'then' argument found.\n");
exit(EXIT_FAILURE);
}
int save_out = dup(STDOUT_FILENO);
if(save_out == -1){
fprintf(stderr,"Error in dup(STDOUT_FILENO)\n");
exit(EXIT_FAILURE);
}
int devNull = open("/dev/null",0);
if(devNull …
Run Code Online (Sandbox Code Playgroud) 从python模块我调用一个Hello World可执行文件,只是打印Hello World
到stdout.我有兴趣将该输出重定向到python StringIO
并遇到这个答案,这几乎让我一直到解决方案.
这个答案的关键部分是这段代码:
1. def redirect_stdout():
2. print "Redirecting stdout"
3. sys.stdout.flush() # <--- important when redirecting to files
4. newstdout = os.dup(1)
5. devnull = os.open('/dev/null', os.O_WRONLY)
6. os.dup2(devnull, 1)
7. os.close(devnull)
8. sys.stdout = os.fdopen(newstdout, 'w')
Run Code Online (Sandbox Code Playgroud)
此外,我想恢复重定向之前的stdout.
dup
和dup2
做什么?/dev/null
?sys.stdout = os.fdopen(newstdout, 'w')
)StringIO
对象中?我很确定,一旦我得到了问题1的答案,问题2和3的答案就很容易了.无论如何我决定发布它们可能会将问题1的答案推到我想去的方向.
我有一个Pointer
具有单个属性的类:contents
,它指向类的对象MyObject
.
class MyObject
def hello; "hello" end
end
class Pointer
attr_reader :contents
def initialize( cont ); @contents = cont end
# perhaps define some more state
end
Run Code Online (Sandbox Code Playgroud)
我希望我Pointer
能够复制自己.我知道该#dup
方法是默认定义的,而#clone
方法应该被覆盖以便能够进行深层复制.但在这里,副本不必太深.所以,#dup
我遇到的第一个困境是,我应该覆盖方法,因为我真的不想复制我的附加状态Pointer
,只是创建一个指向同一个MyObject
实例的新状态?或者我应该避免过度使用#dup
,因为我不是"应该"并#clone
用一种制作浅拷贝的方法来覆盖?
我欢迎对上述内容发表评论,但我要说我会选择覆盖#dup
.我能做到这一点:
class Pointer
def dup; self.class.new( contents ) end
end
Run Code Online (Sandbox Code Playgroud)
但是在网上,我读过类似" dup方法将调用初始化复制方法"的内容.此外,这家伙写的#initialize_clone
,#initialize_dup
并#initialize_copy
在Ruby中.这让我感到疑惑,是最好的做法也许是这样的?
class Pointer
def initialize_copy
# …
Run Code Online (Sandbox Code Playgroud)