Abder-Rahman Ali wrote:
> class ReadFile
> def set_file(filename)
> def initialize
> end
> end
> end

No. You can't nest defs (or at least, they won't do what you expect).

I don't grok Java, so I'm not sure exactly what you want. It looks like 
setFile is creating a new ReadFile object, so is setFile actually a 
class method, not an instance method?

  class ReadFile

    # This is a class method

    def self.set_file(filename)
      new(filename)              # short for: self.new(filename)
    end

    # These are instance methods

    def initialize(filename)
      @filename = filename
    end

    def read
      File.read(@filename)
    end
  end

If this is true, I'd say "set_file" is a poor name for what is basically 
a factory method. In most cases such factory methods aren't used; the 
user of this class would just create an instance directly. e.g.

  rf = ReadFile.new("/etc/motd")
  puts rf.read

But if I've misunderstood, and you want set_file to be an instance 
method, then it could look like this:

  class ReadFile

    def initialize(filename)
      set_file(filename)
    end

    def set_file(filename)
      @filename = filename
    end

    def read
      File.read(@filename)
    end
  end

Or:

  class ReadFile

    def initialize(filename)
      self.file = filename
    end

    def file=(filename)
      @filename = filename
    end

    def read
      File.read(@filename)
    end
  end

Or you could use attr_accessor. This writes accessor methods for you, in 
this case 'filename' and 'filename=' to read and write @filename 
respectively.

  class ReadFile

    attr_accessor :file

  end

  # Example
  rf = ReadFile.new
  rf.file = "/etc/motd"
-- 
Posted via http://www.ruby-forum.com/.