Why "system" command does not read Ruby output file?

Hi,
I have following script. It works fine except that “system” command
does not read output file “tmp_out.txt”, when I run the script. I do not
get any error when I run the script. I expect to see the results of
system “sort tmp_out.txt” on my Terminal window, but do not see it. Do
you guys have any idea what I am doing wrong.

Thank you very much.

#!/Users/yigit/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -w

file_out = File.new(“tmp_out.txt”, “w”) #makes new file for writing
while (DATA.gets) do
line = $_
(chr, start, stop) = line.split("\t")
for i in (Integer(start)…Integer(stop))
file_out.puts i
end
end

system “sort tmp_out.txt”

END
chr 1 4
chr 3 6
chr 5 8

I recommend these articles.

get strings or not

http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-60
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-system

output it

http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-print
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-puts

Kenichi K. wrote in post #1047421:

I recommend these articles.

get strings or not

http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-60
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-system

output it

http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-print
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-puts

Thanks. My colleague just told me that I needed to close the file before
system call. everything is in order now.

Oh…

$VERBOSE = true

open “tmp_out.txt”, “w” do |file|
DATA.each do |line|
_, start, stop = line.split
(Integer(start)…Integer(stop)).each do |n|
file.puts n
end
end
end

system “sort tmp_out.txt”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs