* [ruby-core:121727] [Ruby Feature#21284] Request: add `Array#pad` method
@ 2025-04-25 0:16 MatzFan (Brian Cohen) via ruby-core
2025-04-25 2:13 ` [ruby-core:121728] " nobu (Nobuyoshi Nakada) via ruby-core
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: MatzFan (Brian Cohen) via ruby-core @ 2025-04-25 0:16 UTC (permalink / raw)
To: ruby-core; +Cc: MatzFan (Brian Cohen)
Issue #21284 has been reported by MatzFan (Brian Cohen).
----------------------------------------
Feature #21284: Request: add `Array#pad` method
https://bugs.ruby-lang.org/issues/21284
* 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/
^ permalink raw reply [flat|nested] 7+ messages in thread
* [ruby-core:121728] [Ruby Feature#21284] Request: add `Array#pad` method
2025-04-25 0:16 [ruby-core:121727] [Ruby Feature#21284] Request: add `Array#pad` method MatzFan (Brian Cohen) via ruby-core
@ 2025-04-25 2:13 ` nobu (Nobuyoshi Nakada) via ruby-core
2025-04-25 7:35 ` [ruby-core:121729] " MatzFan (Brian Cohen) via ruby-core
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2025-04-25 2:13 UTC (permalink / raw)
To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)
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/
^ permalink raw reply [flat|nested] 7+ messages in thread
* [ruby-core:121729] [Ruby Feature#21284] Request: add `Array#pad` method
2025-04-25 0:16 [ruby-core:121727] [Ruby Feature#21284] Request: add `Array#pad` method MatzFan (Brian Cohen) via ruby-core
2025-04-25 2:13 ` [ruby-core:121728] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2025-04-25 7:35 ` MatzFan (Brian Cohen) via ruby-core
2025-04-27 9:32 ` [ruby-core:121742] " nobu (Nobuyoshi Nakada) via ruby-core
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: MatzFan (Brian Cohen) via ruby-core @ 2025-04-25 7:35 UTC (permalink / raw)
To: ruby-core; +Cc: MatzFan (Brian Cohen)
Issue #21284 has been updated by MatzFan (Brian Cohen).
When I try and run that code, e.g.
``` ruby
[1, 2, 3].fill(nil, -1^..10)
```
``` shell
'Integer#^': Range can't be coerced into Integer (TypeError)
```
In any case `pad(10)` is much prettier IMHO.
----------------------------------------
Feature #21284: Request: add `Array#pad` method
https://bugs.ruby-lang.org/issues/21284#change-112781
* 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/
^ permalink raw reply [flat|nested] 7+ messages in thread
* [ruby-core:121742] [Ruby Feature#21284] Request: add `Array#pad` method
2025-04-25 0:16 [ruby-core:121727] [Ruby Feature#21284] Request: add `Array#pad` method MatzFan (Brian Cohen) via ruby-core
2025-04-25 2:13 ` [ruby-core:121728] " nobu (Nobuyoshi Nakada) via ruby-core
2025-04-25 7:35 ` [ruby-core:121729] " MatzFan (Brian Cohen) via ruby-core
@ 2025-04-27 9:32 ` nobu (Nobuyoshi Nakada) via ruby-core
2025-04-27 9:35 ` [ruby-core:121743] " MatzFan (Brian Cohen) via ruby-core
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2025-04-27 9:32 UTC (permalink / raw)
To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)
Issue #21284 has been updated by nobu (Nobuyoshi Nakada).
Current Ruby doesn't have start-exclusive range.
`^..` is so-called neko-operator in Perl 6, and just this request reminded me of it, sorry.
----------------------------------------
Feature #21284: Request: add `Array#pad` method
https://bugs.ruby-lang.org/issues/21284#change-112798
* 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/
^ permalink raw reply [flat|nested] 7+ messages in thread
* [ruby-core:121743] [Ruby Feature#21284] Request: add `Array#pad` method
2025-04-25 0:16 [ruby-core:121727] [Ruby Feature#21284] Request: add `Array#pad` method MatzFan (Brian Cohen) via ruby-core
` (2 preceding siblings ...)
2025-04-27 9:32 ` [ruby-core:121742] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2025-04-27 9:35 ` MatzFan (Brian Cohen) via ruby-core
2025-04-30 18:24 ` [ruby-core:121783] " matheusrich (Matheus Richard) via ruby-core
2025-04-30 19:16 ` [ruby-core:121784] " MatzFan (Brian Cohen) via ruby-core
5 siblings, 0 replies; 7+ messages in thread
From: MatzFan (Brian Cohen) via ruby-core @ 2025-04-27 9:35 UTC (permalink / raw)
To: ruby-core; +Cc: MatzFan (Brian Cohen)
Issue #21284 has been updated by MatzFan (Brian Cohen).
No worries, I thought it was some new syntax I was not aware of!
----------------------------------------
Feature #21284: Request: add `Array#pad` method
https://bugs.ruby-lang.org/issues/21284#change-112799
* 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/
^ permalink raw reply [flat|nested] 7+ messages in thread
* [ruby-core:121783] [Ruby Feature#21284] Request: add `Array#pad` method
2025-04-25 0:16 [ruby-core:121727] [Ruby Feature#21284] Request: add `Array#pad` method MatzFan (Brian Cohen) via ruby-core
` (3 preceding siblings ...)
2025-04-27 9:35 ` [ruby-core:121743] " MatzFan (Brian Cohen) via ruby-core
@ 2025-04-30 18:24 ` matheusrich (Matheus Richard) via ruby-core
2025-04-30 19:16 ` [ruby-core:121784] " MatzFan (Brian Cohen) via ruby-core
5 siblings, 0 replies; 7+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-04-30 18:24 UTC (permalink / raw)
To: ruby-core; +Cc: matheusrich (Matheus Richard)
Issue #21284 has been updated by matheusrich (Matheus Richard).
What should happen if the size is smaller than what currently set?
----------------------------------------
Feature #21284: Request: add `Array#pad` method
https://bugs.ruby-lang.org/issues/21284#change-112841
* 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/
^ permalink raw reply [flat|nested] 7+ messages in thread
* [ruby-core:121784] [Ruby Feature#21284] Request: add `Array#pad` method
2025-04-25 0:16 [ruby-core:121727] [Ruby Feature#21284] Request: add `Array#pad` method MatzFan (Brian Cohen) via ruby-core
` (4 preceding siblings ...)
2025-04-30 18:24 ` [ruby-core:121783] " matheusrich (Matheus Richard) via ruby-core
@ 2025-04-30 19:16 ` MatzFan (Brian Cohen) via ruby-core
5 siblings, 0 replies; 7+ messages in thread
From: MatzFan (Brian Cohen) via ruby-core @ 2025-04-30 19:16 UTC (permalink / raw)
To: ruby-core; +Cc: MatzFan (Brian Cohen)
Issue #21284 has been updated by MatzFan (Brian Cohen).
> What should happen if the size is smaller than what currently set?
If the `pad_to_length` argument is less than or equal to the array's current size the array is returned unchanged. If it is larger, elements of the specified kind are pushed to the array until it reaches `pad_to_length` size and the modified array returned.
----------------------------------------
Feature #21284: Request: add `Array#pad` method
https://bugs.ruby-lang.org/issues/21284#change-112842
* 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/
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-04-30 19:17 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-25 0:16 [ruby-core:121727] [Ruby Feature#21284] Request: add `Array#pad` method MatzFan (Brian Cohen) via ruby-core
2025-04-25 2:13 ` [ruby-core:121728] " nobu (Nobuyoshi Nakada) via ruby-core
2025-04-25 7:35 ` [ruby-core:121729] " MatzFan (Brian Cohen) via ruby-core
2025-04-27 9:32 ` [ruby-core:121742] " nobu (Nobuyoshi Nakada) via ruby-core
2025-04-27 9:35 ` [ruby-core:121743] " MatzFan (Brian Cohen) via ruby-core
2025-04-30 18:24 ` [ruby-core:121783] " matheusrich (Matheus Richard) via ruby-core
2025-04-30 19:16 ` [ruby-core:121784] " MatzFan (Brian Cohen) via ruby-core
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).