Is There an Equivalent of Array#Find_Index for the Last Index in Ruby

Is there an equivalent of Array#find_index for the last index in ruby?

In Ruby 1.9.2 Array#rindex accepts block: http://apidock.com/ruby/Array/rindex

Get index of array element faster than O(n)

Convert the array into a hash. Then look for the key.

array = ['a', 'b', 'c']
hash = Hash[array.map.with_index.to_a] # => {"a"=>0, "b"=>1, "c"=>2}
hash['b'] # => 1

How do I find an index of an element in an array matching a condition and starting the search from a particular point in the array?

Using select would return all values where the block returns true for example:

p arr = ["abc", "d", "efg", "h", "abcde", "k"]
# => ["abc", "d", "efg", "h", "abcde", "k"]
p arr.each_index.select{|i| i >= 2 and arr[i].length == 1}
# => [3, 5]

Instead use detect if you want to return only the first value where the block returns true:

p arr = ["abc", "d", "efg", "h", "abcde", "k"]
# => ["abc", "d", "efg", "h", "abcde", "k"]
p arr.each_index.detect{|i| i >= 2 and arr[i].length == 1}
# => 3

Find index of array in multidimensional array by string value

That's returning nil, because there's no element i (index) in the array with value #FFF600 (nor FFF600), you need to access to the hex_value key value:

p [
{:id=>5, :name=>"Leaf Green", :hex_value=>"047115"},
{:id=>15, :name=>"Lemon Yellow", :hex_value=>"FFF600"},
{:id=>16, :name=>"Navy", :hex_value=>"285974"}
].yield_self { |this| this.each_index.select { |index| this[index][:hex_value] == 'FFF600' } }
# [1]

Giving you [1], because of using select, if you want just the first occurrence, you can use find instead.

I'm using yield_self there, to avoid assigning the array to a variable. Which is equivalent to:

array = [
{:id=>5, :name=>"Leaf Green", :hex_value=>"047115"},
{:id=>15, :name=>"Lemon Yellow", :hex_value=>"FFF600"},
{:id=>16, :name=>"Navy", :hex_value=>"285974"}
]
p array.each_index.select { |index| array[index][:hex_value] == 'FFF600' }
# [1]

Being Ruby, you can use the method for that: Enumerable#find_index

p [
{:id=>5, :name=>"Leaf Green", :hex_value=>"047115"},
{:id=>15, :name=>"Lemon Yellow", :hex_value=>"FFF600"},
{:id=>16, :name=>"Navy", :hex_value=>"285974"}
].find_index { |hash| hash[:hex_value] == 'FFF600' }
# 1

In Ruby how do I find the index of one of an array of elements?

find_index takes a single element. You could find the minimum by doing something like

a = ["a", "b", "c"]
to_find = ["b", "c"]
to_find.map {|i| a.find_index(i) } .compact.min # => 1

In Ruby, how do I find the index of an element in an array in a case-insensitive way?

Use Array#find_index:

a = ["A", "B", "C"]
a.find_index {|item| item.casecmp("b") == 0 }
# or
a.find_index {|item| item.downcase == "b" }

Note that the usual Ruby caveats apply for case conversion and comparison of accented and other non-Latin characters. This will change in Ruby 2.4. See this SO question: Ruby 1.9: how can I properly upcase & downcase multibyte strings?

How to do find_index for multiple values in a Ruby array?

In Ruby 1.8.7 and 1.9, iterator methods called without a block return an Enumerator object. So you could do something like:

[0, 0, 1, 0, 1].each_with_index.select { |num, index| num > 0 }.map { |pair| pair[1] }
# => [2, 4]

Stepping through:

[0, 0, 1, 0, 1].each_with_index
# => #<Enumerator: [0, 0, 1, 0, 1]:each_with_index>
_.select { |num, index| num > 0 }
# => [[1, 2], [1, 4]]
_.map { |pair| pair[1] }
# => [2, 4]

indexOf in Ruby

It is the .index method of Array.

http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-index

In ruby, only false and nil are considered as false value, so you could just do:

arr = %w{a, b, c}
c = 'c'
if arr.index c
# do something
else
# do something else
end


Related Topics



Leave a reply



Submit