Compare object attributes with array of objects Ruby on Rails

I have an object that I want to compare with an array of objects. If two specific attributes are equal I want to stop the loop. How can I do that, or how can I do this in some better Rails way?

@item #item to compare with


        @items.each do |item|
            if ( (item.att1 == @item.att1) && (item.att3 == @item.att3) ) 
                is_equal(item.id) 
            else
                #do something
            end
        end

Answers


Use the find method of an Array:

matched_item = @items.find { | item | item.att1 == @item.att1 && item.att1 == @item.att1 }
is_equal(matched_item.id)

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.