Skip to content

Conversation

fatkodima
Copy link
Contributor

@fatkodima fatkodima commented Aug 23, 2020

Related issue - https://bugs.ruby-lang.org/issues/17056

I have also usecases for this, like in rubocops codebase sometimes we need to find start index and end index of some AST node in the stream of tokens. It is pointless to start searching from the beginning for end index when we already found a start index, so we can provide offset index to the Array#index method.

I don't know how to (better) implement index(offset) { |item| ... }. As in the original code this will generate a warning, skip the block and use an offset argument as a searched obj.

This is my first contribution - sorry, if I made some dumb mistakes.

@marcandre
Copy link
Member

Did you have time to revisit this @fatkodima ?

@fatkodima
Copy link
Contributor Author

@marcandre Do you have suggestions on how to implement

I don't know how to (better) implement index(offset) { |item| ... }. As in the original code this will generate a warning, skip the block and use an offset argument as a searched obj.

?
I.e., if I write

index = array.index(offset: 10) { |e| ... }

then we don't know if we should search for offset: 10 hash in array and produce a warning for an ignored block or if we should search by block with an offset.

I can think of only hacks (implemented in C) like

...
INDEX_DEFAULT = Object.new
...
def index(obj = INDEX_DEFAULT, start: 0, &block)
  if obj != INDEX_DEFAULT
    # search obj argument
  else
    # search using block
  end
  # ...
end

@marcandre
Copy link
Member

I believe that we should search by block with an offset.
I haven't checked the API for keyword arguments, but you could distinguish:

array.index(offset: 10) { |e| ... } # => search by block with offset
array.index({offset: 10}) { |e| ... } # => warn, disregard block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants