Can anyone explain range as array#select arguments to me

When you pass a range to select, the begin and end are indexex or the way that human count?

ary = [1, 6, 2, 4, 8]
ary.select!(3..7) # if the end beyond the size, is it loop to begin of the array?
ary # => [6, 4], to me [1, 6] looks like more meaningful 😥
``

I’m pretty sure all it’s doing is filtering values in ary that are between 3 and 7. I.e. the only two are 6 and 4, thus the output is [6, 4].

I do agree that it’s a bit confusing. Maybe we should remove select! and reject! with patterns and instead use grep, which already exists, and also add grep_v (like in Ruby).

3 Likes

While #grep and #grep_v might be more understandable to some programmers (who are familiar with Linux), select and reject are much more clear in terms of simple English language readability. One of my favorite things about Crystal is that it’s easy to read, and I think this is a situation in which clear documentation (which I think we already have for these methods) is better than making the method names more technical.

1 Like

I am surprised that it is the mathematical value…
When array involved range, usually it is the index I think.

Thanks.

I don’t think taking UNIX commands will produce a good API for a general purpose programming language.
Generally, plain English is more descriptive about the action.
There is already select(pattern), we could remove grep(pattern) if we follow the least aliases principle.

1 Like

select for Enumerable and others operate on the values. You can do array[3..6] to get a subrange of an array.

Thanks, didn’t know select also recieve patterns, and range can be a pattern.