I want to write a simple program, that writes console input to file with name i entered.
file_name = ARGV[0]
of = File.open(file_name, 'w')
while a = gets.chomp
puts a
of.puts a
end
# ruby write_script.rb file_name.txt
returns: main: undefined method chomp for nil:NilClass (NoMethodError)
update:
file_name = ARGV[0]
File.open(file_name, 'w') do |file|
while (a = gets)
print a
file.write a
end
end
After executing this code - program terminates at start and empty file creates.
What is the right way to do it?