Ruby语言:Ruby程序运行外部程序(三)

Posted by cnruby Sat, 26 Jan 2008 21:53:00 GMT

Ruby语言:Ruby程序运行外部程序(三)

(.^.)说明:这是一个简单的Windows程序实例应用程序。在下面这个程序中,最后一行代码的函数不能使用Ruby语言的system函数。从中可以看到各种方法之间在不同的场合下存在的差异。

(.^.)在Windows系统下启动Rails应用程序的Ruby程序。
require 'Win32API'

def system(cmd)
  sys = Win32API.new "crtdll", "system", ['P'], 'L'
  sys.Call cmd
end

system("d: && cd D:\opt\rails-app && ruby script/server")



________________________________________________________________ 您只要点击下面图标,就可以把本文加入到您喜欢的公共收藏库中去。
del.icio.us Digg | FURL | Yahoo! My Web 2.0 | Reddit | Blinklist | Fark

Posted in  | Tags , , ,

Ruby语言:Ruby程序运行外部程序(二)

Posted by cnruby Fri, 25 Jan 2008 21:39:00 GMT

Ruby语言:Ruby程序运行外部程序(二)


(.^.)说明:这里主要说明面向操作系统的程序代码

(.^.)Windows操作系统
require 'Win32API' 
def system(cmd)
  sys = Win32API.new "crtdll", "system", ['P'], 'L'
  sys.Call cmd
end

system("del ./hello.txt")

(.^.)Linux操作系统程序一

require "Shell"

sh = Shell.new
sh.ls "-al ."
sh.cd "/opt/java"

(.^.)Linux操作系统程序二
require "Shell"

sh = Shell.new
sh.echo("puts 'Hello World!'") > hello.rb
sh.time("ruby hello.rb")




________________________________________________________________ 您只要点击下面图标,就可以把本文加入到您喜欢的公共收藏库中去。
del.icio.us Digg | FURL | Yahoo! My Web 2.0 | Reddit | Blinklist | Fark

Posted in  | Tags , , ,

Ruby语言:Ruby程序运行外部程序(一)

Posted by cnruby Thu, 24 Jan 2008 21:10:00 GMT

Ruby语言:Ruby程序运行外部程序(一)

(.^.)软件环境
(.^.)函数system
  • Linux系统实例代码
    system "rm /tmp/hello.txt"
  • Windows实例代码
    system "cmd /c del ./hello.txt"
    system "calc"
  • 说明
    在执行该函数system以后,程序会继续执行之后的代码。
(.^.)函数exec
  • Linux系统实例代码
    system "ls -al /tmp/hello.txt"
  • Windows实例代码
    system "ls -al ./hello.txt"
  • 说明
    在执行该函数exec以后,程序就停止执行之后的代码。
(.^.)函数·
  • Windows和Linux系统实例代码
    ·ls -al .·
  • 说明
    函数·(重音符,在Tab键上面的按钮)能够让我们获得输出结果。
(.^.)函数%x
  • Windows和Linux系统实例代码
    %x(ls -al .)
  • 说明
    注意这里的括号不能省略。



________________________________________________________________ 您只要点击下面图标,就可以把本文加入到您喜欢的公共收藏库中去。
del.icio.us Digg | FURL | Yahoo! My Web 2.0 | Reddit | Blinklist | Fark

Posted in  | Tags , , ,