-1

我正在尝试将二维数组输出到控制台;我希望数组中的信息能够很好地格式化,如本问题末尾我想要的输出所示。我的数组创建如下(创建FamilyMember类的实例):

#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' )

我希望能够将数组的内容输出到格式化为字符串的控制台。我需要能够以两种方式做到这一点 - 使用each和单独使用do.

我尝试过的(受之前的 SO question 启发):

def eacharray(an_array)
  an_array.each do |inner|
      puts inner.join(" ")
  end
end

eacharray(fm)

但是上面的输出如下:

1 #<FamilyMember:0x000000027e7d48>
2 #<FamilyMember:0x000000027e7c58>
3 #<FamilyMember:0x000000027e7b68>
4 #<FamilyMember:0x000000027e7a78>
5 #<FamilyMember:0x000000027e7988>

如何使用each和输出格式良好的 2D 数组元素do?任何帮助表示赞赏。谢谢。

理想情况下,我的输出将是这样的:

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 
4

3 回答 3

7

如果您对固定格式感到满意(您可以根据每个字段中的最大数据宽度动态构建格式),您可以编写类似这样的内容。

class FamilyMember
  attr_accessor :name, :sex, :type, :role, :age
  def initialize (*args)
    @name, @sex, @type, @role, @age = args
  end
end

fm = []
fm << FamilyMember.new( 'Andrew',   'Male',   'Child',  'Son' ,    '27' )
fm << FamilyMember.new( 'Bill',     'Male',   'Parent', 'Father',  '63' )
fm << FamilyMember.new( 'Samantha', 'Female', 'Parent', 'Mother',  '62' )
fm << FamilyMember.new( 'Thomas',    'Male',  'Child',  'Dog' ,    '10' )
fm << FamilyMember.new( 'Samantha', 'Female', 'Child',  'Dog' ,     '4' )

format = '%-15s %-8s %-7s %-7s %-7s %s'
puts format % ['Family Member', 'Name', 'Sex', 'Type', 'Role', 'Age']
fm.each_with_index do |member, i|
  puts format % [ i+1, member.name, member.sex, member.type, member.role, member.age ]
end

输出

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

您也可以使用迭代器for ... in,它实际上编译成几乎相同的循环。each

i = 0
for member in fm do
  i += 1
  puts format % [ i, member.name, member.sex, member.type, member.role, member.age ]
end

或者您可以使用大多数 Ruby 程序员忘记的原始while或循环构造。untilRuby 使用它的迭代器更具表现力。

i = 0
while member = fm[i] do
  i += 1
  puts format % [ i, member.name, member.sex, member.type, member.role, member.age ]
end

请注意do,您可以从最后两个示例中省略。只要在while表达式后面有一个换行符(或分号),Ruby 就可以理解。

于 2013-09-28T15:43:09.100 回答
1

像 FamilyMember 这样的类最容易用 Struct 构造。结果只是一个类,但有一些额外的功能。

FamilyMember = Struct.new(:name, :sex, :type, :status, :age)
fm = []
fm << FamilyMember.new('Andrew','Male', 'Child', 'Son' , '27' )
fm << FamilyMember.new('Bill','Male', 'Parent', 'Father' , '63' )

puts FamilyMember.members.map(&:capitalize).join("\t") #members gives you the names of all methods
fm.each{|fam_member|puts fam_member.values.join("\t")} #another feature.

输出:

 Name   Sex Type    Status  Age
 Andrew Male    Child   Son 27
 Bill   Male    Parent  Father  63

只排了...

于 2013-09-28T15:46:57.440 回答
1

我明白,这可能与现在无关。但只是分享这个,以便有人可以利用。

试试这个宝石 - 终端表

终端表

于 2017-06-06T11:09:27.087 回答