ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:121798] [Ruby Feature#21300] Suggestion: Method for Array truncation
@ 2025-05-02  8:31 sigsys (Math Ieu) via ruby-core
  2025-05-02  9:38 ` [ruby-core:121799] " Eregon (Benoit Daloze) via ruby-core
  0 siblings, 1 reply; 2+ messages in thread
From: sigsys (Math Ieu) via ruby-core @ 2025-05-02  8:31 UTC (permalink / raw)
  To: ruby-core; +Cc: sigsys (Math Ieu)

Issue #21300 has been reported by sigsys (Math Ieu).

----------------------------------------
Feature #21300: Suggestion: Method for Array truncation
https://bugs.ruby-lang.org/issues/21300

* Author: sigsys (Math Ieu)
* Status: Open
----------------------------------------
`#pop(n)` and `#slice!(...)` can be used for truncation, but they both allocate a new array for the deleted elements (unless you do it one at a time with `#pop()`...), which is not always needed.

I propose adding a `#size=` method for truncating without allocating a new array:

```
a = [10, 20, 30]
a.size = 2
a # [10, 20]
```

Growing the array could be allowed as well...

```
a = [10, 20]
a.size = 4
a # [10, 20, nil, nil]
```




-- 
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] 2+ messages in thread

* [ruby-core:121799] [Ruby Feature#21300] Suggestion: Method for Array truncation
  2025-05-02  8:31 [ruby-core:121798] [Ruby Feature#21300] Suggestion: Method for Array truncation sigsys (Math Ieu) via ruby-core
@ 2025-05-02  9:38 ` Eregon (Benoit Daloze) via ruby-core
  0 siblings, 0 replies; 2+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-05-02  9:38 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #21300 has been updated by Eregon (Benoit Daloze).


Using `ary.size =` feels unidiomatic Ruby to me, it feels very low level and not very expressive about the intent (e.g. it's not clear if shrinking or growing).

I don't think pop(n) returning an Array is big overhead, i.e., I don't think there is enough reason to add another Array method for this rarely-needed case.

----------------------------------------
Feature #21300: Suggestion: Method for Array truncation
https://bugs.ruby-lang.org/issues/21300#change-112864

* Author: sigsys (Math Ieu)
* Status: Open
----------------------------------------
`#pop(n)` and `#slice!(...)` can be used for truncation, but they both allocate a new array for the deleted elements (unless you do it one at a time with `#pop()`...), which is not always needed.

I propose adding a `#size=` method for truncating without allocating a new array:

```
a = [10, 20, 30]
a.size = 2
a # [10, 20]
```

Growing the array could be allowed as well...

```
a = [10, 20]
a.size = 4
a # [10, 20, nil, nil]
```




-- 
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] 2+ messages in thread

end of thread, other threads:[~2025-05-02  9:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-02  8:31 [ruby-core:121798] [Ruby Feature#21300] Suggestion: Method for Array truncation sigsys (Math Ieu) via ruby-core
2025-05-02  9:38 ` [ruby-core:121799] " Eregon (Benoit Daloze) 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).