Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Ruby 'puts' to write data into a file?

Tags:

ruby

I have the following code that I want to use to ping IP addresses and write to a file. It all works fine, except I can't get it to write to the file.

server = %w'192.168.150.254
192.168.150.251
192.168.120.1
192.168.120.2'

File.open('/test/test2.out','w') do |s|
  server.each do |p|
    r = `ping -a -n 1 #{p}`
    puts r
  end
end
like image 708
rahrahruby Avatar asked May 03 '11 17:05

rahrahruby


1 Answers

Change puts r to s.puts r. You're writing to stdout instead of to s. (See Kernel#puts and IO#puts)

like image 145
Josh Lee Avatar answered Nov 15 '22 17:11

Josh Lee