📌  相关文章
📜  ruby 按属性在数组中查找对象 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:44.210000             🧑  作者: Mango

代码示例1
# Use select to get all objects in an array that match your criteria
my_array.select { |obj| obj.attr == 'value' }

# find_all and filter (in Ruby 2.6+) are aliases for select
my_array.find_all { |obj| obj.attr == 'value' }
my_array.filter { |obj| obj.attr == 'value' }

# Use find to get the first object in an array that matches your criteria
my_array.find { |obj| obj.attr == 'value' }