# Comments of less.rb appreciated

**URL:** <https://www.ruby-forum.com/t/comments-of-less-rb-appreciated/54180>\
**Category:** Ruby\
**Created:** [February 25, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/comments-of-less-rb-appreciated/54180 "2006-02-25T19:21:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 25, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/comments-of-less-rb-appreciated/54180/1 "2006-02-25T19:21:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 26, 2006, 3:33am UTC](https://www.ruby-forum.com/t/comments-of-less-rb-appreciated/54180/2 "2006-02-26T03:33:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 26, 2006, 2:35am UTC](https://www.ruby-forum.com/t/comments-of-less-rb-appreciated/54180/3 "2006-02-26T02:35:24Z")

</div>

On Feb 25, 2006, at 10:19 AM, John M. 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 H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
