Arkady Rodestve
Arkady Rodestve

Reputation: 147

How to work with string?

I have a string with emails (name, surname, email):

@emails = "Nina Beeu [email protected], Vasilina Korute [email protected], Hikote Ewefs [email protected], 
Egert Erm [email protected], Sambuka Ioas [email protected], Vanish Kiki [email protected], Inoke Xxx [email protected]"

I need to substring from this string: name, surname and email and paste them into table:

<table border=1>
  <tr>
    <td>
        Name
    </td>
    <td>
        Surname
    </td>
    <td>
        Email
    </td>
  </tr>
</table>

How i can do it?

Upvotes: 0

Views: 192

Answers (2)

gunn
gunn

Reputation: 9165

@emails.split(/,\s+/).each do |details|
  name, surname, email = details.split(" ")
  # do your html creaty thing here
end

More explicitly, you could do this in erb:

<table border=1>
  <% @emails.split(/,\s+/).each do |details| %>
    <% name, surname, email = details.split(/\s+/) %>
    <tr>
      <td><%= name %></td>
      <td><%= surname %></td>
      <td><%= email %></td>
    </tr>
  <% end %>
</table>

And a variant in haml:

%table(border=1)
  - @emails.split(/,\s+/).each do |details|
    %tr
      - details.split(/\s+/) do |detail|
        %td= detail

Upvotes: 1

fl00r
fl00r

Reputation: 83680

<table>
  <% @emails.split(", ").each do |chunk| %>
    <tr>
      <% ["Name", "Surname", "Email"].zip(chunk.split(" ")).each do |data| %>
        <td><%= data.join(": ")</td>
      <% end %>
    </tr>
  <% end %>
</table>

Upvotes: 1

Related Questions