Park Heesob wrote:
> 
> 
> Hi,
> 
>> From: Daniel Berger <djberg96 / gmail.com>
>> Reply-To: ruby-talk / ruby-lang.org
>> To: ruby-talk / ruby-lang.org (ruby-talk ML)
>> Subject: Setting $? manually - possible?
>> Date: Sat, 29 Apr 2006 23:41:19 +0900
>>
>> Hi all,
>>
>> Ruby 1.8.4
>>
>> I'm working on a custom Process.waitpid method (for Windows).  It 
>> works fine, except that I want to be able to set the Process::Status 
>> ($?) manually within the method.  Specifically, the exitstatus and 
>> pid.  This is pure Ruby, btw.
>>
>> The problem is that Process::Status.new has been undef'd within 
>> process.c and $? has been set to readonly.
>>
>> I've tinkered with redefining Process::Status.new, a custom 
>> Process::Status#initialize, instance_variable_set, etc - all to no avail.
>>
>> Is there any way to set $? from within Ruby?
>>
> Something like this is possible but not recommended.
> 
> require 'Win32API'
> 
> FIXNUM_FLAG = 0x01
> rb_int2inum = Win32API.new('msvcrt-ruby18','rb_int2inum','L','L') # long 
> -> VALUE
> rb_iv_set  = Win32API.new('msvcrt-ruby18', 'rb_iv_set', 'LPL', 'V')
> 
>    system("ver")
> p $?    # -> #<Process::Status: pid=920,exited(0)>
>    obj = rb_int2inum.call($?.object_id) ^ FIXNUM_FLAG
>    rb_iv_set.call(obj,"pid",rb_int2inum.call(1))
>    rb_iv_set.call(obj,"status",rb_int2inum.call(0xFF00))
> p $?    # -> #<Process::Status: pid=1,exited(255)>

Heh, that's awesome.  I may just add that, though I'll toss in some 
error checking and extra documentation. :)

Thanks,

Dan