Article on oreilly.net on how to build Unix tools with Ruby

Thought you’d like to know about this article
This links to the printable version, much easier to read.
http://linux.oreillynet.com/lpt/a/4159

Enjoy.

A pretty good article, though:

  1. It would be nice if it mentioned optparse, which is a little
    cleaner than getoptlong for what the author is doing.
  2. Using “while line = gets do” is just as clean as “while gets”, but
    doesn’t require the use of $_. I suspect the author came from a
    perl background.
  3. I wish he’d mentioned the option of using #!/usr/bin/env ruby
    instead of #!/usr/local/bin/ruby.

Paul

···

On Sat, Sep 20, 2003 at 06:00:21AM +0900, Xavier wrote:

Thought you’d like to know about this article
This links to the printable version, much easier to read.
Radar – O’Reilly

Enjoy.

Nice script for handling CSVs. But it doesn’t handle commas embedded in
double quotes (yet).

···

Xavier NOSPAM@keepyourspam.com wrote:

Thought you’d like to know about this article
This links to the printable version, much easier to read.
Radar – O’Reilly


Geoffrey S. Knauth | Geoffrey S. Knauth

geoff@knauth.org (Geoffrey S. Knauth) writes:

Thought you’d like to know about this article
This links to the printable version, much easier to read.
Radar – O’Reilly

Nice script for handling CSVs. But it doesn’t handle commas embedded in
double quotes (yet).

If anyone wants one, I’ve got a DelimParser class that handles different
delimiters and quote marks as well as delimiters, escaped characters, and
doubled quotes that appear in the data.

It doesn’t use regexps. It’s a simple state machine that walks through each
character in the input.

Ask and thou shall receive.

Jim

···

Xavier NOSPAM@keepyourspam.com wrote:

Jim Menard, jimm@io.com, http://www.io.com/~jimm/
“$665.95: The Retail Price of the Beast” – Unknown

Can someone please explain to me what /usr/bin/env/ruby does?

Thanks,
Gavin

···

On Wednesday, September 24, 2003, 2:33:01 AM, Paul wrote:

  1. I wish he’d mentioned the option of using #!/usr/bin/env ruby
    instead of #!/usr/local/bin/ruby.

Paul Brannan wrote:

  1. I wish he’d mentioned the option of using #!/usr/bin/env ruby
    instead of #!/usr/local/bin/ruby.

Paul

advantages ?

-r.

Speaking of Unix tools, do any of the Perlians in this list remember Tom
Christiansen’s pet project Perl Power Tools?

http://www.perl.com/language/ppt/

The project aims to rewrite many Unix commands in Perl, from simple
commands like ‘echo’, ‘clear’, and ‘cat’ to moderately complex ones like
‘cp’, ‘find’, and ‘ls’ to complex ones like ‘awk’, ‘make’, and ‘m4’.
Take a look at the URL above, they seem to have implemented a good chunk
of them.

Perhaps someone with extra free time on his hands would like to take a
stab on a Ruby Power Tools project? :wink:

···

On Sat, Sep 20, 2003 at 06:00:21AM +0900, Xavier wrote:

Thought you’d like to know about this article
This links to the printable version, much easier to read.
Radar – O’Reilly

Enjoy.


dave

would you please send me a copy or publish it on RAA ?

···

il 23 Sep 2003 15:06:01 -0500, Jim Menard jimm@io.com ha scritto::

It doesn’t use regexps. It’s a simple state machine that walks through each
character in the input.

Ask and thou shall receive.

I have found that the csv parser by NaHi to be very good and
full featured.

http://raa.ruby-lang.org/list.rhtml?name=csv

···

On Wednesday, 24 September 2003 at 5:15:41 +0900, Jim Menard wrote:

geoff@knauth.org (Geoffrey S. Knauth) writes:

Xavier NOSPAM@keepyourspam.com wrote:

Thought you’d like to know about this article
This links to the printable version, much easier to read.
Radar – O’Reilly

Nice script for handling CSVs. But it doesn’t handle commas embedded in
double quotes (yet).


Jim Freeze

God made the world in six days, and was arrested on the seventh.

···

On Wednesday, September 24, 2003, 2:33:01 AM, Paul wrote:

  1. I wish he’d mentioned the option of using #!/usr/bin/env ruby
    instead of #!/usr/local/bin/ruby.

Can someone please explain to me what /usr/bin/env/ruby does?

Note that there’s no slash between “env” and “ruby”… env is a program that
lets you run a program in a modified environment, and it’s also useful on the
shebang line if you don’t know the exact location of the interpreter you want
to run. It will find the executable in the path before executing it.

