Is there an "in" operator in ruby?

Hi all,

I want to do this manipulation. I have an array say @test=[2,4,6,8].
How can i compare a given number against the values which are present in
this array? ie.

In a for loop i will be Iterating through a sequence of number say 1 to
10, and i want to take each of these number 1 to 10 and check if this
number is present in the given array @test, if it is present then i want
to execute some sequence of steps, How can i do this check in ruby ?

Is there a kind of "in" operator in ruby ?

Thank You
Dinesh

···

--
Posted via http://www.ruby-forum.com/.

ar = [1, 2, 3, 4, 5, 6]
ar.include? 2 => true

···

On Apr 19, 2006, at 10:45 AM, Dinesh Umanath wrote:

Hi all,

I want to do this manipulation. I have an array say @test=[2,4,6,8].
How can i compare a given number against the values which are present in
this array? ie.

In a for loop i will be Iterating through a sequence of number say 1 to
10, and i want to take each of these number 1 to 10 and check if this
number is present in the given array @test, if it is present then i want
to execute some sequence of steps, How can i do this check in ruby ?

Is there a kind of "in" operator in ruby ?

Thank You
Dinesh

--
Posted via http://www.ruby-forum.com/\.

Yes, Enumerable#include?:

irb(main):001:0> 1.upto(10) { |x| puts x if [2,4,6,8].include?(x) }
2
4
6
8
=> 1

-- fxn

···

On Apr 19, 2006, at 17:45, Dinesh Umanath wrote:

Hi all,

I want to do this manipulation. I have an array say @test=[2,4,6,8].
How can i compare a given number against the values which are present in
this array? ie.

In a for loop i will be Iterating through a sequence of number say 1 to
10, and i want to take each of these number 1 to 10 and check if this
number is present in the given array @test, if it is present then i want
to execute some sequence of steps, How can i do this check in ruby ?

Is there a kind of "in" operator in ruby ?

for i in [2,4,6,8]
   puts i
end

···

On Apr 19, 2006, at 8:45 AM, Dinesh Umanath wrote:

Hi all,

I want to do this manipulation. I have an array say @test=[2,4,6,8].
How can i compare a given number against the values which are present in
this array? ie.

In a for loop i will be Iterating through a sequence of number say 1 to
10, and i want to take each of these number 1 to 10 and check if this
number is present in the given array @test, if it is present then i want
to execute some sequence of steps, How can i do this check in ruby ?

Is there a kind of "in" operator in ruby ?

Thank You
Dinesh

--
Posted via http://www.ruby-forum.com/\.

Dinesh Umanath wrote:

Hi all,

I want to do this manipulation. I have an array say @test=[2,4,6,8]. How can i compare a given number against the values which are present in this array? ie.

In a for loop i will be Iterating through a sequence of number say 1 to 10, and i want to take each of these number 1 to 10 and check if this number is present in the given array @test, if it is present then i want to execute some sequence of steps, How can i do this check in ruby ?

Is there a kind of "in" operator in ruby ?

It's backwards from the way you want it.

   array.include?(element)

I invite you to go to rcrchive.org and view RCR 241
and vote for it. :wink:

Cheers,
Hal

@test=[2,4,6,8]

(1..10).each do |n|
if @test.include?(n)
  #execute code
end

···

2006/4/19, Jake McArthur <jake.mcarthur@gmail.com>:

ar = [1, 2, 3, 4, 5, 6]
ar.include? 2 => true

On Apr 19, 2006, at 10:45 AM, Dinesh Umanath wrote:

> Hi all,
>
> I want to do this manipulation. I have an array say @test=[2,4,6,8].
> How can i compare a given number against the values which are
> present in
> this array? ie.
>
> In a for loop i will be Iterating through a sequence of number say
> 1 to
> 10, and i want to take each of these number 1 to 10 and check if this
> number is present in the given array @test, if it is present then i
> want
> to execute some sequence of steps, How can i do this check in ruby ?
>
> Is there a kind of "in" operator in ruby ?
>
>
> Thank You
> Dinesh
>
>
> --
> Posted via http://www.ruby-forum.com/\.
>

To expand:

ar = [1, 2, 3, 4, 5, 6]

1.upto 10 do | i |
  do_something if ar.include? i
end

···

On 4/19/06, Jake McArthur <jake.mcarthur@gmail.com> wrote:

ar = [1, 2, 3, 4, 5, 6]
ar.include? 2 => true

On Apr 19, 2006, at 10:45 AM, Dinesh Umanath wrote:

