From: Waldemar Dick Date: 2008-08-13T01:12:54+09:00 Subject: Re: what do you think of this code? Hi, just a short notice: On line 11, you create attribute accessors for @path, i.e. a reader and a writer for @path. That way, as a user of your class, I can set @path, without the checks made in initialize(path). ls = Ls.new(dir) ls.path=nil ls.list_all_array -> will not work as expected So, I would either move the checks to a "path=" method or remove the write accessor for @path. Greetings, Waldemar 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) > 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?