LoginSignup
5
2

More than 3 years have passed since last update.

each_with_indexしつつinjectしたい

Posted at

こうやる。

(1..3).each.with_index.inject([]) { |r , (val, idx)| r << [val , idx] }
=> [[1, 0], [2, 1], [3, 2]]

rubyは「これできないかなー?」って思ったことがだいたいできるのがいいなと常々思います。

参考 https://www.ruby-forum.com/t/enumerable-inject-with-index/202156/3

5
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2