Just for kicks: do you even need a regex here? String#tr should do the trick just fine:
telemachus ~ $ irb >> "500 500 12".tr(' ', '') => "50050012" >> "500 500 12".tr!(' ', '') => "50050012"
As with gsub and gsub! method ! makes a replacement instead of returning a modified result. I do not know what you want here.
In this case, tr seems to me more understandable. I'm not looking for optimization, but it's good to remember that there are many string methods other than regular expressions.
Telemachus
source share