# Handy method Object#in?

**URL:** <https://www.ruby-forum.com/t/handy-method-object-in/120083>\
**Category:** Ruby\
**Created:** [November 13, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/handy-method-object-in/120083 "2007-11-13T16:37:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/94ad74/32.png) [@Kamil](https://www.ruby-forum.com/u/Kamil)\
**Post date:** [November 13, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/handy-method-object-in/120083/1 "2007-11-13T16:37:50Z")

</div>

I wish there would be this simple method in the core:

class Object  
def in?(an\_array)  
an\_array.include?(self)  
end  
end

Having that it’s nice to write:

a = %w(hello world out there)  
puts ‘world’.in?(a)

And it works with Interval instance too.

---

<div class="post-metadata">

**Author:** ![Kamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/94ad74/32.png) [@Kamil](https://www.ruby-forum.com/u/Kamil)\
**Post date:** [November 13, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/handy-method-object-in/120083/2 "2007-11-13T16:47:49Z")

</div>

Hi –

On Wed, 14 Nov 2007, Kamil wrote:

> a = %w(hello world out there)  
> puts ‘world’.in?(a)
> 
> And it works with Interval instance too.

This is a bit of a perma-thread on this mailing list. I guess it’s  
hard to Google for 🙂 But it’s been talked about a lot.

David

---

<div class="post-metadata">

**Author:** ![Kamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/94ad74/32.png) [@Kamil](https://www.ruby-forum.com/u/Kamil)\
**Post date:** [November 13, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/handy-method-object-in/120083/3 "2007-11-13T17:53:05Z")

</div>

On Nov 13, 10:35 am, Kamil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a = %w(hello world out there)  
> puts ‘world’.in?(a)

concise, but it inverts the oop flow. is it really a big deal to do:

a.include?(‘world’)

t.

---

<div class="post-metadata">

**Author:** ![Kamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/94ad74/32.png) [@Kamil](https://www.ruby-forum.com/u/Kamil)\
**Post date:** [November 15, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/handy-method-object-in/120083/4 "2007-11-15T15:54:03Z")

</div>

On Nov 15, 8:16 am, “Pena, Botp” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # \> puts ‘world’.in?(a)
> 
> in english eg, i’d say
> 
> john will swim if he is on the swimming team.
> 
> not
> 
> john will swim if the swimming team includes him (??) [or replace include w other relevant words]
> 
> but yes, that is english, so in ruby we prefer the latter? (just teasing 😉

true enough, computer language tend to force different order though.  
try to imagine it forth 😉

> in fact, i would even like to extend #in? to return the position (if array) or pair (if hash) of obj in collection (nil otherwise); currently include?only returns plain true/false.

not a bad idea.

> kind regards -botp
> 
> ps: heheh, note that i’m also using #in in facets among other things 🙂 why (in ruby) can’t an object ask itself if it’s a member of a collection??

Haha! yep. you got me 😉 but i think its fine for add-on, i mean Ruby  
can’t do everything on it’s own, can it 😉

the reason ruby itself should not, is because it adds a method to all  
objects that simply inverts the actual oop order. techincally we could  
do that with just about every method.

a.index(e)  
e.index\_of(a)

h.key?(k)  
k.key\_of?(h)

etc.

Now this reminds me though, why don’t we have String#puts ?

t.

---

<div class="post-metadata">

**Author:** ![Kamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/94ad74/32.png) [@Kamil](https://www.ruby-forum.com/u/Kamil)\
**Post date:** [November 17, 2007, 3:51am UTC](https://www.ruby-forum.com/t/handy-method-object-in/120083/5 "2007-11-17T03:51:47Z")

</div>

On Nov 13, 2007 3:35 PM, Kamil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a = %w(hello world out there)  
> puts ‘world’.in?(a)
> 
> And it works with Interval instance too.

It is nice, but I would advise you to profile code that uses it and  
ask yourself if it is worth the cost.

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![Kamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/94ad74/32.png) [@Kamil](https://www.ruby-forum.com/u/Kamil)\
**Post date:** [November 15, 2007, 2:18pm UTC](https://www.ruby-forum.com/t/handy-method-object-in/120083/6 "2007-11-15T14:18:16Z")

</div>

From: Trans [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# On Nov 13, 10:35 am, Kamil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

# \> I wish there would be this simple method in the core:

# \> class Object

# \> def in?(an\_array)

# \> an\_array.include?(self)

# \> end

# \> end

# \> Having that it’s nice to write:

# \> a = %w(hello world out there)

# \> puts ‘world’.in?(a)

# concise, but it inverts the oop flow. is it really a big deal to do:

# a.include?(‘world’)

i’m not sure what you mean by oop flow, but i use it like,

obj=“world”  
array = %w(hello world out there)

obj.method if obj in? array

in english eg, i’d say

john will swim if he is on the swimming team.

not

john will swim if the swimming team includes him (??) [ or replace  
include w other relevant words]

but yes, that is english, so in ruby we prefer the latter? (just  
teasing 😉

in fact, i would even like to extend #in? to return the position (if  
array) or pair (if hash) of obj in collection (nil otherwise); currently  
include?only returns plain true/false.

kind regards -botp

ps: heheh, note that i’m also using #in in facets among other things 🙂  
why (in ruby) can’t an object ask itself if it’s a member of a  
collection??
