29

この質問がばかげているかどうかはわかりませんが、それを行う方法が見つかりません。

通常、配列をループに入れるには、これを行います

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

ただし、これがある場合:

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

うまくいきません。

もちろん、私はこのようなものを使用することができます

puts Human.current_humans.inspect

しかし、私は他の選択肢を学びたいです!

4

1 に答える 1