Ruby Array Find_First Object

Ruby Array find_first object?

Either I don't understand your question, or Enumerable#find is the thing you were looking for.

Find first object with relationship

'find' for enumerable:

collection.to_a.find{ |i| i.associations.present? }

Or you could join the associations and take the first e.g.

People.joins(:children).first

Ruby on rails How to find first item in array that matches items in another array

leaderboard = [{:member=>"1", :score=>7.0, :rank=>1},
{:member=>"5", :score=>6.0, :rank=>2},
{:member=>"4", :score=>5.0, :rank=>3},
{:member=>"3", :score=>4.0, :rank=>4},
{:member=>"2", :score=>3.0, :rank=>5}]

active_members = [3,5]

highest_ranked_active_member = leaderboard.
select { |h| active_members.include? h[:member].to_i }.
min_by { |h| h[:rank] }[:member]
#=> "5"

Ruby grab index of first element of an array that matches criteria

a=[100,200,300]
a.index{ |x| x%3==0 } # returns 2

for your case:

options.index{ |o| o.value == some_value }

find first element in array for which block returns true and return the block's return value

Hopefully still actual: here a solution using detect, i made it possible to verbose the output so you can see which expressions are evaluated before returning a hit.

def find_match symbol, string , verbose = false, match = nil
if verbose
RS.detect{|x,v|x==symbol;v.detect{|re|puts re;match=string.match(/#{re}/)}}
else
RS.detect{|x,v|x==symbol;v.detect{|re|match=string.match(/#{re}/)}}
end
match
end

p find_match :x, 'bb12345'
p find_match :x, 'ee12345' , true #verbose output
p find_match :x, '12345'
p find_match :y, '22abcd'

#<MatchData "bb12345">
(?-mix:^\d+$)
(?-mix:^a\d+$)
(?-mix:^bb\d+$)
(?-mix:^ccc\d+$)
(?-mix:^\w+$)
#<MatchData "ee12345">
#<MatchData "12345">
#<MatchData "22abcd">

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 first n elements from array that match a condition

In Ruby 2.0+ you can write:

require 'prime'

Prime.lazy.select{|p| p > 100 }.take(10).to_a #=> [101, 103, 107, 109, 113, 127, 131, 137, 139, 149]

What's the fastest way in Ruby to get the first enumerable element for which a block returns true?

Several core ruby classes, including Array and Hash include the Enumerable module which provides many useful methods to work with these enumerations.

This module provides the find or detect methods which do exactly what you want to achieve:

arr = [12, 88, 107, 500]
arr.find { |num| num > 100 } # => 107

Both method names are synonyms to each other and do exactly the same.

Find first value in hash array

Solution:

a = [
{"title"=>"un", "link"=>nil, "description"=>""},
{"title"=>"deux", "link"=>"https://apple.com", "description"=>"Products"},
{"title"=>"three", "link"=>"http://www.amazon.com", "description"=>"Welcome"},
{"title"=>"four", "link"=>"https://apple.com", "description"=>"iPad"},
]
a.map{|data| data["link"] }.reject(&:blank?).uniq

Explanation:

a is the array of hashes

Map will go through each hash in the array and create a new array with just the "link" value. If the original hash didn't have a link key it will create a nil value in the new array.

Example result after map: 
[nil, "https://apple.com", "https://www.amazon.com", "https://apple.com"]

Reject then calls the rails active support method blank? on each item in the array and if blank? returns true returns a new array without the value:

[nil.blank?, "https://apple.com".blank?, "https://www.amazon.com".blank?, "https://apple.com".blank?]
Result:
["https://apple.com", "https://www.amazon.com", "https://apple.com"]

Uniq then generates an array without duplicates (an alternative is to use a ruby set)

["https://apple.com", "https://www.amazon.com"]

Find first value of inner array based on second value

You can use the find method:

@array.find{|sub_array| sub_array[1] == <your value here>}.try(:first)


Related Topics



Leave a reply



Submit