【转】ruby 调用其他函数
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 name="code">if fork.nil? exec "ruby some_other_file.rb"endputs "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)
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!"endweb_browser = WIN32OLE.new('InternetExplorer.Application')web_browser.visible = trueweb_browser.navigate('http://www.baidu.com')