User Program Communication
Write to File in Ruby
Used to write to a file.
output_file = open(file_name, 'w') #creating the file object #can be done as many times as needed, no newline added automatically output_file.write(content) output_file.write("\n") output_file.close
When opening the file, 'w' specifies write mode.
The base path is the path of the source file.
sc_funfacts = File.open('syntaxcenter_funfacts.txt', 'w') sc_funfacts.write("Did you know that SyntaxCenters framework is Ruby on Rails?") sc_funfacts.write("\n") sc_funfacts.write("Actually, learning Ruby on Rails was one of the motivations behind this project!") sc_funfacts.write("\n") sc_funfacts.close