tags:

views:

41

answers:

2

Is there a synchronized exec in ruby? I try the following code and when I open the file I get nothing, and it's probably because exec doesn't finish writing the file.

exec "sort data.txt > data.sort"
File.foreach("data.sort") { |line| puts line}

Ted

+3  A: 

exec replaces the current process with the one you are executing; nothing after the exec gets run at all! You probably want system instead.

Arkku
+3  A: 

You were looking for system, not exec. However, it's a lot easier than that if you use backticks, which return the output of the command.

puts `sort data.txt`

If you need to iterate, then you can iterate over the return value directly:

sorted = `sort data.txt`
sorted.each do |line|
  puts line
end

or even:

`sort data.txt`.each do |line|
  puts line
end
Mark Thomas