The issue is the ruby version available when rails installation starts is still 2.2.1. Even if you upgrade the ruby version, just check the ruby version
$ ruby -v
It should still show older one.
You should be using ruby version manager(rvm) to manage different versions of ruby. Using this you can change the default version of ruby so that latest version of rails can be installed properly. In case you want to install some other rails version say 4.2 which is compatible with version of ruby available as default in your system,
$ gem install rails -v=4.2
This should install the rails successfully.
Now for better management and as a good practise, Install rvm,
$ \curl -sSL https://get.rvm.io | bash -s stable
Now install the version of ruby as required using
$ rvm install 2.3.1
Next it make this version as default
$ rvm use 2.3.1 --default
just in case you need this version only for a specific project, just use
$ rvm use 2.3.1
$ ruby -v --2.3.1
Fire the rails gem installation now to install.
$ gem install rails
This should help