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

前端 未结 16 2393
眼角桃花
眼角桃花 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:49

    KISS

    arr.each.with_index do |obj, index|
      p 'first' if index == 0        
      p 'last' if index == arr.count-1                  
    end
    

提交回复
热议问题