hi
I'm hugely impressed by your helpfull comments. Thanks alot. I've
tried to follow your recommondations and made again a new version. I
thinks its a cool little program, and I think I post it somewhere with
comments. As reminder for me and maybe valuable information for others

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
#!/usr/bin/env ruby
# The Unix tool 'ls' implemented in ruby
class Ls
	@path = ""
	def initialize(path)
		if File.exists?(path) && File.directory?(path)
			@path = path
		else
			raise ArgumentError, "Directory doesn't exists"
		end
	end
		
	def list_all()
		files_n_dirs = Array.new
		Dir.foreach(@path) do |entry|
			next if ['.','..'].include?(entry)
			files_n_dirs << entry
		end
		return files_n_dirs
	end
end

dir = ARGV.shift
if dir == ['--help'] or dir == ['-h']
	puts "Help: ls - list directories and files "
elsif dir
	begin
		puts Ls.new(dir).list_all()		
	rescue ArgumentError => e
		puts e.message
	end
else puts "no Argument - define path"
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





On Tue, Aug 12, 2008 at 1:45 PM, David A. Black <dblack / rubypal.com> wrote:
> Hi --
>
> Here are a few more comments.
>
> On Tue, 12 Aug 2008, Ben Aurel wrote:
>
>> 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)
>
> Conversion to conventional indentation for free :-)
>
>>  5     if File.exists?(path) && File.directory?(path) && path!=nil
>
> File.exists?(nil) will raise a TypeError, so you'll never get to the
> final test. If you make it through the first two tests, the third test
> is not necessary. So you can get rid of it, and decide whether you
> want the nil error to trickle up. The path argument is required,
> though, so it's not going to be nil just because someone forgot it; it
> will only be nil if someone passes in nil.
>
> You might even consider just storing the path, and then letting
> whatever happens later happen -- like someone trying to access a
> non-existent or restricted directory.
>
>>  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
>
> A slightly more straightforward way to handle that might be:
>  next if ['.', '..'].include?(entry)
>  files_n_dirs << entry
>
>> 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
>
> You can just do:
>
>  puts ls.list_all_array(dir)
>
>
> David
>
> --
> Rails training from David A. Black and Ruby Power and Light:
>  *  Advancing With Rails August 18-21 Edison, NJ
>  * Co-taught by D.A. Black and Erik Kastner
> See http://www.rubypal.com for details and updates!
>
>