From: Craig Demyanovich Date: 2008-02-26T23:41:25+09:00 Subject: Re: Array help needed ------=_Part_19604_2084850.1204036893889 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Tue, Feb 26, 2008 at 9:36 AM, Jano Svitok wrote: > On Tue, Feb 26, 2008 at 3:20 PM, Zouplaz wrote: > > Hello, sorry if this question is stupid but I can't find a solution > > after several readings about Arrays > > > > Array 'a' contains several objets (ruby classes instances), each > > instance have an id property > > > > > > I need a clean solution to return true if array 'a' contains an > instance > > with id foo without iterating the whole array. > > > > Example > > > > def add_user(user) > - unless users.include?(user.id) > + unless users.any? {|u| user.id == u.id } > > users << user > > end > > end If you can't do this def add_user(user) unless users.include?(user) # just check the user instead of the id users << user end end then I'd also recommend Jano's solution. Regards, Craig ------=_Part_19604_2084850.1204036893889--