– Mike


Michael W. Thelen
Experience is what you get when you don’t get what you want.
– Dan Stanford

I thought someone had already mentioned the advantages.
Basically, you don’t need to know the path of ruby.
So, if you are distributing an application, your app
will run no matter where ruby is installed.

There are also options that you can pass to env to
control environment variables.

The main disadvantage is that using env increases
the security risk over a hard coded path.

···

On Wednesday, 24 September 2003 at 12:30:24 +0900, Bermejo, Rodrigo wrote:

Paul Brannan wrote:

  1. I wish he’d mentioned the option of using #!/usr/bin/env ruby
    instead of #!/usr/local/bin/ruby.

advantages ?


Jim Freeze

It is now 10 p.m. Do you know where Henry Kissinger is?
– Elizabeth Carpenter

rpt.rubyforge.org is still available…

Yours,

Tom

···

On Wed, 2003-09-24 at 02:23, David Garamond wrote:

Speaking of Unix tools, do any of the Perlians in this list remember Tom
Christiansen’s pet project Perl Power Tools?

http://www.perl.com/language/ppt/

The project aims to rewrite many Unix commands in Perl, from simple
commands like ‘echo’, ‘clear’, and ‘cat’ to moderately complex ones like
‘cp’, ‘find’, and ‘ls’ to complex ones like ‘awk’, ‘make’, and ‘m4’.
Take a look at the URL above, they seem to have implemented a good chunk
of them.

Perhaps someone with extra free time on his hands would like to take a
stab on a Ruby Power Tools project? :wink:


Tom Copeland tom@infoether.com
InfoEther

David Garamond wrote:

Thought you’d like to know about this article
This links to the printable version, much easier to read.
Radar – O’Reilly

Enjoy.

Speaking of Unix tools, do any of the Perlians in this list remember Tom
Christiansen’s pet project Perl Power Tools?

http://www.perl.com/language/ppt/

The project aims to rewrite many Unix commands in Perl, from simple
commands like ‘echo’, ‘clear’, and ‘cat’ to moderately complex ones like
‘cp’, ‘find’, and ‘ls’ to complex ones like ‘awk’, ‘make’, and ‘m4’.
Take a look at the URL above, they seem to have implemented a good chunk
of them.

Perhaps someone with extra free time on his hands would like to take a
stab on a Ruby Power Tools project? :wink:

Actually, I had thought of this quite some time ago. I picked the
project name “Runix” and played with a cute logo that (sort of)
spelled Runix in Norse runes.

The aim at the time was to implement a free set of Unix-like tools
for Windows (without the heavyweight solution of Cygwin).

David Alan Black and I also discussed this as a possible codefest
activity, but dumped it in favor of scanf.

Finally I decided: It’s an interesting and fun activity, but it’s not
a high priority for me. I myself am rapidly moving away from Windows,
and it’s not usually wise to invest in the past.

Hal

···

On Sat, Sep 20, 2003 at 06:00:21AM +0900, Xavier wrote:

gabriele renzi surrender_it@remove.yahoo.it writes:

···

il 23 Sep 2003 15:06:01 -0500, Jim Menard jimm@io.com ha scritto::

It doesn’t use regexps. It’s a simple state machine that walks through each
character in the input.

Ask and thou shall receive.

would you please send me a copy or publish it on RAA ?

Gabriele,

I emailed it to you and Michael and posted it to
http://www.io.com/~jimm/downloads/index.html.

Jim

Jim Menard, jimm@io.com, http://www.io.com/~jimm/
“Unix is extremely user-friendly. It also happens to be extremely
selective when picking its friends.” – Daniel E. Macks

Speaking of env, does this work on linux when options are passed
to ruby?

I’m not at a linux terminal right now, but when the shebang is:

/usr/bin/env ruby -w

I get the error that ruby -w is not found.
The gnu manual says that it should work, but it certainly
doesn’t work as it does on FreeBSD or Sun.

The distro is RH 7.3.

···

On Wednesday, 24 September 2003 at 8:24:36 +0900, Michael W Thelen wrote:

On Wednesday, September 24, 2003, 2:33:01 AM, Paul wrote:

  1. I wish he’d mentioned the option of using #!/usr/bin/env ruby
    instead of #!/usr/local/bin/ruby.

Can someone please explain to me what /usr/bin/env/ruby does?


Jim Freeze

Adore, v.:
To venerate expectantly.
– Ambrose Bierce, “The Devil’s Dictionary”

Hi,

From: “Jim Freeze” jim@freeze.org
Sent: Wednesday, September 24, 2003 7:05 AM

