我有給定一組學生和他們的等級的功能,使得分類:這是你如何測試功能以在Ruby中塊?
students = {"Good student":"A","Bad Student":"D","Average student":"C"}
student_cat = ["Best","So-so","Below average"]
我的功能是這樣的:現在我測試它像這樣
categorize(students,student_cat) do |s,g|
# s would be "Good student" and g would be "Best"
# s would be "Bad student" and g would be "Below average"
# s would be "Average student" and g would be "So-so"
end
:
categorize(students,student_cat) do |s,g|
assert g == "Best" if s == "Good student"
assert g == "Below average" if s == "Bad student"
assert g == "So-so" if s == "Average student"
end
是否有另一種測試功能的方式以塊爲參數?這足夠好嗎?
我不知道這是否是多數人的意見,但IMO塊,其返回的集合不應該屈服工作它的元素,這導致了必要的編碼(例如參見韋恩的答案)。因此通常:1)返回一個簡單陣列,或者2)如果期望長的輸出,返回一個枚舉(這是懶惰) – tokland 2011-05-15 14:09:00