From: "Dan0042 (Daniel DeLorme) via ruby-core" Date: 2026-07-25T15:04:03+00:00 Subject: [ruby-core:126167] [Ruby Feature#17056] Array#index: Allow specifying the position to start search as in String#index Issue #17056 has been updated by Dan0042 (Daniel DeLorme). ### 1. Object, block, and Enumerator forms Imho there are two options here a) If block form is supported, the offset is a keyword argument. It was not requested by the OP (also not in my duplicate #19177), but it's more versatile and future-proof. Although it introduces incompatibility due to keyword extension (described below) ```ruby index(object, offset: 0) index(offset: 0){ |element| ... } index(offset: 0) ``` b) If block form is not supported, the offset should be a positional argument, for consistency and duck-typing with String#index It's more limited, and makes it hard to add offset to block form in the future. ```ruby index(object, offset = 0) ``` Overall a) seems better to me, even if not fully consistent with String#index ### 2. Compatibility with searching for a Hash Ah yes, this is the standard problem with [keyword extension](#14183#note-29) What would happen with this code? ```ruby [{ key: 2 }].index(key: 2) ``` Will it now raise "unknown keyword: :key" ? ### 3. Semantics of `Array#rindex` > I think the corresponding Array behavior should be: > > ```ruby > [:a, :b, :a, :b, :a].rindex(:a, offset: 2) # => 2 > [:a, :b, :a, :b, :a].rindex(:a, offset: -2) # => 2 > [:a, :b, :a, :b, :a].rindex(:a, offset: 100) # => 4 > ``` 100% agree. ### 4. Boundary and conversion behavior > Should `Array#index` and `Array#rindex` follow the corresponding String methods for all of these cases? In general I would say yes, simply because having consistency for this reduces gotchas and footguns. But I'm not sure about this behavior of String: ```ruby "0aaaaaaaaaa9".rindex("a", 999) #=> 10 "0aaaaaaaaaa9".index("a", -999) #=> nil ``` To me, these two results are inconsistent and I feel like the last case should return 1. Should Array#index follow the same pattern or not? ```ruby [:a, :b, :a, :b, :a].index(:a, offset: -100) #=> nil (like String#index) or 0 ? ``` ---------------------------------------- Feature #17056: Array#index: Allow specifying the position to start search as in String#index https://bugs.ruby-lang.org/issues/17056#change-118233 * Author: TylerRick (Tyler Rick) * Status: Open ---------------------------------------- I have a use case of finding the first matching line within a given section in a file. After finding the line number of the start of the section, I want to find the first match after that line. My workaround for now is to use `with_index`: ```ruby lines = pathname.read.lines section_start_line = lines.index {|line| line.start_with?(/#* #{section_name}/) } lines.index.with_index {|line, i| i > section_start_line && line.include?(sought) } ``` I'd like to do it in a more concise way using a feature of `Array#index` that I propose here, which is analogous to `String#index`. If the second parameter of `String#index` is present, it specifies the position in the string to begin the search: ```ruby 'abcabc'.index('a') # => 0 'abcabc'.index('a',2) # => 3 ``` I would expect to also be able to do: ```ruby 'abcabc'.chars.index('a') # => 0 'abcabc'.chars.index('a', 2) ``` Using such feature, I would be able to do: ```ruby lines.index(sought, section_start_line) ``` This would give Ruby better parity with other programming languages like Python: ```python >>> list('abcabc') ['a', 'b', 'c', 'a', 'b', 'c'] >>> list('abcabc').index('a') 0 >>> list('abcabc').index('a', 2) 3 ``` ## End index We can further think of an optional parameter to specify the position to end the search. The following languages allow specifying both start and end indexes: - [Python](https://docs.python.org/3/tutorial/datastructures.html) - [C#](https://docs.microsoft.com/en-us/dotnet/api/system.array.indexof?view=netcore-3.1) Ruby's `String#index` does not have one, so we could make a separate proposal to add `end` to both methods at the same time. -- 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/