Thought you’d like to know about this article
This links to the printable version, much easier to read.
Radar – O’Reilly

Nice script for handling CSVs. But it doesn’t handle commas embedded in
double quotes (yet).

I have found that the csv parser by NaHi to be very good and
full featured.

http://raa.ruby-lang.org/list.rhtml?name=csv

And is bundled in ruby from 1.8…
It can parse <<EOS.chomp
a,b,“,”,“”“”,“\r\n”,“\r”,“\n”
EOS

Of cource it must have easy interface!
Any suggestion are welcome. How do you want to write
parsing code?

Regards,
// NaHi

PS. Current interface;

SYNOPSIS

  1. reader = CSV.open( filename, “r” )

  2. CSV.open( filename, “r” ) do | row |

    end

  3. writer = CSV.open( filename, “w” )

  4. CSV.open( filename, “w” ) do | writer |

    end

ARGS

filename: filename to open.
mode: “r” for read (parse)
“w” for write (generate)
row: an Array of cells which is a parsed line.
writer: Created writer instance. See CSV::Writer#<< and
CSV::Writer#addRow to know how to generate CSV string.

RETURNS

reader: Create reader instance. To get parse result, see
CSV::Reader#each.
writer: Created writer instance. See CSV::Writer#<< and
CSV::Writer#addRow to know how to generate CSV string.

DESCRIPTION

Open a CSV formatted file to read or write.

EXAMPLE 1

reader = CSV.open( “csvfile.csv”, “r” )
row1 = reader.shift
row2 = reader.shift
if row2.empty?
p “row2 not find.”
end
reader.close

EXAMPLE 2

CSV.open( “csvfile.csv”, “r” ) do | row |
p row
end

EXAMPLE 3

writer = CSV.open( “csvfile.csv”, “w” )
writer << [ “r1c1”, “r1c2” ] << [ “r2c1”, “r2c2” ] << [ nil, nil ]
writer.close

EXAMPLE 4

CSV.open( “csvfile.csv”, “w” ) do | writer |
writer << [ “r1c1”, “r1c2” ]
writer << [ “r2c1”, “r2c2” ]
writer << [ nil, nil ]
end

···

Xavier NOSPAM@keepyourspam.com wrote:

Hal Fulton wrote: (re: runix)
.

Finally I decided: It’s an interesting and fun activity, but it’s not
a high priority for me. I myself am rapidly moving away from Windows,
and it’s not usually wise to invest in the past.

Then think of it as an investment in the future. Just as you leave one
OS for another today, you may do the same in the future, even if going
from one flavor of *nix to another. I’ve run into assorted iratation
because different 'nix do not implement all utilities quite the same
way. Whether it be grep with slighly different switches, or some
version of ifconfig, or whatever, it’s nice to have a known set of tools
that Do The Right Thing wherever they are.

At worst, if you move to a new platform you may need to tweak the Runix
stuff to accomodate quirks of the OS, but you only need do that once,
then resume using the format most familiar.

James

···

Hal

Perhaps someone with extra free time on his hands would like to take a
stab on a Ruby Power Tools project? :wink:

Actually, I had thought of this quite some time ago. I picked the
project name “Runix” and played with a cute logo that (sort of)
spelled Runix in Norse runes.

The aim at the time was to implement a free set of Unix-like tools
for Windows (without the heavyweight solution of Cygwin).

There are other (lightweight) solutions anyway: mingw and unixtools.
I see no point in a Ruby project for this; it doesn’t solve any
problems that I’ve seen come up on the list.

David Alan Black and I also discussed this as a possible codefest
activity, but dumped it in favor of scanf.

Finally I decided: It’s an interesting and fun activity, but it’s not
a high priority for me. I myself am rapidly moving away from Windows,
and it’s not usually wise to invest in the past.

Gavin

···

On Thursday, September 25, 2003, 3:40:40 AM, Hal wrote:

No, it doesn’t. I’ve seen a detailed explanation somewhere on a perl
mailing list or newsgroup, but I can’t remember the specifics.

Paul

···

On Wed, Sep 24, 2003 at 09:04:55AM +0900, Jim Freeze wrote:

Speaking of env, does this work on linux when options are passed
to ruby?

Hi,

From: “NAKAMURA, Hiroshi” nakahiro@sarion.co.jp
Sent: Wednesday, September 24, 2003 11:54 AM

Of cource it must have easy interface!
Any suggestion are welcome. How do you want to write
parsing code?

I didn’t mean “it must be easy already without any modification”.
I mean “it should have easy interface if the current interface
is not easy”.

Where’s my teacher of English composition?

Regards,
// NaHi