Ruby/File Directory/IO.popen

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

IO.popen("-")

IO.popen("-") do |mypipe|
  if mypipe
    puts "I"m the parent: pid = #{Process.pid}"
    listen = mypipe.gets
    puts listen
  else
    puts "I"m the child: pid = #{Process.pid}"
  end
end



Open a file and write string into it

check = IO.popen("spell","r+")
check.puts("A")
check.puts("D")
check.close_write
list = check.readlines
list.collect! { |x| x.chomp }



open a program with a read/write I/O stream and handle data in both directions

handle = IO.popen("other_program", "r+")
handle.puts "send input to other program"
handle.close_write
while line = handle.gets
  puts line
end



you open up an I/O stream with dir

# read the lines one by one, as with other forms of I/O streams, and close the stream
ls = IO.popen("dir", "r")
while line = ls.gets
  puts line
end
ls.close