易截截图软件、单文件、免安装、纯绿色、仅160KB

六种用ruby调用执行shell命令的方法

六种用ruby调用执行shell命令的方法
碰到需要调用操作系统shell命令的时候,Ruby为我们提供了六种完成任务的方法:
1.Exec方法:
    Kernel#exec方法通过调用指定的命令取代当前进程:
  例子:
      $ irb
      >> exec 'echo "hello $HOSTNAME"'
         hello nate.local
      $
值得注意的是,exec方法用echo命令来取代了irb进程从而退出了irb。主要的缺点是,你无法从你的ruby脚本里知道这个命令是成功还是失败。
 
2.System方法。
  Kernel#system方法操作命令同上, 但是它是运行一个子shell来避免覆盖当前进程。如果命令执行成功则返回true,否则返回false。
 
 $ irb            
  >> system 'echo "hello $HOSTNAME"'
  hello nate.local
  => true
  >> system 'false'
  => false
  >> puts $?
  256
  => nil
  >>
3.反引号(Backticks,Esc键下面那个键)
$ irb
  >> today = `date`
  => "Mon Mar 12 18:15:35 PDT 2007n"
  >> $?
  => #<Process::Status: pid=25827,exited(0)>
  >> $?.to_i
  => 0
这种方法是最普遍的用法了。它也是运行在一个子shell中。
4.IO#popen
  $ irb
  >> IO.popen("date") { |f| puts f.gets }
  Mon Mar 12 18:58:56 PDT 2007
  => nil
5.open3#popen3
$ irb
  >> stdin, stdout, stderr = Open3.popen3('dc')
  => [#<IO:0x6e5474>, #<IO:0x6e5438>, #<IO:0x6e53d4>]
  >> stdin.puts(5)
  => nil
  >> stdin.puts(10)
  => nil
  >> stdin.puts("+")
  => nil
  >> stdin.puts("p")
  => nil
  >> stdout.gets
  => "15n"
6.Open4#popen4
$ irb
  >> require "open4"
&n


相关文档:

自动化测试之路(三) ruby里的get与set方法

照例可以先看端程序
class Person
 
  def initialize( name,age=18 )
    @name = name
    @age = age
    @motherland = "China"
  end
 
  def talk
    puts "my name is "+@name+", age is "+@age.to_s
 &n ......

升级到snow leopard后ruby的问题

1. ruby已成为1.87 2. 必须先安装安装光盘里的新的xcode,在"optional"目录里 3. 可能需要重新安装macport http://trac.macports.org/wiki/Migration 4. 或者升级macport http://weblog.rubyonrails.org/2009/8/30/upgrading-to-snow-leopard $ sudo port selfupdate
$ sudo port sync
$ sudo port upgrade --force insta ......

转载——Ruby字符串处理

转自:http://developer.51cto.com/art/200912/170762.htm 
Ruby字符串处理函数总结列表分享
Ruby字符串处理函数包括返回字符串长度函数;判断字符串中是否包含另一个串函数;字符串插入;字符串分隔,默认分隔符为空格等等。
 
str.length => integer 
str.include? other_str
&nbs ......

Install Cassandra with Ruby 1.9.1 on Windows

To get it done is not easy. I spent a whole day to figure out the various compatibility issues along the way out.
Now there still might be potential issues, but it works by my rough test.
Step 1: Install Apache Cassandra
  You may know that the Ruby gem cassandra will do it for you.
  ......

Ruby 程序员的挚友

历史背景[1]
诞生于1993年2月24日的Ruby(红宝石),由日本人松本行弘(Yuki Matsumoto)于1994年12月发布。 Ruby 作为一种纯面向对象的脚本程序设计语言,吸收了Smalltalk和Perl两种程序语言的特性。
Ruby理念[2]
1.首先考虑的是,减少编程时不必要的琐碎时间,令编写程序的人高兴;
2.其次是良好的界面设计;
3.强 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号