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

P

Paul Brannan

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
 
J

Jim Menard

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
 
G

gabriele renzi

il 23 Sep 2003 15:06:01 -0500 said:
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 ?
 
G

Gavin Sinclair

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

Thanks,
Gavin
 
M

Michael W Thelen

* Gavin Sinclair said:
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
 
J

Jim Freeze

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

Paul Brannan

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

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
 
N

NAKAMURA, Hiroshi

Hi,
From: "Jim Freeze" <[email protected]>
Sent: Wednesday, September 24, 2003 7:05 AM
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
 
N

NAKAMURA, Hiroshi

Hi,
From: "NAKAMURA, Hiroshi" <[email protected]>
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
 
J

Jim Freeze

advantages ?

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

David Garamond

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? ;-)
 
M

Mike Stok

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.

One way of getting arounf env's ignoring ARGS iff you are just doing -w
is to put

$VERBOSE = true

after the shebang line. See
http://mail.gnu.org/archive/html/bug-sh-utils/2002-04/msg00020.html
for an explanation.

Hope this helps,

Mike
 
J

Jim Freeze

One way of getting arounf env's ignoring ARGS iff you are just doing -w
is to put

$VERBOSE = true

after the shebang line. See

Yes, that is our current work around. It's probably the better
convention because it is reliable for all platforms.
 
T

Tom Copeland

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? ;-)

rpt.rubyforge.org is still available...

Yours,

Tom
 
H

Hal Fulton

David said:
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? ;-)

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top