Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
22
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Organization

配列の n 個目以降を取得

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

[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 凄い!! 驚き最大限!!!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
22
Help us understand the problem. What are the problem?