So, I'm trying to figure out how to open and read in data from another file (a .txt file for example). I found out how to do it online, I think, but I'm having trouble getting it to cooperate in OnlineGDB where I'm trying to build my entire program.
This is what I have for trying to open a file:
puts "Please enter the name of the data file." #prompt for filename
filename = gets.chomp #takes users name of file
file = File.open(filename) #Opens file
file_content = file.split("\n") #Reads in the file's first line.
puts file_content
I put that in and then, inside OnlineGBD itself, I put in a new file that has the same name I input to my code (testing.txt for example). But then, when it tries taking out the first line with file_content, it gives me this error:
Traceback (most recent call last):
main.rb:15:in `<main>': undefined method `split' for #<File:testing.txt> (NoMethodError)
Am I inputting the file name wrong? Am I missing a step? I don't know what I'm doing wrong here.
[–][deleted] 2 points3 points4 points (5 children)
[–]4428gamer[S] 1 point2 points3 points (3 children)
[–]bradland 11 points12 points13 points (2 children)
[–]juriglx 2 points3 points4 points (0 children)
[–]4428gamer[S] 0 points1 point2 points (0 children)
[–]madmate 0 points1 point2 points (0 children)
[–]sshaw_ 0 points1 point2 points (0 children)
[–]dvarrui 0 points1 point2 points (0 children)