#Creating a class that will count words in any object file
class File_handling
attr_reader :fname
def initialize(fname)
#@fname = fname
if File.exist? fname
@fname = fname
else
fname=(fname)#why this doesnt work to call the function below
end
end
def fname=(finame)
if File.exist? finame
@fname = finame
else
p "Enter a good file name >>> "
filename = gets.chomp
@fname=(filename)
end
end
def printfile()
File.foreach(@fname) do |line|
puts line.chomp
end
end
end
f1 = File_handling.new('text.txt')
f1.printfile()
def printfiles(fname)
File.foreach(fname) do |line|
puts line
end
end
p printfiles('test.txt')
I'm new to Ruby and trying to understand a few things. I'm not really finished with the class but I would like to know why calling the fname= function above does not work I never get the else message no matter what file name I enter