From: Hassan Schroeder Date: 2012-04-11T11:19:52+09:00 Subject: Re: less absolutely horrible way to make sure array 1 only contains elements from array 2? On Tue, Apr 10, 2012 at 6:44 PM, ruby ismyname wrote: > I need to make sure that myselections only contains elements from > choices, includes? doesn't work because it allows for partial matches. > This works but it looks absolutely horrible. I have to agree with that last sentence :-) I'm not sure what 'partial matches' means in this context but maybe this will be useful -- 1.9.2-p290 :024 > choices = %w[a b c d e f g h i j k] => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"] 1.9.2-p290 :025 > selections = %w[c h k] => ["c", "h", "k"] 1.9.2-p290 :026 > bad_selections = %w[a b z] => ["a", "b", "z"] 1.9.2-p290 :027 > selections.drop_while{|selection| choices.include? selection } => [] 1.9.2-p290 :028 > bad_selections.drop_while{|selection| choices.include? selection } => ["z"] 1.9.2-p290 :029 > selections.drop_while{|selection| choices.include? selection }.count > 0 => false 1.9.2-p290 :030 > bad_selections.drop_while{|selection| choices.include? selection }.count > 0 => true I'm sure this is also one of those TMTOWTDI cases :-) -- Hassan Schroeder ------------------------ hassan.schroeder@gmail.com http://about.me/hassanschroeder twitter: @hassan