Ruby launching system apps?

I have a UNIX machine and I want a ruby app that can launch UNIX commands
like awk or sed…how would I implement this?

use Shell.new?

Either

sys cmd here
or
system(“sys cmd here”)

The backticks `` capture the output return it as a string.
The system command returns sucess or failure.

ri system
This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

--------------------------------------------------------- Kernel::system
system( aCmd [, args]* ) → true or false

···

On Tuesday, 26 August 2003 at 2:25:04 +0900, Dan wrote:

I have a UNIX machine and I want a ruby app that can launch UNIX commands
like awk or sed…how would I implement this?

use Shell.new?


 Executes aCmd in a subshell, returning true if the command was
 found and ran successfully, false otherwise. An error status is
 available in $?. The arguments are processed in the same way as for
 Kernel::exec on page 419.
    system("echo *")
    system("echo", "*")
 produces:
    config.h main.rb
    *


Jim Freeze

“I used to think that the brain was the most wonderful organ in my
body. Then I realized who was telling me this.”
– Emo Phillips

The simplest way is to use “system” or `` (you can use Shell too, of
course). ôhe best way is to implement in Ruby all you do with awk and sed,
as it has (almost) everything those utilities offer.

Before I found Ruby, my favorite combination was sh/awk/sed. Now I do
everything of that kind in Ruby, however knowledge and long experience with
those utilities help me a lot, especially with regexp stuff.

Gennady.

···

----- Original Message -----
From: “Dan” falseflyboy@yahoo.comNONO
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, August 25, 2003 10:25 AM
Subject: Ruby launching system apps?

I have a UNIX machine and I want a ruby app that can launch UNIX commands
like awk or sed…how would I implement this?

use Shell.new?

Dan wrote:

I have a UNIX machine and I want a ruby app that can launch UNIX commands
like awk or sed…how would I implement this?

use Shell.new?

cat some_file # returns string of stdout
system ‘cat some_file’ # returns success (true/false)
exec ‘cat some_file’ # replaces current process
popen ‘cat some_file’ # returns IO object connected to stdin and stdout

require ‘open3’
Open3.popen3 ‘cat some_file’ # returns [in,out,err]

I’ve found that when you have a program that makes use of UNIX commands
within various classes and methods that system and `` calls can lead to
synchronization problems – and that such problems can be solved by
using the Shell#transact method.

Here’s an example:

class Document

Shell.def_system_command(“lynx”)

attr_reader :path

def initialize(path)
@path = path
end

def html_to_text_stdout

the following line is needed because @path is not in the scope of the

transact block below
path = @path
html_to_text = String.new
shell = Shell.new
shell.transact do
html_to_text = (shell.lynx(“-dump”, path)).to_s
end
html_to_text
end

end

I agree with Gennady that you will frequently be better off using
native Ruby classes and methods for many things, particularly in-memory
string manipulation, even if comparable tools are available through a
Unix command.

Regards,

Mark

···

On Monday, August 25, 2003, at 01:25 PM, Dan wrote:

I have a UNIX machine and I want a ruby app that can launch UNIX
commands
like awk or sed…how would I implement this?

use Shell.new?

The Shell module lets you invoke UNIX commands, but bear in mind
that doing so is inefficient, not portable to non-UNIX machines,
and likely to cause thread synchronization issues. Ruby itself can
do everything awk and sed can do without having to resort to
running external programs.

-Mark

···

On Monday, August 25, 2003, at 01:25 PM, Dan wrote:

I have a UNIX machine and I want a ruby app that can launch UNIX
commands like awk or sed…how would I implement this?

I agree with Gennady that you will frequently be better off using
native Ruby classes and methods for many things, particularly
in-memory string manipulation, even if comparable tools are available
through a Unix command.

Regards,

Mark

on ruby 1.6.8

time ruby -p -e ‘gsub(“sed”,“ruby”)’ file_with_1000000_sed_lines
7.61s user 2.02s system 35% cpu 27.161 total

time sed s/sed/ruby/g file_with_1000000_sed_lines
2.05s user 1.71s system 32% cpu 11.571 total

…I expected other results…but (I hope) it’s improved on 1.8 (
I could not test it now on 1.8 )

-r.

I have a UNIX machine and I want a ruby app that can launch UNIX
commands like awk or sed…how would I implement this?

The Shell module lets you invoke UNIX commands, but bear in mind
that doing so is inefficient,

Avoid premature optimization. If the inefficiency is a problem,
implement in Ruby. If it is still a problem, extend Ruby with C.

not portable to non-UNIX machines,

True. The universe of Unix machines is pretty large (including Mac OS
X). I don’t know about Windows with cygwin.

and likely to cause thread synchronization issues.

The Shell#transact method will handle synchronization.

Ruby itself can
do everything awk and sed can do without having to resort to
running external programs.
[snip]

