# Attaching a script to IO

**URL:** <https://www.ruby-forum.com/t/attaching-a-script-to-io/110274>\
**Category:** Ruby\
**Created:** [July 24, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/attaching-a-script-to-io/110274 "2007-07-24T20:49:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 24, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/attaching-a-script-to-io/110274/1 "2007-07-24T20:49:41Z")

</div>

Hey all,  
I’m trying to attach a script’s output to IO (a socket. Is IO  
the right term?)  
I currently use a GServer script to accept connections from localhost.

I don’t know if that made sense, but is there any way to change the  
default output to my socket output?

Thanks,  
---------------------------------------------------------------|  
~Ari  
ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.10.0]

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 24, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/attaching-a-script-to-io/110274/2 "2007-07-24T21:13:43Z")

</div>

Oh, and if it makes a difference, information on using sockets with  
Highline (Thanks, james!) would be GREATLY appreciated.

On Jul 24, 2007, at 2:48 PM, Ari B. wrote:

> ~Ari  
> ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.10.0]

-------------------------------------------|  
Nietzsche is my copilot

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 24, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/attaching-a-script-to-io/110274/3 "2007-07-24T21:23:33Z")

</div>

On Wed, Jul 25, 2007 at 03:48:11AM +0900, Ari B. wrote:

> Hey all,  
> I’m trying to attach a script’s output to IO (a socket. Is IO  
> the right term?)

Works for me!

> I currently use a GServer script to accept connections from localhost.

Well, can you just send whatever you need to send via puts?

Example:

class MyThingy \< GServer  
def initialize(port=3000, \*args)  
super(port, \*args)  
end  
def serve(io)  
io.puts “Hello!”  
end  
end

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 24, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/attaching-a-script-to-io/110274/4 "2007-07-24T21:39:49Z")

</div>

On Jul 24, 2007, at 3:21 PM, Michael G. wrote:

> end  
> end

Well, except I’m not trying to send “hello”. I’m trying to keep my  
script nice and clean, so I have a bunch of ugly code in a separate  
file (which I call with require) and a method test(). If I pass io  
as an argument with test:

def serve(io)  
test(io)  
end

and then later in test(io) have

io.puts

will the variable scope allow that?

Or should I find a way to reroute $stdout?

thanks,  
ari  
-------------------------------------------|  
Nietzsche is my copilot

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 24, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/attaching-a-script-to-io/110274/5 "2007-07-24T23:26:33Z")

</div>

On 24.07.2007 21:39, Ari B. wrote:

> > def serve(io)  
> > test(io)  
> > end
> 
> and then later in test(io) have
> 
> io.puts
> 
> will the variable scope allow that?
> 
> Or should I find a way to reroute $stdout?

It seems you want your script to act as a _server_. In that case class  
TCPServer will help you. Please have a look:

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib\_network.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_network.html)

Kind regards

robert
