从数组中提取名称并在每个名称后面添加换行符

时间:2022-06-09 08:40:32

Below is the unit test I wrote for a small piece of Ruby code.

下面是我为一小段Ruby代码编写的单元测试。

subject { AutoShop.new 'Mr. Fix It', 'WA987654321', Employee.new('Sue', 'Owner', 0) }

it '#employee_list returns list of employee names' do
  # setup
  input = [Employee.new('Lou', 'Receptionist', 90_000)]
  subject.append_employees input
  subject.employee_list.should eq "Sue\nLou"
end

And here is the code for the class. However, I cannot get the names to display properly in the employee_list method. I'm brand new to Ruby, so any suggestions would be greatly appreciated.

这是这个类的代码。但是,在employee_list方法中,不能正确显示名称。我是Ruby的新手,所以任何建议都非常感谢。

class AutoShop < Business

    attr_accessor :employees

  def initialize(name, tax_id, employee)
    super(name, tax_id)
    @employees = []
    @employees << employee
  end   

  def append_employees(input)
    input.map { |x| @employees << x  }  
  end

  def employee_list
    @employees.map {|x| x.name}
  end   

end     

2 个解决方案

#1


4  

Your employee_list method, based on your unit test, should output each name separated by a linebreak \n. So, add a .join("\n") to produce a string from the array.

您的employee_list方法,基于您的单元测试,应该输出每个名称,用一个linebreak \n分隔。因此,添加.join(“\n”)以从数组中生成字符串。

def employee_list
  @employees.map {|x| x.name }.join("\n")
end  

The .map can be done with a "pretzel-colon" shorthand too:

map也可以用“椒盐脆饼-冒号”来表示:

@employees.map(&:name).join("\n")

#2


1  

Although @MichaelBerkowski already answered your main question, I would pay heed to your append_employees method looks a bit strange. You were likely to use Array#each, not Array#map there. While @employees variable is not affected by that change, the returning value differs (unnecessarily gorging your memory). Compare:

虽然@MichaelBerkowski已经回答了您的主要问题,但是我要注意append_employees方法看起来有点奇怪。您可能会使用数组#each,而不是数组#map。虽然@employees变量不受该更改的影响,但是返回值是不同的(不必要地占用您的内存)。比较:

2.0.0-preview2 :001 > input = [1, 2, 3, 4]
 => [1, 2, 3, 4] 
2.0.0-preview2 :002 > e = [] ; input.each { |x| e << x  }
 => [1, 2, 3, 4] 
2.0.0-preview2 :003 > e = [] ; input.map { |x| e << x  }
 => [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]] 

#1


4  

Your employee_list method, based on your unit test, should output each name separated by a linebreak \n. So, add a .join("\n") to produce a string from the array.

您的employee_list方法,基于您的单元测试,应该输出每个名称,用一个linebreak \n分隔。因此,添加.join(“\n”)以从数组中生成字符串。

def employee_list
  @employees.map {|x| x.name }.join("\n")
end  

The .map can be done with a "pretzel-colon" shorthand too:

map也可以用“椒盐脆饼-冒号”来表示:

@employees.map(&:name).join("\n")

#2


1  

Although @MichaelBerkowski already answered your main question, I would pay heed to your append_employees method looks a bit strange. You were likely to use Array#each, not Array#map there. While @employees variable is not affected by that change, the returning value differs (unnecessarily gorging your memory). Compare:

虽然@MichaelBerkowski已经回答了您的主要问题,但是我要注意append_employees方法看起来有点奇怪。您可能会使用数组#each,而不是数组#map。虽然@employees变量不受该更改的影响,但是返回值是不同的(不必要地占用您的内存)。比较:

2.0.0-preview2 :001 > input = [1, 2, 3, 4]
 => [1, 2, 3, 4] 
2.0.0-preview2 :002 > e = [] ; input.each { |x| e << x  }
 => [1, 2, 3, 4] 
2.0.0-preview2 :003 > e = [] ; input.map { |x| e << x  }
 => [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]