Home > OS >  How can I display country code and last 3 digits of phone number in Ruby on Rails views?
How can I display country code and last 3 digits of phone number in Ruby on Rails views?

Time:09-17

I am a beginner in RoR.
This is the original phone number 77123456999

I am going to display it as 77xxxxxx999

Here is my index.html.erb file code.

<tbody>
    <% @users.each do |user| %>
        <tr>
            <td>
                <%= user.username %>
            </td>
            <td>
                <%= user.phone %>
            </td>
            <td>
                <%= user.email %>
            </td>
            <td>
                <%= user.state %>
            </td>
        </tr>
    <% end %>
</tbody>

The value of user.phone is 77123456999 How can I change is as I want?

CodePudding user response:

I would use gsub with a regexp:

phone_number = " 77123456999"
phone_number.gsub(/(?<=\w{2})\w(?=\w{3})/, 'x')
#=> " 77xxxxxx999"
  • Related