LoginSignup
2
1

More than 5 years have passed since last update.

配列が特定の長さになるまでにパディングする

Last updated at Posted at 2016-09-01

Array#fill を使います。

girls = %w(まどか さやか)
#=> ["まどか", "さやか"]

girls.dup.fill(girls.length...5) { '杏子' }
#=> ["まどか", "さやか", "杏子", "杏子", "杏子"]

dup を外せば、レシーバを直接書き換えます。

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