Nie jestem pewien, czy to pytanie jest zbyt głupie, ale nie znalazłem sposobu, aby to zrobić.Ruby: metoda drukowania i czyszczenia tablicy
Zazwyczaj umieszcza się tablicę w pętli zrobić to
current_humans = [.....]
current_humans.each do |characteristic|
puts characteristic
end
Jednak jeśli mam to:
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
To nie działa.
Oczywiście mogę użyć czegoś jak ten
puts Human.current_humans.inspect
ale chcę nauczyć się innych alternatyw!
Witam, wiem, że to jest stare, ale właśnie robiłem Katas i natknąłem się na ten post. Dlaczego "p" nie powinno być używane (proszę podać głębsze wyjaśnienie niż debugowanie, jeśli to możliwe)? Również użyłem 'p a' i' put a.inspect' na tablicy o nazwie "a" i tylko 'p a' pracował. Czy czegoś brakuje? – rorykoehler