[Feature] Add #except to Array

43 views
Skip to first unread message

Alex Golubenko

unread,
Feb 25, 2020, 11:21:35 PM2/25/20
to Ruby on Rails: Core
Hi guys!

Time to time I meet questions on SO about how to take elements from array avoid some indexes.

I think it might be helpful for many developers to have something like `Hash#except` but for `Array`.

I would like to add PR with it if someone interested in it.

Thanks in advance for any thoughts about this idea.

Josh Brody

unread,
Feb 25, 2020, 11:34:23 PM2/25/20
to rubyonra...@googlegroups.com
Would this alias Array#excluding? I think the Array#except is natural and I’ve found myself asking why it’s not aliased already many times but I’m not the one to ask. 🙂

Sent from my iPhone

On Feb 25, 2020, at 10:21 PM, Alex Golubenko <alexandr1...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/479e2902-3cb1-4bb3-9347-5aeefe27379d%40googlegroups.com.

Alex Golubenko

unread,
Feb 26, 2020, 3:49:07 AM2/26/20
to rubyonra...@googlegroups.com
It wouldn't be alias 😅
The main idea is to implement method which we can use to exclude elements from array by their indexes.

For example: 
%w( a b c d e f).except(0, -1) 
=> ['b', 'c', 'd', 'e']

ср, 26 февр. 2020 г., 06:34 Josh Brody <jo...@josh.mn>:

Nicholas Schwaderer

unread,
Feb 26, 2020, 7:19:25 AM2/26/20
to Ruby on Rails: Core
Nice suggestion! :) 

I see how it would replicate the Hash#except syntax as seen here: https://apidock.com/rails/Hash/except

However, could this be confusing for those who don't anticipate the index stripping behavior but more of a reject/excluding behavior?

i.e.:

```
%w( 1 5 6 8 0 9 0 ).except(0)
#=> [5,6,8,0,9,0]
```

Would it be better to explicitly add the index to the name such as `#except_index` or more verbose `#except_with_index` ? 



On Wednesday, February 26, 2020 at 8:49:07 AM UTC, Alex Golubenko wrote:
It wouldn't be alias 😅
The main idea is to implement method which we can use to exclude elements from array by their indexes.

For example: 
%w( a b c d e f).except(0, -1) 
=> ['b', 'c', 'd', 'e']

ср, 26 февр. 2020 г., 06:34 Josh Brody <jo...@josh.mn>:
Would this alias Array#excluding? I think the Array#except is natural and I’ve found myself asking why it’s not aliased already many times but I’m not the one to ask. 🙂
Sent from my iPhone

On Feb 25, 2020, at 10:21 PM, Alex Golubenko <alexandr1...@gmail.com> wrote:


Hi guys!

Time to time I meet questions on SO about how to take elements from array avoid some indexes.

I think it might be helpful for many developers to have something like `Hash#except` but for `Array`.

I would like to add PR with it if someone interested in it.

Thanks in advance for any thoughts about this idea.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.

Alex Golubenko

unread,
Feb 26, 2020, 7:25:52 AM2/26/20
to rubyonra...@googlegroups.com
I'm totally agree that it might be confusing so yeah, maybe `#except_index` will be the best variant 😅

ср, 26 февр. 2020 г., 14:19 Nicholas Schwaderer <nicholas....@gmail.com>:
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/6904f0ef-9b96-432d-95a4-f35d8892303b%40googlegroups.com.

Thushara Wijeratna

unread,
Feb 26, 2020, 5:31:38 PM2/26/20
to Ruby on Rails: Core
What would be a typical use case for such a function - I can understand the special case of wanting to remove the first / last items in the array, but an arbitrary one?

Alex Golubenko

unread,
Feb 27, 2020, 8:03:07 AM2/27/20
to Ruby on Rails: Core
I think your question is kinda abstractive because many Rails/Ruby methods is mostly for special cases and many of them might be not used ever by most of the projects.


четверг, 27 февраля 2020 г., 0:31:38 UTC+2 пользователь Thushara Wijeratna написал:

Ян Матвейчук

unread,
Feb 27, 2020, 8:23:14 AM2/27/20
to Ruby on Rails: Core
I think there should be a lot of use cases. It's not gonna be the fastest method, but it will be quite popular. From my side I implemented such a method and used it for complex data parsing. I would recommend to add ranges to arguments.   

Alex Golubenko

unread,
Feb 29, 2020, 6:56:15 AM2/29/20
to Ruby on Rails: Core
I opened PR: https://github.com/rails/rails/pull/38611

среда, 26 февраля 2020 г., 6:21:35 UTC+2 пользователь Alex Golubenko написал:
Reply all
Reply to author
Forward
0 new messages