backport commits from ruby/ruby trunk.

Hi, I cherry-picked following fixes from ruby/ruby trunk.

 * r46711
 * r46710
 * r46709
 * r46027
 * r44502
 * r46143
 * r46142

@drbrain Should I support Ruby 1.8 series? If it's yes, I'll added more fixes as same as https://github.com/rubygems/rubygems/commit/a34fb569e41cd87866e644d92a9df4be89b3cad2