redirect STDOUT to function?

J

Jonathan Castello

Hello=2C
I'm using Ruby as a scripting language for a program that supports the WSH =
scripting interface=2C and I have a small problem: 'puts' and other output =
functions don't work (because STDOUT is invalid)=2C as with 'gets' (but wit=
h STDIN). I'd like to redirect all accesses to STDOUT and STDIN to my own f=
unctions=2C which would handle the IO through the main application's script=
ing interface.
I know that $stdout and others can be reassigned to other IO objects=2C but=
I haven't yet found a way to reliably redirect through a function instead.=
Of course=2C this is probably because I'm relatively new to Ruby.
Does anyone know how I could do something like this?~Jonathan Castello

_________________________________________________________________
Bing brings you health info from trusted sources.
http://www.bing.com/search?q=3Dpet+allergy&form=3DMHEINA&publ=3DWLHMTAG&cre=
a=3DTXT_MHEINA_Health_Health_PetAllergy_1x1=
 
C

Christopher Dicely

I know that $stdout and others can be reassigned to other IO objects, but I haven't yet found a way to reliably
redirect through a function instead. Of course, this is probably because I'm relatively new to Ruby.
Does anyone know how I could do something like this?~Jonathan Castello

I haven't tried this, but you should be able to create an IO-like
class/object where the methods you care about are (or call) the custom
functions you want to use. It would be nice if there was an module you
could mix into an object that implemented a few of the more basic
function which would build on those to provide the rest of the IO
interface, the way Enumerable builds on an "each" method in the class
its mixed into.
 
J

Jonathan Castello

On the module suggestion=2C that would definitely be pretty great! Also=2C =
I've already considered the IO-like class=2C and I'm just not sure where to=
start. I don't know what method(s) puts/p/gets/et cetera depend on=2C or w=
hat the dependencies depend on=3B it's a fairly difficult problem for a new=
comer to Ruby like me.
Date: Tue=2C 22 Sep 2009 10:44:43 +0900
From: (e-mail address removed)
Subject: Re: redirect STDOUT to function?
To: (e-mail address removed)
=20
=20
=20
I haven't tried this=2C but you should be able to create an IO-like
class/object where the methods you care about are (or call) the custom
functions you want to use. It would be nice if there was an module you
could mix into an object that implemented a few of the more basic
function which would build on those to provide the rest of the IO
interface=2C the way Enumerable builds on an "each" method in the class
its mixed into.
=20
_________________________________________________________________
Ready for Fall shows? Use Bing to find helpful ratings and reviews on digit=
al tv's.
http://www.bing.com/shopping/search?q=3Ddigital+tv's&form=3DMSHNCB&publ=3DW=
LHMTAG&crea=3DTEXT_MSHNCB_Vertical_Shopping_DigitalTVs_1x1=
 
K

Ken Bloom

Hello,
I'm using Ruby as a scripting language for a program that supports the
WSH scripting interface, and I have a small problem: 'puts' and other
output functions don't work (because STDOUT is invalid), as with 'gets'
(but with STDIN). I'd like to redirect all accesses to STDOUT and STDIN
to my own functions, which would handle the IO through the main
application's scripting interface. I know that $stdout and others can be
reassigned to other IO objects, but I haven't yet found a way to
reliably redirect through a function instead. Of course, this is
probably because I'm relatively new to Ruby. Does anyone know how I
could do something like this?~Jonathan Castello

Create a brand new IO object that's backed by the function you want.
(Maybe a thread and a StringIO would do the trick.)

--Ken
 
J

Jonathan Castello

Hmm=2C what do you mean by "backed by the function you want"? My whole prob=
lem is that I don't know what function(s) to redefine in order for the obje=
ct to work reliably. For example=2C do all of the methods rely on just one =
to access the data directly=2C or are there multiple such methods I'd need =
to account for?
Date: Tue=2C 22 Sep 2009 11:08:42 +0900
From: (e-mail address removed)
Subject: Re: redirect STDOUT to function?
To: (e-mail address removed)
=20
On Tue=2C 22 Sep 2009 10:07:16 +0900=2C Jonathan Castello wrote:
=20
=20
Create a brand new IO object that's backed by the function you want.=20
(Maybe a thread and a StringIO would do the trick.)
=20
--Ken
=20
--=20
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
=20
=20

_________________________________________________________________
Bing brings you health info from trusted sources.
http://www.bing.com/search?q=3Dpet+allergy&form=3DMHEINA&publ=3DWLHMTAG&cre=
a=3DTXT_MHEINA_Health_Health_PetAllergy_1x1=
 
W

Walton Hoops

Why not just override the puts/gets function to your liking?

irb(main):001:0> def puts(str)
irb(main):002:1> :foo
irb(main):003:1> end
=> nil
irb(main):004:0> puts "Hello World"
=> :foo


-----Original Message-----
From: Jonathan Castello [mailto:[email protected]]
Sent: Monday, September 21, 2009 8:21 PM
To: ruby-talk ML
Subject: Re: redirect STDOUT to function?


Hmm, what do you mean by "backed by the function you want"? My whole
problem is that I don't know what function(s) to redefine in order for
the object to work reliably. For example, do all of the methods rely on
just one to access the data directly, or are there multiple such
methods I'd need to account for?
Date: Tue, 22 Sep 2009 11:08:42 +0900
From: (e-mail address removed)
Subject: Re: redirect STDOUT to function?
To: (e-mail address removed)



Create a brand new IO object that's backed by the function you want.
(Maybe a thread and a StringIO would do the trick.)

--Ken

_________________________________________________________________
Bing brings you health info from trusted sources.
http://www.bing.com/search?q=pet+allergy&form=MHEINA&publ=WLHMTAG&crea=
TXT_MHEINA_Health_Health_PetAllergy_1x1
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top