I would just change `s.gsub!(/\snulls\s+first\Z/i, ' NULLS LAST') || s.gsub!(/\snulls\s+last\Z/i, ' NULLS FIRST')`
to `s.gsub!(/\snulls\s+first\s*\Z/i, ' NULLS LAST') || s.gsub!(/\snulls\s+last\s*\Z/i, ' NULLS FIRST')` so it can handle spaces after the `last` or `first`.