-1
如何在ruby中查找數組中最長的名稱。這是我試過的:試圖在Ruby中查找數組中最長的字符串
people = ["john", "clark", "stan", "mike", "nick", "devon"]
def find_longest_name(people)
longest_name = nil
longest_name_length = -1
people.each do |friend|
if friend.length > longest_name_length
longest_name = frined
longest_name_length = friend.length
end
end
return longest_name
end
puts "#{longest_name_length}"
我得到一個錯誤,說我有一個未定義的變量。
'frined'!='friend' – axiac
*你找哪家*錯誤?請閱讀* whole *錯誤,它會*告訴*你a)*完全*在哪裏找到未定義的變量和b)該變量的*精確*名稱是什麼。看看這個名字。你1000000%確定這是變量的名稱? –
在'puts「#{longest_name_length}」'中,'longest_name_length'在方法定義中定義。將無法從外部訪問。 –