How do I create a table to display all the instances of my Ruby class using ERB?

StackOverflow https://stackoverflow.com/questions/22481109

  •  16-06-2023
  •  | 
  •  

سؤال

My table has the following columns:

<tr>
  <th>Number</th>
  <th>User</th>
  <th>Password</th>
  <th>UID</th>
  <th>GID</th>
  <th>GCOS</th>
  <th>Home Directory</th>
  <th>Login Shell</th>
</tr>

I have class Student, which looks like this:

class Student
  attr_accessor :user_name, :password, :uid, :gid,
                :gcos_field, :directory, :shell
  attr_reader :count

  def initialize(data)
    @user_name,@password,@uid,@gid,@gcos_field,@directory,@shell = data
    @@count = defined?(@@count) ? @@count += 1 : 1

  end
end

There are about thirty instances of the class under the array map:

@students = datalines.map { |line| Student.new(line.chomp.split(":")) }

A sample element from datalines array is this:

dputnam:x:4185:208:Douglas Vernon Putnam:/users/dputnam:/bin/bash

I haven't been able to get the @@count to function properly either.

My failed attempt at producing a table made all the student names (of my Ruby class incidentally) display horizontally rather than vertically:

<% @students.each do |parameters| %>
  <td><%= parameters.user_name %></td>
<% end %>

Any help would be appreciated! Here's a link to the output if it helps: http://hills.ccsf.edu/~wly3/cs132a/lab4.cgi

Okay the ERB template (I think this is what it is) since it was requested:

<h1>CS132A Lab 4</h1>
<br>
<br>
<table>

<tr>
  <th>Number</th>
  <th>User</th>
  <th>Password</th>
  <th>UID</th>
  <th>GID</th>
  <th>GCOS</th>
  <th>Home Directory</th>
  <th>Login Shell</th>
</tr>

<tr>
<% @students.each do |parameters| %>
  <td><%= parameters.user_name %></td>
<% end %>
</tr>

</table>


<% finish = Time.now %>
<h2>Elapsed time in seconds:<%= (finish.to_f - start.to_f).to_s %></h2>
هل كانت مفيدة؟

المحلول

You have to generate a new <tr> for each student:

<% @students.each do |parameters| %>
  <tr>
    <td><%= parameters.user_name %></td>
  </tr>
<% end %>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top