I am writing a program to read files and search text in it. I have written the first initial steps. In the below given code you can see a symbol ** — **. This where I want to pass member variable value of Class [CurrentFile].
Please also suggest what improvements I can do in this code.
class CurrentFile
attr_accessor :currentFileName, :currentFileContent
end
class OpenFile < CurrentFile
def OpenFileToRead() #Open file as read-only.
thisFile = File.open(** ----- **, 'r')
counter = 1
begin
file = File.new(thisFile, "r")
while (line = file.gets)
puts "#{counter}: #{line}"
counter = counter + 1
end
file.close
rescue => err
puts "Exception: #{err}"
err
end #End of Begin block
end #End of OpenFileToRead
end #End of Class: OpenFile
fileToRead = CurrentFile.new #Create instance of CurrentFile Class
fileToRead.currentFileName = "C:\WorkSpace\SearchText\abc.php" #Set file name to read
myFile = OpenFile.new #Create instance of OpenFile Class
You don’t need two classes.
Since OpenFile inherits CurrentFile then you have currentFileName and currentFileContent attributes in OpenFile. This means you can use
currentFileNameinFile.open.Or if you want two classes than pass a currentFile instance as a parameter to OpenFile and don’t inherit: