Ruby: méthode pour imprimer et soigné, un tableau

Je ne sais pas si cette question est trop bête, mais je n'ai pas trouvé un moyen de le faire.

Généralement pour mettre en évidence un tableau dans une boucle je fais ce

current_humans = [.....]
current_humans.each do |characteristic|
  puts characteristic
end

Cependant, si j'ai ceci:

class Human
  attr_accessor:name,:country,:sex
  @@current_humans = []

  def self.current_humans
    @@current_humans
  end

  def self.print    
    #@@current_humans.each do |characteristic|
    #  puts characteristic
    #end
    return @@current_humans.to_s    
  end

  def initialize(name='',country='',sex='')
    @name    = name
    @country = country
    @sex     = sex

    @@current_humans << self #everytime it is save or initialize it save all the data into an array
    puts "A new human has been instantiated"
  end       
end

jhon = Human.new('Jhon','American','M')
mary = Human.new('Mary','German','F')
puts Human.print

Il ne fonctionne pas.

Bien sûr, je peux utiliser quelque chose comme cela

puts Human.current_humans.inspect

mais je veux apprendre d'autres alternatives!

InformationsquelleAutor Manza | 2013-04-03