this is my solution to your inputs

1.) better errorhandling

2.) no more string methods in the class.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1 #!/usr/bin/env ruby
  2 # The Unix tool 'ls' implemented in ruby
  3 class Ls
  4         def initialize(path)
  5                 if File.exists?(path) && File.directory?(path) && path!=nil
  6                         @path = path
  7                 else
  8                         raise ArgumentError, "Directory doesn't exists"
  9                 end
 10         end
 11         attr_accessor :path
 12
 13         def list_all_array(dir)
 14                 files_n_dirs = Array.new
 15                 Dir.foreach(path) do |entry|
 16                         if entry != "." &&  entry != ".."
 17                                 files_n_dirs << entry
 18                         end
 19                 end
 20                 return files_n_dirs
 21         end
 22 end
 23
 24 dir = ARGV.shift
 25 if dir == ['--help'] or dir == ['-h']
 26         puts "Help: ls - list directories and files "
 27 elsif dir
 28         begin
 29                 ls = Ls.new(dir)
 30                 ls.list_all_array(dir).each do |i|
 31                         puts i
 32                 end
 33
 34         rescue ArgumentError => e
 35                 puts e.message
 36         end
 37 else puts "no Argument - define path"
 38 end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Like it better now?




On Tue, Aug 12, 2008 at 9:46 AM, Frederick Cheung
<frederick.cheung / gmail.com> wrote:
>
> On 12 Aug 2008, at 14:35, James Gray wrote:
>
>> On Aug 12, 2008, at 8:29 AM, Ben Aurel wrote:
>>
>>> thanks a lot for qour hints. I made a few changes based on your tips.
>>>
>>> 1.) the constructor:
>>> ~~~~~~~~~~~
>>> 6         def initialize(path)
>>> 7                 if File.exists?(path) && File.directory?(path) &&
>>> path!=nil
>>> 8                         @path = path
>>> 9                 else
>>> 10                         puts "Directory doesn't exists"
>>> 11                         exit 1
>>> 12                 end
>>> 13         end
>>> 14         attr_accessor :path
>>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> Does this look OK to you?
>>
>> I like this a lot less.  Let's say I want to build a GUI version of the
>> command.  I can't use your class now, because it prints text messages and
>> exits when I should probably be showing an error dialog.  That makes your
>> class less generally useful, I think.
>
> Yup. Instead of that puts and call to exit I'd raise an appropriate
> exception (eg ArgumentError)
>
> Your command line harness for this class can always rescue this and print
> the message.
> Fred
>
>