LoginSignup
3
3

More than 5 years have passed since last update.

[1, 2, 3]から[[1, 2], [2, 3]]を得る

Last updated at Posted at 2012-12-05
a = [1, 2, 3]

# v1
(a.size-1).times.map {|i|
  [a[i], a[i+1]]
}

# v2
a[0..-2].zip(a[1..-1]) # a.size > 0 でないと駄目

# v3 by akicho8さん
a.each_cons(2).to_a # akicho8

もっと読みやすい方法を知りたい.→each_consがよさそう

3
3
2

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
3
3