tags:

views:

39

answers:

2
require 'fileutils.rb'

k=FileUtils.mkdir_p "d:/pptomasdsdr1xks_6b27"

filename = "#{k}""/kddabab"  
extension = ".txt"  
 co=1

  $stdout=File.open("#{filename}#{co}#{extension}" ,'w') 

puts "sachin" 

$stdout.close

puts "amit" 

im getting error like

stdout.rb:14:in write': closed stream (IOError) from stdout.rb:14:inputs'

i don't want amit to be printd in my file so that i close the file but i got this error help me plzzz

+2  A: 

Don't use $stdout to write to a file.

amphetamachine
+1  A: 

Don't change $stdout, and certainly not without storing away the old value somewhere so you can restore it after you're done with it.

Instead, call puts on the file object:

File.open("#{filename}#{co}#{extension}" ,'w') do |file|
  file.puts "sachin" # This goes to the file
end

puts "amit" # This goes to standard output
Lars Haugseth
but lars can i call any function using file object
Amit singh tomar