ruby: Fix mutually dependent links corner-case

Ref: #28467

For some reason ```HOMEBREW_PREFIX/lib/ruby/gems``` was creating a symlink to ```prefix/lib/ruby``` on a fresh install of ruby. This patch checks for that case and if necessary removes it and creates a new folder. It applies to all three folders, not just ```gems```.

The patch also preserves the existing behavior of leaving each folder intact if it exists provided it does not violate this edge case.