论坛首页 编程语言技术论坛

Ruby怎样传递参数给子进程(最简单的进程之间通信方法)

浏览 3302 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-09-27   最后修改:2011-09-27
1.子进程main.rb
ENV["ttt"]="yanzilee9292"
system 'ruby t.rb'
 
2.子进程t.rb
puts ENV["ttt"]
 
3.运行
ruby main.rb

这样控制台会打印出yanzilee9292




Environment variables are variables passed to programs by the command line or the graphical shell. Though there are a number of environment variables that only affect the command line or graphical shell itself (such as PATH or HOME), there are also several that directly affect how Ruby scripts execute.

Accessing Environment Variables from Ruby
Ruby has direct access to environment variables via the ENV hash. Environment variables can be directly read or written to by using the index operator with a string argument. Note that writing to environment variables will only have an effect on child processes of the Ruby script. Other invocations of the script will not see the changes in environment variables.
   发表时间:2011-09-27  
最简单的进程通讯是用管道嘛:
hooopo@hooopo:~$ cat a.rb
#!/usr/bin/env ruby
puts "Hello World"
hooopo@hooopo:~$ cat b.rb 
#!/usr/bin/env ruby
puts gets * 10
hooopo@hooopo:~$ ./a.rb|./b.rb
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
0 请登录后投票
   发表时间:2011-09-27  
这种方法也可以的  考虑到不同操作系统,Ruby的环境变量封装了一层,使用的时候更加简单
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics