comments of less.rb appreciated

J

John Maclean

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()
 
E

Eric Hodel

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?
YAGNI

also how come this hasn't been turned into a class?
YAGNI

is that because that there is no need for one?
Yes.

also why the use of GLOBAL VARIABLES? I thought that they are not
from the "church of good design"

There are no global variables in less.rb. Global variables start
with a $.
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

This is in sample/, so they may or may not work out of the box.
less.rb isn't installed anywhere on your system, so YMMV.

Also, less.rb won't work when the PATH env var is empty if you use
which (think cron).
ZCAT = "/usr/bin/zcat"
LESS = "/usr/bin/less"

funny how you can call ruby less.rb with --help

Why? It follows logically from the implementation.
# dunno about this class or method
FILE = ARGV.pop

ARGV is the command line argument Array.
# 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)

If there are options after removing the filename join them with
spaces. Otherwise use an empty string. No exception handling,
exceptions involve begin/rescue/ensure/raise.
# we are grepping for stuff...

matching, not grepping.
# 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?

The string printf format variable.
# see `ri kernel.format` for the *format* method
# "Argument is a string to be substituted"
# i think it works with sprintf
Correct.

# 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))

This is why less.rb --help works.
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

Differences in style.
 
J

John Maclean

Thanks for that reply. YANGNI's made my day. I'll keep things as
simple as poss from now on...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top