title: Output an Array of objects to terminal, as a table with attributes in fixed-width columns
body: <p>I am attempting to output a 2D array to the console; I want the information in the array to be nicely formatted as shown in my desired output at the end of this question. My array is created as follows (instances are created of the <code>FamilyMember</code> class):</p>
  
  <pre><code>#family_members.rb
  
  class FamilyMember
  
  attr_accessor :name, :sex, :status, :age
  def initialize (name, sex, type, role, age)
     @name = name
     @sex = sex
     @type = type
     @role = role
     @age = age
  end
  
  end
  
  # Below, an array is created called fm; instances of the class are then instantiated within the array elements
  fm = {}
  fm[1] = FamilyMember.new('Andrew','Male', 'Child', 'Son' , '27' )
  fm[2] = FamilyMember.new('Bill','Male', 'Parent', 'Father' , '63' )
  fm[3] = FamilyMember.new('Samantha','Female', 'Parent', 'Mother' , '62' )
  fm[4] = FamilyMember.new('Thomas','Male', 'Child', 'Dog' , '10' )
  fm[5] = FamilyMember.new('Samantha', 'Female', 'Child', 'Dog' , '4' )
  </code></pre>
  
  <p>I want to be able to output the contents of the array to the console formatted as a string. I need to be able to do this two ways - using <code>each</code> and seperately by using <code>do</code>. </p>
  
  <p>What I have attempted (<a href="https://stackoverflow.com/questions/7478300/how-do-i-print-a-multi-dimensional-array-in-ruby">inspired by a previous SO question</a>):</p>
  
  <pre><code>def eacharray(an_array)
    an_array.each do |inner|
        puts inner.join(" ")
    end
  end
  
  eacharray(fm)
  </code></pre>
  
  <p>However the output from the above is as follows:</p>
  
  <pre><code>1 #&lt;FamilyMember:0x000000027e7d48&gt;
  2 #&lt;FamilyMember:0x000000027e7c58&gt;
  3 #&lt;FamilyMember:0x000000027e7b68&gt;
  4 #&lt;FamilyMember:0x000000027e7a78&gt;
  5 #&lt;FamilyMember:0x000000027e7988&gt;
  </code></pre>
  
  <p>How do I output the 2D array elements nicely formatted using <code>each</code> and <code>do</code>?. Any help appreciated. Thanks. </p>
  
  <p>Ideally, my output would be like this:</p>
  
  <pre><code>Family Member   Name     Sex     Type    Role    Age
  1               Andrew   Male    Child   Son     27
  2               Bill     Male    Parent  Father  63
  3               Samantha Female  Parent  Mother  62 
  4               Thomas   Male    Child   Dog     10
  5               Samantha Female  Child   Dog     4 
  </code></pre>