Included as part of ActiveSupport, Rails has a handy helper for formatting phone numbers. Passing a number/string to the helper and some optional args and it will take care of the rest.
Here’s a few examples of how it works:
number_to_phone(8858846) # 885-8846 number_to_phone(8778858846) # 877-885-8846 number_to_phone(8778858846, area_code: true) # (877) 885-8846
To see all the options for the method, check out the Rails docs https://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html#method-i-number_to_phone