> This is not doing what you think it does - when @path = "" ...

thanks for the input. Here are my changes and my reasoning about it:

- like the unix 'ls' command the default parameter is '.'

- I demand 'path' as a initialize argument but I wont set a default
value because creating a new instance with new() is wrong and should
raise an error.

- I try to catch all the important exceptions

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4         def initialize(path)
  5                 @path = path
  6                 if @path == "" || @path == nil
  7                         @path = "."
  8                 elsif File.exists?(@path)
  9                         raise ArgumentError, "This is a file not a
directory"
 10                 elsif  !File.directory?(@path)
 11                         raise ArgumentError, "Directory doesn't exists"
 12                 end
 13         end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I have to admit that I have a bit a hard time to grock the usage of
the different ruby class and object variables (local vars, global,
instance, class). I hope correct this time (line 5)


On Tue, Aug 12, 2008 at 7:46 PM, Martin DeMello <martindemello / gmail.com> wrote:
> On Tue, Aug 12, 2008 at 4:37 PM, Ben Aurel <ben.aurel / gmail.com> wrote:
>> #!/usr/bin/env ruby
>> # The Unix tool 'ls' implemented in ruby
>> class Ls
>>        @path = ""
>
> This is not doing what you think it does - when @path = "" is
> executed, you are in the context of the class object Ls, which will
> acquire a "class instance variable" @path, which is then never used
> (or, indeed, accessible). If your intent was to have a default path of
> "" if none was given, you need the next line to be
>
>>        def initialize(path)
>
> def initialize(path = "")
>
> but since immediately after you are checking the path
>
>>                if File.exists?(path) && File.directory?(path)
>
> you might as well let it be nil if not supplied, and omit the default
> altogether.
>
> martin
>
>