From: GianFranco Bozzetti Date: 2010-04-12T15:55:14+09:00 Subject: Re: Comparing Arrays "Derek Cannon" wrote in message news:24f32d608e99f3ef93bca701fdfe130c@ruby-forum.com... > Hello everyone, another easy question from a beginner: How do you create > a method to compare two arrays so that if they share at least 1 common > element it returns true? > > For example: > a = %w(m w f) > b = %w(m w) > c = %w(t r) > > share_elements?(a,b) #=> true > share_elements?(a,c) #=> false > > Thanks for your time, > Derek > -- > Posted via http://www.ruby-forum.com/. > An answer: # array_cmp.rb def share_elements?(a1,a2) not (a1 & a2).empty? end a = %w(m w f) b = %w(m w) c = %w(t r) r = share_elements?(a,b) #=> true printf("%12s/%-12s => %s\n", a.inspect.to_s, b.inspect.to_s, r.to_s) r = share_elements?(a,c) #=> false printf("%12s/%-12s => %s\n", a.inspect.to_s, c.inspect.to_s, r.to_s) exit(0) Hth gfb