The installation instructions for CocoaPods are unfortunately out of date for later versions of macOS. Essentially, Apple stopped shipping a complete version of Ruby, so the Gem install fails.
I got around this by installing Ruby etc. via Homebrew
brew install rbenv ruby-build ruby
After that, I had to set up the relevant symbolic links manually
cd /usr/local/bin
ln -s ../Cellar/ruby/3.2.2_1/bin/* .
Finally, I had to add an extra parameter to the Gem install command
sudo gem install -n /usr/local/bin cocoapods