31
23

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.

配列の n 個目以降を取得

Last updated at Posted at 2013-02-26

ってどうやるのかなぁと思ったら教えてモラタ

[1] pry(main)> [1,2,3,4,5][1..-1]
=> [2, 3, 4, 5]

すごい!!!
1..-1 とか Range としておかしそうなのに!!
Ruby すごい!!

ちなみに

[2] pry(main)> [1,2,3,4,5][10..-1]
=> nil

nil 帰ってくることあるから気をつけてねと教わりました!!

追記
Rails というか ActiveSupport では

[1] pry(main)> [1,2,3,4,5].from(1)
=> [2, 3, 4, 5]
[2] pry(main)> [1,2,3,4,5].from(10)
=> []

というのがあると教えてもらいますた!!!
こちらは範囲外では空配列返ってくるんですね!!

def from(position)
  self[position, length] || []
end

コードみたらこんなでした!!

Rails 凄い!! 驚き最大限!!!

31
23
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
31
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?