Magic First and Last Indicator in a Loop in Ruby/Rails?

前端 未结 16 2385
眼角桃花
眼角桃花 2020-12-08 00:01

Ruby/Rails does lots of cool stuff when it comes to sugar for basic things, and I think there\'s a very common scenario that I was wondering if anyone has done a helper or s

16条回答
  •  醉话见心
    2020-12-08 00:54

    You could grab the first and last elements and process them differently, if you like.

    first = array.shift
    last = array.pop
    process_first_one
    array.each { |x| process_middle_bits }
    process_last_one
    

提交回复
热议问题