hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Ruby for Windows in Under 1K #

by why in bits

If you enjoyed Rubyless Ruby, here’s a remix for Windows. I’m using the Windows Scripting Host for this one, which means an appearance from good ol’ XmlHttpRequest. It’s the first new wave of 2006: Ajax for Batch Files, peoepl!

Here’s the code, I’ll show ya how to run it after the jump.

 tryurl = "http://tryruby.hobix.com/irb?cmd=" 

 // strap on xhr, nice, nice
 function wget( url, hdrs ) {
   var http = WScript.CreateObject( "Microsoft.XMLHTTP" );
   http.open( "GET", url, false );
   for ( var k in hdrs ) {
     http.setRequestHeader( k, hdrs[k] );
   }
   http.send( "" );
   return http.responseText;
 }

 // the prompt!
 sess = wget( tryurl + escape( "!INIT!IRB!" ), {} )
 WScript.Echo( "Interactive Ruby ready." )
 WScript.StdOut.Write( ">> " )
 WScript.StdIn.Read(0)
 while ( true ) {
   cmd = WScript.StdIn.ReadLine()
   resp = ""; ps = ">>";
   if ( cmd ) {
     resp = wget( tryurl + escape( cmd ).replace( '+', '%2B' ), 
                 {'Cookie': '_session_id=' + sess});
     ps = "..";
     if ( resp ) {
       WScript.StdOut.Write( resp );
       ps = ">>";
     }
   }
   WScript.StdOut.Write( ps + " " );
 }

You can download the script try.js by doing a right-click Save as... in IE. To run the thing from the prompt:

 cscript /Nologo try.js

While the Windows Scripting Host has probably been the number one source of viruses for the platform (ILOVEYOU comes to mind), it’s actually a very natural way to shell script. Many years ago there was a brilliant desktop shell called Graphite which had WSH hooks. It’s too bad that lots of stuff is buried in obscure objects.

For the above script, I found the ss64 reference to be the most practical reference. And JS/XHR docs are wide and far, of course.

said on 17 Dec 2005 at 03:48

Jeepers! I’ll try this the moment I’m back in front of a Windows box on Monday.

said on 17 Dec 2005 at 09:01

you never cease to amaze

said on 17 Dec 2005 at 13:18

Cool.

said on 19 Dec 2005 at 13:47

Fantastic.

said on 20 Dec 2005 at 19:01

Haaaah, this is great. I’ll use it to convert my geek friends who up until now didn’t know any better than to use other languages.

Comments are closed for this entry.