【转】ruby 调用其他函数

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:05   608   0
1、获取其他程序的运行结果

x= system("date")

x='date'

x=%x{date}



2、调用其他程序,同时把执行权移交给被调程序

exec "shutdown -s -t 0"

puts "this will never be displayed!"



3、同时运行两个程序

forking就是值程序的实例进程复制自身,导致该程序的两个进程并发运行。


if fork.nil?<pre class="ruby" name="code">if fork.nil?
exec "ruby some_other_file.rb"
end

puts "this ruby script now run!"


4、与另一个程序进行交互

5、安全级别$SAFE,在drb中有使用过该特性,防止客户端调用server服务执行非法操作

6、使用window的API
[code = "ruby"]
require "Win32API"

title = "My Application"
text = "Hello world"
Win32API.new('user32','MessageBox' , %w{L P P L},'I').call(0,text,title,0)
[/code]
7、控制windows程序

对windows的自动化访问是通过ruby的WIN32OLE程序库实现的,例如

[code]
require "Win32API"
require "win32ole"

title = "My Application"
text = "Hello world"
result = Win32API.new('user32','MessageBox' , %w{L P P L},'I').call(0,text,title,1)

case result
when 1
puts "clicked OK"
when 2
puts "clicked cancel!"
else
puts "clicked something else!"
end

web_browser = WIN32OLE.new('InternetExplorer.Application')
web_browser.visible = true
web_browser.navigate('http://www.baidu.com')
[/code]

转自: [url]http://blog.csdn.net/zhanggs007/article/details/7606525[/url]
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP