开发者

Is there a way to synchronize this?

Is there a synchronized exec in ruby? I try the following code and when I open the file I get nothing, and it's p开发者_开发知识库robably because exec doesn't finish writing the file.

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

Ted


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