From: Jacob Fugal Date: 2006-04-20T02:05:44+09:00 Subject: Re: Is there an "in" operator in ruby ? On 4/19/06, Dirk Meijer wrote: > @test=[2,4,6,8] > > (1..10).each do |n| > if @test.include?(n) > #execute code > end > end On 4/19/06, Matthew Moss wrote: > Why all that when: > > @test.each do |n| > # execute code > end Ah, but what if @test includes elements not in the original loop? Eg: @test=[2,4,6,8,13] (1..10).each do |n| if @test.include?(n) #execute code end end # => executed for 2, 4, 6, 8 @test.each do |n| # execute code end # => executed for 2, 4, 6, 8, 13 But, in agreeance with other posters in the thread, I'd actually use some set arithmetic: ([*1..10] & @test).each do |n| # execute code end Of course, this assumes that the elements of the outer loop are known in advance, as opposed to being generated in a while loop, a non-expanded large range, etc. Jacob Fugal