> Hi all,
>
> I want to do this manipulation. I have an array say @test=[2,4,6,8].
> How can i compare a given number against the values which are
> present in
> this array? ie.
>
> In a for loop i will be Iterating through a sequence of number say
> 1 to
> 10, and i want to take each of these number 1 to 10 and check if this
> number is present in the given array @test, if it is present then i
> want
> to execute some sequence of steps, How can i do this check in ruby ?
>
> Is there a kind of "in" operator in ruby ?
>
>
> Thank You
> Dinesh
>
>
> --
> Posted via http://www.ruby-forum.com/\.
>

silly me, should of course be:

@test=[2,4,6,8]

(1..10).each do |n|
  if @test.include?(n)
    #execute code
  end
end

···

2006/4/19, Dirk Meijer <hawkman.gelooft@gmail.com>:

@test=[2,4,6,8]

(1..10).each do |n|
if @test.include?(n)
  #execute code
end

2006/4/19, Jake McArthur <jake.mcarthur@gmail.com>:

> ar = [1, 2, 3, 4, 5, 6]
> ar.include? 2 => true
>
> On Apr 19, 2006, at 10:45 AM, Dinesh Umanath wrote:
>
> > Hi all,
> >
> > I want to do this manipulation. I have an array say @test=[2,4,6,8].
> > How can i compare a given number against the values which are
> > present in
> > this array? ie.
> >
> > In a for loop i will be Iterating through a sequence of number say
> > 1 to
> > 10, and i want to take each of these number 1 to 10 and check if this
> > number is present in the given array @test, if it is present then i
> > want
> > to execute some sequence of steps, How can i do this check in ruby ?
> >
> > Is there a kind of "in" operator in ruby ?
> >
> >
> > Thank You
> > Dinesh
> >
> >
> > --
> > Posted via http://www.ruby-forum.com/\.
> >
>
>
>

If Array was extended with some set theory, things like this would be possible:

@test.intersect([1..10]).each { |item| puts item }

:slight_smile:

- Jake McArthur

Why all that when:

@test.each do |n|
   # execute code
end

···

On 4/19/06, Dirk Meijer <hawkman.gelooft@gmail.com> wrote:

silly me, should of course be:

@test=[2,4,6,8]

(1..10).each do |n|
  if @test.include?(n)
    #execute code
  end
end

2006/4/19, Dirk Meijer <hawkman.gelooft@gmail.com>:
>
>
> @test=[2,4,6,8]
>
>
> (1..10).each do |n|
> if @test.include?(n)
> #execute code
> end
>
>
> 2006/4/19, Jake McArthur <jake.mcarthur@gmail.com>:
>
> > ar = [1, 2, 3, 4, 5, 6]
> > ar.include? 2 => true
> >
> > On Apr 19, 2006, at 10:45 AM, Dinesh Umanath wrote:
> >
> > > Hi all,
> > >
> > > I want to do this manipulation. I have an array say @test=[2,4,6,8].
> > > How can i compare a given number against the values which are
> > > present in
> > > this array? ie.
> > >
> > > In a for loop i will be Iterating through a sequence of number say
> > > 1 to
> > > 10, and i want to take each of these number 1 to 10 and check if this
> > > number is present in the given array @test, if it is present then i
> > > want
> > > to execute some sequence of steps, How can i do this check in ruby ?
> > >
> > > Is there a kind of "in" operator in ruby ?
> > >
> > >
> > > Thank You
> > > Dinesh
> > >
> > >
> > > --
> > > Posted via http://www.ruby-forum.com/\.
> > >
> >
> >
> >
>
>
>

If Array was extended with some set theory, things like this would be possible:

@test.intersect([1..10]).each { |item| puts item }

x = [*1..5]

=> [1, 2, 3, 4, 5]

y = [*3..7]

=> [3, 4, 5, 6, 7]

x & y

=> [3, 4, 5]

x | y

=> [1, 2, 3, 4, 5, 6, 7]

x - y

=> [1, 2]

y - x

=> [6, 7]

Regards,

Bill

···

From: "Jake McArthur" <jake.mcarthur@gmail.com>

There you go, ruining my moment of self-apparent ingenuity. :stuck_out_tongue:

Anyway....

(@test & [*1..10]).each { |item| puts item }

- Jake

···

On Apr 19, 2006, at 11:49 AM, Bill Kelly wrote:

From: "Jake McArthur" <jake.mcarthur@gmail.com>

If Array was extended with some set theory, things like this would be possible:
@test.intersect([1..10]).each { |item| puts item }

x = [*1..5]

=> [1, 2, 3, 4, 5]

y = [*3..7]

=> [3, 4, 5, 6, 7]

x & y

=> [3, 4, 5]

x | y

=> [1, 2, 3, 4, 5, 6, 7]

x - y

=> [1, 2]

y - x

=> [6, 7]

Regards,

Bill

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

···

On 4/19/06, Dirk Meijer <hawkman.gelooft@gmail.com> 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 <matthew.moss.coder@gmail.com> wrote:

Why all that when:

@test.each do |n|
   # execute code
end