Jerome
Jerome

Reputation: 6189

Outputting a ruby Matrix in Rails view

If the following Matrix is being generated

@matrix = Matrix[[243, 256.0, 17762.980000000025], [363, 394.05, 27477.839999999997], [127, 495.5, 9712.96], [38, 249.0, 3497.8000000000006], [26, 110.0, 1365.4600000000005], [258, 650.0, 17966.500000000007]]

When they are rendered to view

<% @matrix.each do |item| %>
  <tr>
    <td><%= item[0] %></td>
    <td><%= item[1] %></td>
    <td><%= item[2] %></td>
  </tr>
<% end %>

type errors are appearing, such as

NoMethodError - undefined method `[]' for 256.0:Float:
NoMethodError - undefined method `[]' for 243:Fixnum

What is the proper way to invoke these values?

Upvotes: 0

Views: 36

Answers (1)

Huy Vo
Huy Vo

Reputation: 2500

You can use row_vectors to returns an array of row vectors:

<% @matrix.row_vectors.each do |row| %>
  <tr>
    <td><%= row[0] %></td>
    <td><%= row[1] %></td>
    <td><%= row[2] %></td>
  </tr>
<% end %>

Upvotes: 1

Related Questions