2008/2/26, Jano Svitok <jan.svitok / gmail.com>:
> On Tue, Feb 26, 2008 at 3:20 PM, Zouplaz <user / domain.invalid> 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

For a more efficient solution use a Hash:

def add_user(user)
  @users[user.id] ||= user
end

def users
  @users.values
end

Kind regards

robert

-- 
use.inject do |as, often| as.you_can - without end