- 1、本文档共22页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
timerbsucc返回一天后的时间单位scommandrb作用程序
1、time.rb
succ:返回一天后的时间,单位s
2、command.rb
作用:程序返回cmd执行的输出保留在output数组里以及执行的状态exit_status
class Command
def Command.run(cmd,max_time=15)
output=
pipe=IO.popen(#{cmd} 21,w+)
if pipe
begin
timeout(max_time) do
while !pipe.eof? do
output+=pipe.gets
end
end
rescue TimeoutError
raise #{cmd} timed out!\n\n#{output[0]}
end
Process.wait(pipe.pid)
if $?.signaled?
exit_status=$?.termsig
elsif $?.exited?
exit_status=$?.exitstatus
end
else
raise ERROR! Could not open pipe to #{cmd}
end
return [output,exit_status]
end
end
语法解析:
类函数的几种方式
class Example
def instance_method # instance method
end
def Example.class_method # class method
end
end
The value returned by a Ruby method is the value of the last expression evaluated, so we can get rid of the temporary variable and the return statement altogether.
Class methods are distinguished from instance methods by their definition; class methods
are defined by placing the class name and a period in front of the method name
上面的IO.open以及Process.wait(pipe.pid)都是class method的用法。
调用系统命令的几种方式:
irb(main):002:0 `ls account*` #这个结果带有\n,所以要用chomp来去除
= account.rb\naccount_params\n
irb(main):003:0 system(ls account*) #没有\n
account.rb account_params
= true
irb(main):005:0 pipe=IO.popen(ls account*) #执行子进程,并将结果输出到ruby标准输出
= #IO:0x2b704a3afd00
irb(main):006:0 pipe.gets #一次得到命令执行的一行结果
= account.rb\n
irb(main):008:0 pipe.gets
= account_params\n
irb(main):009:0 pipe.gets
= nil
IO类简介
standard input has an fd of 0, standard output an fd of 1, and standard error an fd of 2.
a = IO.new(2, w) # 2 is standard error
STDERR.puts Hello
a.puts World
produces:
Hello
World
IO.open(1, w) do |io|
io.puts Writing to stdout
end
produces:
Writing to stdout
pipe.pid 返回当前进程的id号
Process 和 Process::status 简介
Process.wait(pipe.pid) 等待进程的结束,返回进程的id号
pid = fork { sleep 100 }
Process.kill(9, pid) ! 1
Process.wait ! 27202
$?.signaled? ! true # 被异常信号中断的进程,如kill调的进程则返回true
exit
文档评论(0)