Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Input redirection in ruby

Reply
Thread Tools

Input redirection in ruby

 
 
Michal Suchanek
Guest
Posts: n/a
 
      07-31-2008
Hello

I could not find any way to redirect input and output of programs in ruby.

You can capture the fds but not redirect AFAICT.

So unless there is something I have overlooked I add the the ruby wishlist:

let popen and popen3 take IO arguments for use as stdin, stdout, and
stderr. Since they can be named in 1.9 (or was that dropped?) you may
specify anything you want. These should probably work only with IO
that is backed by a real fd, support for StringIO or similar might be
problematic.

Thanks

Michal

 
Reply With Quote
 
 
 
 
Shadowfirebird
Guest
Posts: n/a
 
      07-31-2008
Well, you can say $stderr.puts "message", for example. But I'm
guessing that you are talking about something more comprehensive?

On Thu, Jul 31, 2008 at 3:10 PM, Michal Suchanek <(E-Mail Removed)> wrote:
> Hello
>
> I could not find any way to redirect input and output of programs in ruby.
>
> You can capture the fds but not redirect AFAICT.
>
> So unless there is something I have overlooked I add the the ruby wishlist:
>
> let popen and popen3 take IO arguments for use as stdin, stdout, and
> stderr. Since they can be named in 1.9 (or was that dropped?) you may
> specify anything you want. These should probably work only with IO
> that is backed by a real fd, support for StringIO or similar might be
> problematic.
>
> Thanks
>
> Michal
>
>




--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      07-31-2008
On 31.07.2008 16:10, Michal Suchanek wrote:
> I could not find any way to redirect input and output of programs in ruby.
>
> You can capture the fds but not redirect AFAICT.


You can - and it's pretty easy also. The key feature to know is IO#reopen:

robert@fussel ~
$ rm x

robert@fussel ~
$ ./ro.rb
foo 1
foo 2

robert@fussel ~
$ head x
insgesamt 4319
drwx------+ 22 robert Kein 0 Feb 17 16:27 Anwendungsdaten
drwx------+ 2 robert Kein 0 Jul 27 23:24 Cookies
drwx------+ 12 robert Kein 0 Jul 27 14:18 Desktop
drwx------+ 2 robert Kein 0 Aug 18 2004 Druckumgebung
drwx------+ 17 robert Kein 0 Apr 20 12:14 Eigene Dateien
drwx------+ 5 robert Kein 0 Feb 8 19:58 Favoriten
drwx------+ 6 robert Kein 0 Sep 17 2006 Lokale Einstellungen
-rwx------+ 1 robert Kein 4194304 Jul 27 12:07 NTUSER.DAT
drwx------+ 2 robert Kein 0 Mar 24 18:05 Netzwerkumgebung

robert@fussel ~
$ cat ro.rb
#!/bin/env ruby

puts "foo 1"

fork do
STDOUT.reopen("x", "w")
exec "ls", "-l"
end

puts "foo 2"

robert@fussel ~
$

Alternatively you can use the shell's redirection feature when invoking
system with one argument only:

robert@fussel ~
$ ruby -e 'system "ls -l > xx"'

robert@fussel ~
$ head xx
insgesamt 4323
drwx------+ 22 robert Kein 0 Feb 17 16:27 Anwendungsdaten
drwx------+ 2 robert Kein 0 Jul 27 23:24 Cookies
drwx------+ 12 robert Kein 0 Jul 27 14:18 Desktop
drwx------+ 2 robert Kein 0 Aug 18 2004 Druckumgebung
drwx------+ 17 robert Kein 0 Apr 20 12:14 Eigene Dateien
drwx------+ 5 robert Kein 0 Feb 8 19:58 Favoriten
drwx------+ 6 robert Kein 0 Sep 17 2006 Lokale Einstellungen
-rwx------+ 1 robert Kein 4194304 Jul 27 12:07 NTUSER.DAT
drwx------+ 2 robert Kein 0 Mar 24 18:05 Netzwerkumgebung

robert@fussel ~
$

Cheers

robert
 
Reply With Quote
 
Michal Suchanek
Guest
Posts: n/a
 
      07-31-2008
On 31/07/2008, Robert Klemme <(E-Mail Removed)> wrote:
> On 31.07.2008 16:10, Michal Suchanek wrote:
>
> > I could not find any way to redirect input and output of programs in ruby.
> >
> > You can capture the fds but not redirect AFAICT.
> >

>
> You can - and it's pretty easy also. The key feature to know is IO#reopen:


Yes, this makes stuff much easier. Still to do what shell does when
you use a shell pipe you need a working fork. I am not sure how fork
is emulated on platforms that use spawn like Windows.

According to porting tutorials passing the fd's is possible with the
windows API but doing a full fork() is hard - it normally requires
copying the whole address space manually because part of the NT core
functionality is not exposed in the Win32 API.

Thanks

Michal

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-01-2008
2008/8/1 Michal Suchanek <(E-Mail Removed)>:
> On 31/07/2008, Robert Klemme <(E-Mail Removed)> wrote:
>> On 31.07.2008 16:10, Michal Suchanek wrote:
>>
>> > I could not find any way to redirect input and output of programs in ruby.
>> >
>> > You can capture the fds but not redirect AFAICT.
>> >

>>
>> You can - and it's pretty easy also. The key feature to know is IO#reopen:

>
> Yes, this makes stuff much easier. Still to do what shell does when
> you use a shell pipe you need a working fork. I am not sure how fork
> is emulated on platforms that use spawn like Windows.


Well, I was actually on Windows (although I have to admit that I use
the cygwin version all the time).

Can't remember: do IO.popen and friends work in the Windows version?
If it does, then you got your redirection with pipes already.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Input redirection with IDLE candide Python 0 10-13-2009 02:16 PM
fork/exec with input redirection Dan Upton Python 3 11-27-2007 08:45 PM
[ANN] ruby-co.de: now with more flexible redirection Jan 'jast' Krueger Ruby 0 08-30-2005 09:16 PM
Ruby redirection woes Steven Kah Hien Wong Ruby 10 11-04-2004 02:08 AM
input redirection MadHatter Perl 6 10-16-2003 04:05 PM



Advertisments