tags:

views:

117

answers:

2

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
A: 

I think you just want to write currentFileName if you're trying to access that instance's currentFileName attribute.

Chuck
@Chuck: It is giving error ------- [SearchText.rb:13:in `OpenFileToRead': undefined method `currentFileName' for CurrentFile:Class (NoMethodError) from SearchText.rb:33>Exit code: 1]
RPK
There is no reason why it would do that. And, in fact, if you simply copy and paste the code you posted in your question into a file and run it, it works just the way you intended it to. (Obviously, you have to apply Chuck's suggestion first.)
Jörg W Mittag
@Chuck, @Jorg: It is giving error. But how to post the code snippet again in this post?
RPK
@Rohit: You can use the "edit" button at the bottom of your question to update it.
Chuck
+2  A: 

You don't need two classes.

Since OpenFile inherits CurrentFile then you have currentFileName and currentFileContent attributes in OpenFile. This means you can use currentFileName in File.open.

fileToRead = OpenFile.new #Create instance of CurrentFile Class
fileToRead.currentFileName = "C:\WorkSpace\SearchText\abc.php" #Set file name to read
fileToRead.OpenFileToRead

Or if you want two classes than pass a currentFile instance as a parameter to OpenFile and don't inherit:

class OpenFile
    def initialize(file)
      @file = file
    end

    def OpenFileToRead()  #Open file as read-only.
      thisFile = File.open(@file.currentFileName, '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
  end

fileToRead = CurrentFile.new #Create instance of CurrentFile Class
fileToRead.currentFileName = "C:\WorkSpace\SearchText\abc.php" #Set file name to read
myFile = OpenFile.new(fileToRead) #Create instance of OpenFile Class
myFile.OpenFileToRead
Mihai A