Skip to content

Instantly share code, notes, and snippets.

@avdi
Created June 3, 2009 04:34
Show Gist options
  • Save avdi/122788 to your computer and use it in GitHub Desktop.
Save avdi/122788 to your computer and use it in GitHub Desktop.
require 'rbconfig'
def hello(source, expect_input)
puts "Hello from #{source}"
if expect_input
puts "Standard input contains: \"#{$stdin.read}\""
else
puts "No stdin, or stdin is same as parent's"
end
$stderr.puts "Hello, standard error"
end
THIS_FILE = File.expand_path(__FILE__)
RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
if $PROGRAM_NAME == __FILE__
puts "1. Backtick operator"
output = `#{RUBY} -r#{THIS_FILE} -e'hello("backticks", false)'`
puts "output: #{output}"
puts
puts "2. Kernel.system"
success = system(%Q<#{RUBY} -r#{THIS_FILE} -e 'hello("backticks", false)'>)
puts "success: #{success}"
puts
puts "3. Kernel.fork"
pid = fork do
hello("fork()", false)
end
puts "pid: #{pid}"
puts
puts "4. Kernel.open with |"
cmd = %Q<|#{RUBY} -r#{THIS_FILE} -e 'hello("open(|)", true)'>
open(cmd, 'w+') do |subprocess|
subprocess.write("hello from parent")
subprocess.close_write
puts "output: #{subprocess.read}"
puts
end
puts "5. Kernel.open with |-"
open("|-", "w+") do |subprocess|
if subprocess.nil? # child
hello("open(|-)", true)
exit
else # parent
subprocess.write("hello from parent")
subprocess.close_write
puts "output: #{subprocess.read}"
puts
end
end
puts "6. IO.popen"
cmd = %Q<#{RUBY} -r#{THIS_FILE} -e 'hello("popen", true)'>
IO.popen(cmd, 'w+') do |subprocess|
subprocess.write("hello from parent")
subprocess.close_write
puts "output: #{subprocess.read}"
puts
end
puts "7. IO.popen with -"
IO.popen("-", "w+") do |subprocess|
if subprocess.nil? # child
hello("popen(-)", true)
exit
else # parent
subprocess.write("hello from parent")
subprocess.close_write
puts "output: #{subprocess.read}"
puts
end
end
puts "8. Open3.popen3"
require 'open3'
cmd = %Q<#{RUBY} -r#{THIS_FILE} -e 'hello("popen3", true)'>
Open3.popen3(cmd) do |stdin, stdout, stderr|
stdin.write("hello from parent")
stdin.close
puts "output: #{stdout.read}"
puts "error output: #{stderr.read}"
puts
end
puts "9. Shell"
require 'shell'
Shell.def_system_command 'ruby', RUBY
shell = Shell.new
args = ['-r', THIS_FILE, '-e', 'hello("shell", true)']
shell.transact do
output = ""
output = echo("hello from parent") | ruby(*args)
puts "output: #{output}"
puts
end
puts "10. Open4"
require 'rubygems'
require 'open4'
cmd = %Q<#{RUBY} -r#{THIS_FILE} -e 'hello("Open4", true)'>
stdin = StringIO.new("Hello from parent")
stdout = StringIO.new
stderr = StringIO.new
Open4.spawn(cmd,
'stdin' => stdin,
'stdout' => stdout,
'stderr' => stderr)
puts "output: #{stdout.string}"
puts "error output: #{stderr.string}"
puts
puts "11. Rake FileUtils#ruby"
require 'rubygems'
require 'rake'
include FileUtils
args = ['-r', THIS_FILE, '-e', 'hello("rake", false)']
ruby(*args)
puts
puts "12. Daemons"
require 'rubygems'
require 'daemons'
daemon = Daemons.call do
hello('daemons', false)
end
daemon.stop
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment