You could use modules:

module SortedOutput
  def read
    @lines = File.readlines(@file).sort
  end
end

Then you can do:

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

  def read
    @lines = File.readlines(@file)
  end

  def output(io=$stdout)
    read unless @lines
    @lines.each_with_index do |line, i|
      io.puts "#{i+1}. #{line}"
    end
  end
end
    

outputter = MyFileOutputter.new("myfile.txt")
outputter.extend SortedOutput
outputter.output

I find mixing in Modules that replace methods on object instances pretty
handy.

-rich


On 6/16/04 11:10 AM, "Florian Weber" <csshsh / structbench.com> wrote:

> hi!
> 
> i have a design question. for example i have a class which reads a file
> and prints out each line with line numbers. what if now i wanna be able
> to plugin some functionality which sorts the file, depending on some
> conditions (extension, etc) before its passed on to the line numbering
> functionality. the class should be as less tied as possible to the
> sorting
> functionality as possible..
> 
> how would you normally design that in ruby?
> 
> would you just overwrite the method which calls the file read method
> and passes it to the numbering method?
> 
> would you pack an object in between, which would check if the file
> has to be sorted, because the conditions are true, read the file and
> the return the sorted or unsorted content?
> 
> basically im wondering if its considered a good practice to overwrite
> methods on the same class or if its kinda hackish.. =)
> 
> thanks a lot!
> 
> ciao!
> florian
> 
> 
> 
>