[ruby-core:121728] [Ruby Feature#21284] Request: add `Array#pad` method
From:
"nobu (Nobuyoshi Nakada) via ruby-core" <ruby-core@...>
Date:
2025-04-25 02:13:01 UTC
List:
ruby-core #121728
Issue #21284 has been updated by nobu (Nobuyoshi Nakada).
It may be possible with `Array#fill` and start-exclusive range?
```ruby
ary.fill(object, -1^..padding_length)
```
----------------------------------------
Feature #21284: Request: add `Array#pad` method
https://bugs.ruby-lang.org/issues/21284#change-112780
* Author: MatzFan (Brian Cohen)
* Status: Open
----------------------------------------
A method to pad an array of arbitrary length with objects up to a specified array size does not currently exist.
`Array#fill` does not do this and I therefore propose the following method:
``` ruby
class Array
def pad(pad_to_length, object = nil)
fill(object, size, pad_to_length - size)
end
end
```
I have provided an [answer](https://stackoverflow.com/a/79591610/4114896) to a Stack Overflow question asking how this can be done in Ruby. I've also explained in a comment on the answer which suggests using `Array#fill` why this does not meet the exact need.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/