Ruby inject with index and brackets

前端 未结 4 1151
谎友^
谎友^ 2021-02-12 10:34

I try to clean my Code. The first Version uses each_with_index. In the second version I tried to compact the code with the Enumerable.inject_with_index-constr

4条回答
  •  一生所求
    2021-02-12 10:52

    lines = %w(a b c)
    indexes = lines.each_with_index.inject([]) do |acc, (el, ind)|
      acc << ind - 1 if el == "b"
      acc
    end
    
    indexes # => [0]
    

提交回复
热议问题