Last Updated: February 25, 2016
·
512
· chrismo

misc collection of ruby at_exit fu

puts "#{Process.pid}: Parent"
2.times { at_exit { puts "#{Process.pid}: parent at_exit (2 times)" } }
2.times {  END { puts "#{Process.pid}: parent END (only one)" } }
trap(0) { puts "#{Process.pid}: parent trap(0)" }
trap('CLD') { puts "#{Process.pid}: parent: child died" }
fork {
  puts "#{Process.pid}: Child A"
  at_exit { puts "#{Process.pid}: at_exit in child if only exit called" }
  END { puts "#{Process.pid}: END in child if only exit called" }
  trap(0) { "#{Process.pid}: trap(0) in child - NEVER HAPPENS" }
  exit
}
fork {
  puts "#{Process.pid}: Child B"
  at_exit { puts "#{Process.pid}: at_exit - NEVER HAPPENS" }
  END { puts "#{Process.pid}: END in child - NEVER HAPPENS" }
  trap(0) { "#{Process.pid}: trap(0) in child - NEVER HAPPENS" }
  exit!
}
Process.wait

https://gist.github.com/4157576