True. ‘awk’ and ‘sed’ are the least likely candidates, in my opinion,
for wrapping in the Shell class. However, if you have a shell script
with functionality you want to use in a Ruby program that you don’t
want to spend the time reimplementing in Ruby, the Shell class may be
your best bet, at least initially.

Regards,

Mark

···

On Monday, August 25, 2003, at 05:05 PM, Mark J. Reed wrote:

On Monday, August 25, 2003, at 01:25 PM, Dan wrote:

I agree with Gennady that you will frequently be better off using
native Ruby classes and methods for many things, particularly
in-memory string manipulation, even if comparable tools are available
through a Unix command.

Regards,

Mark

on ruby 1.6.8

time ruby -p -e ‘gsub(“sed”,“ruby”)’ file_with_1000000_sed_lines
7.61s user 2.02s system 35% cpu 27.161 total

time sed s/sed/ruby/g file_with_1000000_sed_lines
2.05s user 1.71s system 32% cpu 11.571 total

…I expected other results…but (I hope) it’s improved on 1.8 (
I could not test it now on 1.8 )

-r.

I don’t think that it is fair to compare ruby with sed because Ruby’s
regular expressions are better than extended REs (ERE) while sed seems
to support at most basic regular expressions (BRE). If you only need a
subset it’s no wonder that sed is faster and a good choice for
optimization. Compared to Perl’s RE Ruby is not too bad and I suppose
that Perl’s RE are very highly optimized for speed.

flori@lambda big$ echo sed: ; time sed s/sed/ruby/g sed.txt > /dev/null ; echo ruby: ; time ruby -p -e ‘gsub(/sed/,%q/ruby/)’ sed.txt > /dev/null ; echo perl: ; time perl -p -e ‘s/sed/ruby/g’ sed.txt > /dev/null
sed:

real 0m1.793s
user 0m1.670s
sys 0m0.016s
ruby:

real 0m13.141s
user 0m11.953s
sys 0m0.131s
perl:

real 0m9.773s
user 0m9.109s
sys 0m0.104s

flori@lambda flori$ ruby -e ‘puts “%.2f” % ((11.953 / 9.109 - 1) * 100)’
31.22

Perl is ca. 30% faster than Ruby, that’s not too much. Given the fact
that in Ruby regular expressions are first class citizens and full
flexed objects after beeing created, while you have to use work arounds
like qr/…/ if you want to pass them around in Perl.

···

On 2003-08-26 23:03:55 +0900, Bermejo, Rodrigo wrote:

on ruby 1.6.8

time ruby -p -e ‘gsub(“sed”,“ruby”)’ file_with_1000000_sed_lines
7.61s user 2.02s system 35% cpu 27.161 total

time sed s/sed/ruby/g file_with_1000000_sed_lines
2.05s user 1.71s system 32% cpu 11.571 total

…I expected other results…but (I hope) it’s improved on 1.8 (
I could not test it now on 1.8 )


It is by the goodness of God that in our country we have those three
unspeakably precious things: freedom of speech, freedom of conscience, and
the prudence never to practice either.
– Mark Twain, More Tramps Abroad

Wrong comparison, given the subject of the thread. Invoking sed
from within ruby is much slower than pure ruby, by my
experiments.

···

In article 3F4A7612.3050103@ps.ge.com, Bermejo, Rodrigo wrote:

time ruby -p -e ‘gsub(“sed”,“ruby”)’ file_with_1000000_sed_lines
7.61s user 2.02s system 35% cpu 27.161 total

time sed s/sed/ruby/g file_with_1000000_sed_lines
2.05s user 1.71s system 32% cpu 11.571 total

Avoid premature optimization. If the inefficiency is a problem,

I hear this a thousand times, but this makes it not more truth.
You should only avoid optimization if it results in a more complex
structure. This is only in a few cases the reality. Often an optimal
datastructure doesn't take longer to debug or to write.

Hi –

···

On Tue, 26 Aug 2003, Lothar Scholz wrote:

Avoid premature optimization. If the inefficiency is a problem,

I hear this a thousand times, but this makes it not more truth.
You should only avoid optimization if it results in a more complex
structure. This is only in a few cases the reality. Often an optimal
datastructure doesn’t take longer to debug or to write.

Then it isn’t premature :slight_smile:

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Lothar Scholz wrote:

Avoid premature optimization. If the inefficiency is a problem,

I hear this a thousand times, but this makes it not more truth.
You should only avoid optimization if it results in a more complex
structure. This is only in a few cases the reality. Often an optimal
datastructure doesn’t take longer to debug or to write.

The whole quote was:
"Avoid premature optimization. If the inefficiency is a problem,
implement in Ruby. If it is still a problem, extend Ruby with C. "

I think Mark was trying to say that using the unix sed command might be
faster then a Ruby equivalent. a) try reworking your algorithm, still
too slow?, b) write it into a C extension (might even be able to use
some of the sed source code here).

The benefit of this approach is that the end result is much more
Ruby-like and platform independent. Everyone can benefit from your work.

Michael