Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
对于一个数组[1, 2, 3, 4],,当我处理每个元素时,我怎么知道我在数组的最后一个元素上。
[1, 2, 3, 4],
arr = [1, 2, 3, 4] arr.each do |a| ... end
arr = [1, 2, 3, 4] l = arr.length - 1 arr.each_with_index do |a, i| if i == l ... else ... end end
arr = [1, 2, 3, 4] arr.each_with_index do |el, idx| if idx == arr.length - 1 # you're on the last element end end