# Using "if 3 in list\_foo" for Python for Ruby

**URL:** <https://www.ruby-forum.com/t/using-if-3-in-list-foo-for-python-for-ruby/115291>\
**Category:** Ruby\
**Created:** [September 23, 2007, 8:38am UTC](https://www.ruby-forum.com/t/using-if-3-in-list-foo-for-python-for-ruby/115291 "2007-09-23T08:38:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 23, 2007, 8:38am UTC](https://www.ruby-forum.com/t/using-if-3-in-list-foo-for-python-for-ruby/115291/1 "2007-09-23T08:38:59Z")

</div>

There is no such thing as

if 3 in [3, 2, 1]

construct in Ruby? so the common way in Ruby is

if list1.include?(3)

or

if list1.index(3)

is that right? is there an idiom in Ruby to do something like

if 3.in([3,2,1])

or

if 3.in [3,2,1]

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 23, 2007, 8:55am UTC](https://www.ruby-forum.com/t/using-if-3-in-list-foo-for-python-for-ruby/115291/2 "2007-09-23T08:55:50Z")

</div>

Hi –

On Sun, 23 Sep 2007, SpringFlowers AutumnMoon wrote:

> if list1.index(3)
> 
> is that right? is there an idiom in Ruby to do something like
> 
> if 3.in([3,2,1])
> 
> or
> 
> if 3.in [3,2,1]

The include? way is the usual way. Hal F. at one point proposed an  
in or in? method that would essentially be:

class Object  
def in?(collection)  
collection.include?(self)  
end  
end

but I don’t think it’s on the radar for being added.

David

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 23, 2007, 9:48am UTC](https://www.ruby-forum.com/t/using-if-3-in-list-foo-for-python-for-ruby/115291/3 "2007-09-23T09:48:17Z")

</div>

David A. Black wrote:

> On Sun, 23 Sep 2007, SpringFlowers AutumnMoon wrote:

> > if 3.in [3,2,1]
> 
> The include? way is the usual way. Hal F. at one point proposed an  
> in or in? method that would essentially be:
> 
> class Object  
> def in?(collection)  
> collection.include?(self)  
> end  
> end
> 
> but I don’t think it’s on the radar for being added.

Hm… wouldn’t that be handy…

i just sort of think that, if you ask me, “have you been to [Hawaii,  
China, France]”, i probably should be able to look at each one and tell  
you an answer of yes or no, instead of asking [Hawaii, China, France] to  
tell whether they have seen me.
