From: Mikel Lindsaar Date: 2008-07-07T15:23:19+09:00 Subject: Re: Find matching elements in array On Mon, Jul 7, 2008 at 4:04 PM, Pragash Mr. wrote: > I have one array for example[1,2,3,4] > i need to find the matching elements of another array is there any way > to find.... > for example [1,2,3,4].include?(2) > it will return true > but i need to find one or more elements > example : [1,2,3,4].include?(2,3) > it is throwing an error Depends what you want, if you just want to find out if the numbers 2 and 3 are in the other array, you could use: == Array RDoc: array & other_array Set Intersection—Returns a new array containing elements common to the two arrays, with no duplicates. [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ] so you could do something like this: >> class Array >> def contains?(array) >> (self & array) == array >> end >> end => nil >> ?> [1,2,3,4].contains?([1,2]) => true ?> [1,2,3,4].contains?([1,3]) => true >> [1,2,3,4].contains?([1,9]) => false >> [1,2,3,4].contains?([12,3]) => false >> [1,2,3,4].contains?([2,3]) => true Mikel -- http://lindsaar.net/ Rails, RSpec and Life blog....