Forum: Ruby Setting $? manually - possible?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2006-05-03 19:08
(Received via mailing list)
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?

Thanks,

Dan
E51d56251ec4affafe85ee9367228965?d=identicon&s=25 Park Heesob (Guest)
on 2006-05-03 19:10
(Received via mailing list)
Hi,

>I'm working on a custom Process.waitpid method (for Windows).  It works
>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)>


Regards,
Park Heesob
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2006-08-03 13:33
(Received via mailing list)
Park Heesob wrote:
>> Hi all,
>>
> 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
This topic is locked and can not be replied to.