if a file.txt is in the same directory, can i use load to load it into my function that should read the contents to screen? how to do it with 'load file.rb' method? in ruby

To load the contents of a file in Ruby using the load method, you need to ensure that the file is in the same directory as your script.

Here is a simple example of how you can accomplish this:

main.rb
# Assuming both the script and file.txt are in the same directory

# Define a function to read and display the file contents
def read_file(file_name)
  File.open(file_name, "r") do |file|
    puts file.read
  end
end

# Call the function to read and display the file contents
read_file("file.txt")
298 chars
12 lines

You can run the script by executing the following command:

ruby script.rb
15 chars
2 lines

Replace "script.rb" with the name of your Ruby script file.

This will load the contents of "file.txt" and display them on the screen.

related categories

gistlibby LogSnag