Ruby Pipe and C Extensions
I have some ruby code (1.9) like
@开发者_JAVA技巧rd,@wd = IO.pipe
def callback()
puts @wd.class
# do stuff
end
pid = fork do
@rd.close
register_callback(:callback)
end
@wd.close
# do some stuff in parent process
register_callback is a C extension that makes a blocking system call, and upon certain conditions will call the ruby function associated with the symbol passed in.
However, @wd is of type NilClass according to the message I get when I run this program and it tries to access @wd in the callback function, which doesn't make any sense to me. Any help is appreciated.
after you call the register_callback method. The rest of the code continues to execute (as you do the register_callback method call inside fork). So @wd.close runs, before your callback is made. Hence when the callback() method is finally called. @wd is nil (which is the result of @wd.close).
精彩评论