You can use the File.exists? method to check if the file "file.txt" exists in the same directory. If it exists, you can use File.readlines or File.open to read its contents and print them to the screen.
Here's an example:
main.rb147 chars9 lines
In this example, we first check if the file "file.txt" exists using the File.exists? method. If it exists, we read its contents using File.readlines and store them in the variable file_contents
. Finally, we print the file contents to the screen using puts.
Please note that File.readlines will read the entire file into memory at once. If you have a very large file, you may want to consider using File.open and reading line by line to avoid memory issues.
Also, make sure that the "file.txt" file is in the same directory as your Ruby script or specify the full path to the file if it is located elsewhere.
gistlibby LogSnag