Ruby find and return objects in an array based on an attribute
array_of_objects.select { |favor| favor.completed == false }
Will return all the objects that's completed is false.
You can also use find_all
instead of select
.
Return object after performing intersection of two arrays based on attribute
you can:
1 :
override the eql?(other)
method then the array intersection will work
class Link < ApplicationRecord
def eql?(other)
self.class == other.class && self.id == other&.id # classes comparing class is a guard here
end
# you should always update the hash if you are overriding the eql?() https://stackoverflow.com/a/54961965/5872935
def hash
self.id.hash
end
end
2:
use array.select
:
array_links.flat_map {|i| selected_links.select {|k| k.user_id == i.user_id }}
get element in array based on element's attributes
Ruby's selected method is probably what you're looking for.
obj_array.select { |obj| obj.type == type1 }
Obviously make sure you define type1
.
Searching array of objects for item in Ruby
Your array contains a Hash, with keys that are symbols (in hashes, key: value
is a shorthand for :key => value
). Therefore, you need to replace item.login
with item[:login]
:
name = "joseph"
array = [{"login":"joseph","id":4,"url":"localhost/joe","description":nil},
{"login":"billy","id":10,"url":"localhost/billy","description":nil}]
arrayIndex = array.find_index{ |item| item[:login] == name }
puts arrayIndex
The code above retrieves the index at which the sought object is in the array. If you want the object and not the index, use find
instead of find_index
:
arrayItem = array.find{ |item| item[:login] == name }
Also, note that in Ruby, null
is actually called nil
.
Iterate over Array of Objects and Return Attributes
Use Array#map
to invoke the title
method on each and create a new array with the results:
loe.article.map(&:title)
The above is shorthand for
loe.article.map{ |o| o.title }
Return object attribute from array of objects in Ruby
You can avoid the creation of a @results variable which you need to return explicitly by using the Enumerable#map method, something like this:
items.map do |attribs|
a = attribs.item_attributes
{:label => "#{a.title.to_s[0,85] unless a.title.nil?}",
:value => "#{a.title.to_s unless a.title.nil?}",
:img => "#{attribs.medium_image.url.to_s unless attribs.medium_image.url.nil?}"
}
end
Enumerable#map returns an array of the return values of running the supplied block on each item in the receiving array. So if the intention in your original method is to return @results, you might be better off using map.
Incidentally, even if you put 'return @results' at the end of the method as it stands, it will only give the attributes computed in the last iteration of the loop, since you reset @results to [] each time the block is called. You could either avoid this by initializing @results to [] before the loop or by avoiding it entirely using map.
Find duplicate objects in an array based on a subset of attributes in Ruby
cars.group_by{|car| [car.engine_size, car.accelaration, car.mass]}
Related Topics
"Use" Keyword/Word in Ruby/Rails/Rack Code
Regex to Match Hashtags in a Sentence Using Ruby
How to Make a Check Box Default to Being "Checked" in Rails 1.2.3
Retrieving Image Height with Carrierwave
Machinist VS Factorygirl - Pros and Cons
To Make an API Call with an Oauth Token
Rails Join a List of Strings with Commas and "And" Before the Last
Is There a Ruby One-Line "Return If X"
Add_Foreign_Key VS Add_Reference in Rails
How to Get the File Extension from a Url
Ruby Gems in Stand-Alone Ruby Scripts
After Installing a Gem Within a Script, How to Load the Gem
Programmatically Getting Full Ruby Version