小编Sco*_*son的帖子

Ruby:打印和整理数组的方法

我不确定这个问题是否过于愚蠢,但我还没有找到办法.

通常将数组放入循环中我这样做

current_humans = [.....]
current_humans.each do |characteristic|
  puts characteristic
end
Run Code Online (Sandbox Code Playgroud)

但是,如果我有这个:

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
Run Code Online (Sandbox Code Playgroud)

它不起作用. …

ruby arrays class puts

26
推荐指数
1
解决办法
7万
查看次数

标签 统计

arrays ×1

class ×1

puts ×1

ruby ×1