我有一个字符串数组,我想在我的视图中显示所有用“,”分隔的元素。当我执行此数组的每个数组时,如何知道当前元素是否为最后一个?
像这样吗?
- categoryName.each do |name|
- if (name.last) -- the last elemnt of array
= name
- else
= name + ", "
任何的想法!
您可以为此使用Enumerable#each_with_index:
- categoryName.each_with_index do |name, i|
- if (i == categoryName.length - 1) -- the last elemnt of array
= name
- else
= name + ", "
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句