我需要将哈希值输出到表格中。数据有一个名为students
的数组,其中包含带有键"first_name"
,"last_name"
和"grade_level"
的哈希值。
这是我的代码。
students = []
# Dummy Inputs.
students = [
{
"first_name" => "Bob",
"last_name" => "Builder",
"grade_level" => 4
},
{
"first_name" => "Test",
"last_name" => "Buida",
"grade_level" => 3
},
{
"first_name" => "Senior",
"last_name" => "June",
"grade_level" => 5
},
{
"first_name" => "John",
"last_name" => "Smith",
"grade_level" => 2
},
{
"first_name" => "Me",
"last_name" => "Developer",
"grade_level" => 11
}]
...
puts "\n--------------- \n" +
"| CLASS ROSTER \n" +
"--------------- \n" # Felt lazy to add them individual put
puts "First Name Last Name Grade Level\n"
students.each do |student|
puts "#{student["first_name"]} #{student["last_name"]} #{student["grade_level"]}"
我使用tab设置列。有时,它会像下面一样出现故障。
有没有办法让这更好?
答案 0 :(得分:2)
可能应该在这里使用String#ljust
和String#rjust
助手。
首先,让我们准备输出:
puts titles, roster
现在你可以打印出来了:
First Name | Last Name | Grade Level
Bob Builder 4
Test Buida 3
Senior June 5
John Smith 2
Me Developer 11
这是输出:
touchesBegan
随意更改加入者和字段大小以查看其工作原理。