1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby最大値(max)と最小値(min)の位置インデックス(index)を得る方法

Last updated at Posted at 2018-11-05
ruby
 a=[1,2].each_with_index.max[1]
# =>1
 a=[1,2].each_with_index.min[1]
# =>0

1番から始めたい場合

ruby
 a=[1,2].each_with_index.max[1]+1
# =>2
 a=[1,2].each_with_index.min[1]+1
=>1

参考
https://stackoverflow.com/questions/2149802/in-ruby-what-is-the-cleanest-way-of-obtaining-the-index-of-the-largest-value-in

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?