我有一个类的对象,我想用它复制它dup.其中一个实例变量是一个数组,它似乎引用了它.我以为dup实际上创造了一个DUPLICATE.
这是我的IRB会议:
irb(main):094:0> class G
irb(main):095:1> attr_accessor :iv
irb(main):096:1> def initialize
irb(main):097:2> @iv = [1,2,3]
irb(main):098:2> end
irb(main):099:1> end
=> nil
irb(main):100:0> a=G.new
=> #<G:0x27331f8 @iv=[1, 2, 3]>
irb(main):101:0> b=a.dup
=> #<G:0x20e4730 @iv=[1, 2, 3]>
irb(main):103:0> b.iv<<4
=> [1, 2, 3, 4]
irb(main):104:0> a
=> #<G:0x27331f8 @iv=[1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我希望a不会改变,因为dup创建一个全新的变量,而不是引用.
另请注意,如果[1,2,3]要用标量替换G::initialize,dup则不会引用它.
在这个C计划中
#include <stdio.h>
#include <fcntl.h>
int main()
{
int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU);
dup2(stdout, file);
system("ls -l");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将system()的输出重定向到一个文件,因为我已经使用了dup2但它无法正常工作.
这段代码出了什么问题?
如果有更好的方法可以告诉我吗?(不在>终端使用)
我试图从Perl设置任意管道,就像shell一样.
这有所期望的效果,就像"echo foo | sed s/oo/ar /":
#!/usr/bin/perl
use strict;
use IO::Handle;
$| = 1;
my ($first_prog, $second_prog) = ([qw(echo foo)], [qw(sed s/oo/ar/)]);
#$second_prog = ["less"];
pipe(my $second_prog_input, my $first_prog_output)
or die "problem setting up pipe: $!\n";
if (fork) {
STDOUT->fdopen(fileno($first_prog_output), 'w') or die;
exec(@$first_prog) or die;
}
else {
STDIN->fdopen(fileno($second_prog_input), 'r') or die;
exec(@$second_prog) or
die "couldn't exec: $!: command was @$first_prog\n";
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将第二个参数设置为"less"时,我的终端会闪烁,而我在寻呼机中看不到输出.除了短暂的闪光,没有任何迹象表明运行较少.
现在我完全没有得到的是以下内容的行为类似于"echo foo | less":
pipe(my $first_prog_output, STDIN) or die "problem setting up pipe: $!\n";
my …Run Code Online (Sandbox Code Playgroud) 我正在编写一些测试Test::More,并且我正在测试打印的功能之一STDERR.我想测试输出STDERR,但有点不确定如何做到这一点.我知道我很亲密.这有效:
use strict;
use warnings;
use feature qw(say);
close STDERR;
open STDERR, ">", \my $error_string;
say STDERR "This is my message";
say qq(The \$error_string is equal to "$error_string");
Run Code Online (Sandbox Code Playgroud)
打印出:
The $error_string is equal to "This is my message
"
Run Code Online (Sandbox Code Playgroud)
但是,我不想关闭STDERR.我只是想重复它.
我试过这个:
use strict;
use warnings;
use feature qw(say);
open my $error_fh, ">", my $error_string;
open STDERR, ">&", $error_fh;
say STDERR "This is my message";
close $error_fh;
say qq(The \$error_string is equal to "$error_string");
Run Code Online (Sandbox Code Playgroud)
但是, …
我在这个错误中停留了相当长的时间,已经走到了尽头。
我得到这个完全无益的错误
can't dup NilClass
Run Code Online (Sandbox Code Playgroud)
就是这种情况。
我有一类与另一类有关系。说
class Parent
end
class Child < Parent
unloadable
:has_many :parents, :foreign_key => "child"
end
Run Code Online (Sandbox Code Playgroud)
首次访问该错误不会发生。它是第二次访问该孩子。
究竟是什么导致此错误,并且有解决方案?
我引用了以下链接,但无济于事
更新资料
我发现了这个
但这又暗示了同样的道理。但是我的lib中确实有一个模块。它与模型无关。
我正在尝试将STDOUT和STDERR重定向到套接字.
我做了:
if(fork() == 0)
{
dup2(newsock, STDOUT_FILENO);
dup2(newsock, STDERR_FILENO);
execvp();
}
Run Code Online (Sandbox Code Playgroud)
不知何故,它只显示了输出的第一个小部分.
例如,当我尝试执行ls或mkdir时,它显示在"mkdir"上.
有什么问题?
我尝试了它的工作,但我只能重定向STDOUT或STDERR之一
close(1);
dup(newsock);
Run Code Online (Sandbox Code Playgroud)
非常感谢.
我现在面对这个问题一段时间了,请仔细研究一下.
object.inspect给了我这个输出
<RawMaterial id: nil, name: "Jam Button 9 mm Antique Silver", rate: 1.0, raw_material_wastage: 0.0, total_raw_material: 8.0, slug: nil, costing_id: nil, created_at: nil, updated_at: nil, inventory_item_id: 758, costing_wastage: 0.0, pick_from_order_sheet: false>
Run Code Online (Sandbox Code Playgroud)
raise object.to_yaml给出了这个输出
-- !ruby/object:RawMaterial
raw_attributes:
costing_id:
id:
name: Jam Button 9 mm Antique Silver
rate: '1'
raw_material_wastage: '0'
total_raw_material: '8'
slug:
created_at:
updated_at:
inventory_item_id: '758'
costing_wastage: '0'
pick_from_order_sheet: f
attributes: !ruby/object:ActiveRecord::AttributeSet
attributes: !ruby/object:ActiveRecord::LazyAttributeHash
types:
id: &3 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer
precision:
scale:
limit:
range: !ruby/range
begin: -2147483648
end: 2147483648
excl: true …Run Code Online (Sandbox Code Playgroud) 在一本关于C编程的(德语)书中(Linux-UNIX-Programmierung,由JürgenWolf)我发现了一个声明,翻译成英文如下(由我编号的句子):
在某些情况下,您可能需要复制文件描述符[1].这样做的一个例子是,如果父进程想要与子进程交换数据,并且子进程通过使用
exec*()[2] 被新进程覆盖.在这种情况下,没有dup() or dup2(),将设置close-on-exec标志[3].设置此标志后,所有文件描述符都将变为无效(因为被新进程覆盖) - 也就是说,它们不再存在[4].因此,父母和子女过程之间的沟通将被停止[5].另一方面,如果使用复制文件描述符dup() or dup2(),则删除close-on-exec标志,并且新覆盖的进程可以使用此文件描述符进行通信[6].
我认为上段包含一些误导性陈述甚至错误.
在句子[3]中,我不明白为什么没有使用dup()或者dup2()设置close-on-exec标志?
目前,我正在编写一个小的外壳程序(重定向,管道,exec等)。一直在尝试弄清楚Linux Shell在解决I / O重定向方面采取的步骤。
关于我需要帮助的一些问题:
寻找重定向时,shell从命令行读取哪个方向?从左到右还是相反?使用递归?
外壳需要寻找什么情况?(不确定是否有很多或只有一对可以包含很多变化)
无论如何,我能想到的是一些(如果我错了,请纠正我):
cmd > file1 # stdout of cmd goes to file
cmd file1 > file2 # stdout of cmd with file1 as an argument goes to file2
cmd file2 < file1 # stdin on file2 comes from file1
Run Code Online (Sandbox Code Playgroud)
现在,我不知道在以下情况下的过程(如外壳如何查找和处理这些情况)。Shell所采取的步骤对我来说是未知的
cmd file2 > file3 < file1 # using "tee" in place of "cmd" I don't know
# how to do the dups and when to exec
cmd file2 < file3 > file1 # same …Run Code Online (Sandbox Code Playgroud) 背景:我们有一个运行 Busybox 的嵌入式 Linux 系统(资源有限),我们有一个正在运行的进程,它通常会在控制台上(通过 stdout / stderr)吐出大量信息,但我们想暂时将其重定向到 syslog(使用 syslogd / logger) on command - 无需重新启动进程或重新启动。
使用此处找到的代码效果很好,直到我们尝试停止/关闭记录器 fd,此时写入 stdout/stderr 失败并且一切都中断了。下面的例子:
int main()
{
// Got command to direct all output to syslog:
FILE *fl;
fl = popen("logger","w");
if(fl == NULL)
return 1;
fprintf(fl,"logger test new"); // This goes to syslogd
int nf;
nf = fileno(fl);
dup2(nf,STDOUT_FILENO);
dup2(nf,STDERR_FILENO);
fprintf(stdout,"Written in stdout\n");
fprintf(stderr,"Written in stderr\n");
// ...some time later when we've logged enough things:
pclose(fl);
fprintf(stdout,"This will …Run Code Online (Sandbox Code Playgroud)