[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

using a filter inside Ruby

Eric Schwartz

9/9/2003 7:32:00 PM

I've the contents of a raw log file in memory, and a program that will
parse this raw log data and print out human-readable info. The
problem is, I can't figure out how to use this program as a filter in
Ruby.

I've tried:

IO.popen('/usr/games/jive', 'w+') { |io|
io.puts "What is going on?"
puts io.gets
}

But it just hangs at the gets. This is nagging at the back of my
mind, and if I had my copy of _The Unix Programming Environment_, I
bet I'd find it in there, but it's on loan at the moment.

I could, if no other solution presents itself, write the raw log to a
file, run the filter on the file, and then read that data back into
ruby, but this strikes me as inefficient and error-prone. Can it be
done the way I'm trying to?

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
5 Answers

Eric Schwartz

9/9/2003 8:11:00 PM

0

Eric Schwartz <emschwar@pobox.com> writes:
> I''ve tried:
>
> IO.popen(''/usr/games/jive'', ''w+'') { |io|
> io.puts "What is going on?"
> puts io.gets
> }
>
> But it just hangs at the gets.

Okay, after a fair amount of searching, including some blind guessing,
I came across some old ruby-talk posts mentioning Open3, which seems
to do what I want:

Open3.popen3(''jive'') { |wtr,rdr,err|
wtr.puts "what is up?"
wtr.close
puts rdr.gets
}

This points up my major frustration with Ruby-- I had to completely
guess as to how Open3 worked, or *even that it existed at all*. There
are no docs I can find for it, and this makes developing with Ruby
more of a pain than it needs to be. If I were doing this in perl, I''d
just have to ''perldoc -q pipe'', and instantly I have docs on
IPC::Open2 and a reference to IPC::Open3 if I need that.

Pardon the gripiness, please, but I really hate guessing about my
programming language. POLS is nice, but having easily accessible docs
to confirm all the corner cases is even better, IMO.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.

Jason Creighton

9/10/2003 6:26:00 PM

0

On Tue, 09 Sep 2003 14:11:19 -0600
Eric Schwartz <emschwar@pobox.com> wrote:

> Eric Schwartz <emschwar@pobox.com> writes:
> > I''ve tried:
> >
> > IO.popen(''/usr/games/jive'', ''w+'') { |io|
> > io.puts "What is going on?"
> > puts io.gets
> > }
> >
> > But it just hangs at the gets.
>
> Okay, after a fair amount of searching, including some blind guessing,
> I came across some old ruby-talk posts mentioning Open3, which seems
> to do what I want:
>
> Open3.popen3(''jive'') { |wtr,rdr,err|
> wtr.puts "what is up?"
> wtr.close
> puts rdr.gets
> }
>
> This points up my major frustration with Ruby-- I had to completely
> guess as to how Open3 worked, or *even that it existed at all*. There
> are no docs I can find for it, and this makes developing with Ruby
> more of a pain than it needs to be. If I were doing this in perl, I''d
> just have to ''perldoc -q pipe'', and instantly I have docs on
> IPC::Open2 and a reference to IPC::Open3 if I need that.

Yes, you''re right, the documentation should be better. Of course, if you
look at open3.rb, you''ll see this:

# Usage:
# require "open3"
#
# in, out, err = Open3.popen3(''nroff -man'')
# or
# include Open3
# in, out, err = popen3(''nroff -man'')

HOWEVER, this doesn''t even work because ''in'' is a keyword in Ruby!
That, IMHO, should be fixed. (Both this specific case and documentation
for libraries in general. However, I obviously don''t care *that* much,
otherwise I''d be writing documentation right now.)

Jason Creighton

Gavin Sinclair

9/11/2003 6:54:00 AM

0

>> This points up my major frustration with Ruby-- I had to completely
>> guess as to how Open3 worked, or *even that it existed at all*. There
>> are no docs I can find for it, and this makes developing with Ruby
>> more of a pain than it needs to be. If I were doing this in perl, I''d
>> just have to ''perldoc -q pipe'', and instantly I have docs on
>> IPC::Open2 and a reference to IPC::Open3 if I need that.
>
> Yes, you''re right, the documentation should be better. Of course, if you
> look at open3.rb, you''ll see this:
>
> # Usage:
> # require "open3"
> #
> # in, out, err = Open3.popen3(''nroff -man'')
> # or
> # include Open3
> # in, out, err = popen3(''nroff -man'')
>
> HOWEVER, this doesn''t even work because ''in'' is a keyword in Ruby! That,
> IMHO, should be fixed. (Both this specific case and documentation for
> libraries in general. However, I obviously don''t care *that* much,
> otherwise I''d be writing documentation right now.)
>
> Jason Creighton






Gavin Sinclair

9/11/2003 7:13:00 AM

0

>>> This points up my major frustration with Ruby-- I had to completely
>>> guess as to how Open3 worked, or *even that it existed at all*.
>>> There are no docs I can find for it, and this makes developing with
>>> Ruby more of a pain than it needs to be. If I were doing this in
>>> perl, I''d just have to ''perldoc -q pipe'', and instantly I have docs
>>> on
>>> IPC::Open2 and a reference to IPC::Open3 if I need that.
>>
>> Yes, you''re right, the documentation should be better. Of course, if
>> you look at open3.rb, you''ll see this:
>>
>> # Usage:
>> # require "open3"
>> #
>> # in, out, err = Open3.popen3(''nroff -man'')
>> # or
>> # include Open3
>> # in, out, err = popen3(''nroff -man'')
>>
>> HOWEVER, this doesn''t even work because ''in'' is a keyword in Ruby!
>> That, IMHO, should be fixed. (Both this specific case and
>> documentation for libraries in general. However, I obviously don''t
>> care *that* much, otherwise I''d be writing documentation right now.)
>>
>> Jason Creighton

Damn %#$^#$ web email client! %^#@$%@#$

Sorry for the redundant post. What I was going to say is:

Here''s how you can ensure this is fixed:
* subscribe to ruby-core@ruby-lang.org
* create a patch to fix that documentation issue (diff -u format, I think)
* send it to ruby-core, as a "PATCH: ..." subject

It seems heavyweight, but it makes it easier for the maintainer to go
"yep, that looks good, I''ll commit that". And besides, once you''ve
subscribed, it''s easier to contribute more patches!

Of course, for some files, it may be better to send patches directly to
the author, but if the file is in the Ruby distro, ruby-core is a good
catch-all.

Gavin



Eric Schwartz

9/11/2003 5:39:00 PM

0

Jason Creighton <androflux@softhome.net.remove.to.reply> writes:
> Yes, you''re right, the documentation should be better. Of course, if you
> look at open3.rb, you''ll see this:

It''s not so much bad documentation as completely nonexistent
documentation. AFAIK, there''s no documentation that even lets me know
there is such a thing as Open3. While it''s good to be able to look at
the source in case the documentation''s ambiguous or misleading, it
shouldn''t be a requirement in learning how to use something in the
first place.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.