From: John Maclean Date: 2006-02-26T03:19:11+09:00 Subject: comments of less.rb appreciated I've had a look at /usr/share/doc/ruby-1.8.4/sample/less.rb and made some comments on the processes where I could. It came with my repos installation of Ruby. Can someone point me in the right direction here, as there a a number of things that I'm not familar with or understand.... #!/usr/bin/ruby # there isn't a single def foo ... end method here - why? # also how come this hasn't been turned into a class? # is that because that there is no need for one? # also why the use of GLOBAL VARIABLES? I thought that they are not from the "church of good design" # gobal vars to hard-code where these programs are... assuming a Unix-based system # it was weired because the path was initially set to /usr/local/bin/ # which did not work # i personally think it's better to use a ruby equivalent of `which less` to find out where the command is # rather than hard coding it in this way ZCAT = "/usr/bin/zcat" LESS = "/usr/bin/less" # funny how you can call ruby less.rb with --help # dunno about this class or method FILE = ARGV.pop # ah, i think that this may be exception handling. # if no arguments from command line *OPTION* is zero # otherwise out an empty string at the end of the filename?? OPTION = (if ARGV.length == 0; "" else ARGV.join(" "); end) # we are grepping for stuff... # a file that ends in Z or gz - these are compressed files if FILE =~ /\.(Z|gz)$/ # what is *%s*? # - is it a string that we come across when we are grepping through the files? # see `ri kernel.format` for the *format* method # "Argument is a string to be substituted" # i think it works with sprintf # run zcat on a compressed file _first_ then view it with less exec(format("%s %s | %s %s", ZCAT, FILE, LESS, OPTION)) # otherwise there *is* no compressed file and we treat it differently elsif FILE == nil # exec(format("%s %s", LESS, OPTION)) else print(format("%s %s %s", LESS, OPTION, FILE), "\n") exec(format("%s %s %s", LESS, OPTION, FILE)) end # end the `less`ing. i dont know why the use of the empty brackets exit() -- John Maclean MSc (DIC) 07739 171 531