[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

comments of less.rb appreciated

John Maclean

2/25/2006 6:19:00 PM

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



2 Answers

Eric Hodel

2/26/2006 1:34:00 AM

0


On Feb 25, 2006, at 10:19 AM, John Maclean wrote:

> 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.

> exit()

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...




John Maclean

2/26/2006 2:30:00 AM

0

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

On Sun, 26 Feb 2006 10:34:03 +0900 Eric
Hodel <drbrain@segment7.net> wrote:

>
> On Feb 25, 2006, at 10:19 AM, John Maclean wrote:
>
> > 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.
>
> > exit()
>


--
John Maclean
MSc (DIC)
07739 171 531