Read/Open Files


#1

I have to write some code (duh) and I will have a menu that says

puts “Welcome To The Baseball Calculator”
puts “1. Show Major League winners”
puts “2. Show number of wins for a team”
puts "3. Exit "

choice = gets.chomp.to_i

if choice == 1

end

thats all I have so far…

I have a text file “winners” and a text file “teams”

how do I open/read those :confused:


#2

Hello @sdem9,

Would be really helpful if you post your code directly in your topic.


#3

class SportsFiles
def initialize (teams)
@Teams = “teams.txt”
end

def read_file(teams)
file = File.open(“teams.txt”, “r”)
end

puts “Welcome To The Baseball Calculator”
puts “1. Show Major League winners”
puts “2. Show number of wins for a team”
puts "3. Exit "

choice = gets.chomp.to_i
if choice == 1

puts

end
end


#4

Thanks @sdem9!

If you would like to read the file and to show each line:

teams = File.open('teams.txt', 'r')

teams.each do |team|
  p team
end

#5

Thank you so much @Maniac !!


#7

im not sure where im suppose to place in my code…


#8

nvm I got it lol thanks for all the help