backport r47787 from ruby/ruby trunk