# A method that checks if an object is in a range

**URL:** <https://www.ruby-forum.com/t/a-method-that-checks-if-an-object-is-in-a-range/131720>\
**Category:** Ruby\
**Created:** [March 12, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/a-method-that-checks-if-an-object-is-in-a-range/131720 "2008-03-12T19:01:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 12, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/a-method-that-checks-if-an-object-is-in-a-range/131720/1 "2008-03-12T19:01:08Z")

</div>

Hi,

I wonder if there is a built-in method which does just like:

range.include?(obj)

but for the obj object. I came up with something like:

module Comparable  
def in?(range)  
range.in?(self)  
end  
end

1.in? (1…10) # =\> true

Of course I can use the range.include? syntax, but I find the second  
approach more convenient 🙂

Regards,  
m4chu

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 12, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/a-method-that-checks-if-an-object-is-in-a-range/131720/2 "2008-03-12T19:57:51Z")

</div>

On Mar 12, 2008, at 6:59 PM, removed\_email\_address@domain.invalid wrote:

> ```
> range.in?(self)
> 
> ```

Hi m4chu,

First, to answer your question: no, there is no such method in Ruby.

This was an example we discussed that in “Monkeypatching is destroying  
Ruby”, which i wanted to comment on. (… and went into a rather long  
holiday before i could) But i don’t want to bring the old up thread,  
so i’ll discuss this here.

I consider this bad style from a design perspective (although it is a  
nice detail that you don’t extend Object but Comparable). It reads  
well, but if you think through the system you are creating, it is not  
that nice.

First, your implementation has some issues. If you are striving for a  
natural use of you primary language (in that domain, english), be  
aware that #in? can also be used for Arrays and not just ranges. So,  
the naming of the parameter is a bit amiss. This does not matter that  
much as all Enumerables respond to #include? and can be considered  
nitpicking. It can also be argued that _every_ Object can be _in_ a  
collection. Why only Comparables?  
This is a danger of every implementation that tries to make the  
language more casual.

Second, i see another problem: It is pretty clear that the  
Implementation of inclusion has to be implemented in the collection.  
So, to keep things clear (from an object-orientated point of view),  
the implementing class should be called. Your style adds another call  
just for being nice (because it is just a proxy method).

I also believe it to be unnatural.  
If I try to visualize what my program is doing, I always see my  
objects as having a conversation that i moderate. What would you ask  
your group of objects?

1. “Hey group, does Florian belong to you?” #=\> “no, no, never”

or

1. “Hey Flo, would you please ask the group whether you belong to it?”

# thats your style

If you think in terms of encapsulation, a single object does not have  
to care whether it is referenced in some collection (because another  
entity [the collection] can already handle the problem).

I don’t think this should ever be standard. If you want to use this -  
go ahead and do it. But i would think twice about it’s implications  
before i would code that uses such patterns (it may be worth it, it  
may not be worth it). It doesn’t destroy the world, but it can create  
frustrations and enforces a style that is not ‘standard’.

Greetings  
Florian G.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 12, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/a-method-that-checks-if-an-object-is-in-a-range/131720/3 "2008-03-12T20:00:48Z")

</div>

On Wed, Mar 12, 2008 at 12:59 PM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> range.in?(self)
> end
> 
> ```
> 
> end
> 
> 1.in? (1…10) # =\> true
> 
> Of course I can use the range.include? syntax, but I find the second  
> approach more convenient 🙂
> 
> Regards,  
> m4chu

I personally am not a fan of such methods, but perhaps  
Comparable#between will satisfy you.

Daniel Brumbaugh K.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 12, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/a-method-that-checks-if-an-object-is-in-a-range/131720/4 "2008-03-12T22:01:55Z")

</div>

On Mar 12, 2008, at 9:20 PM, removed\_email\_address@domain.invalid wrote:

> Florian,
> 
> Yeah, I know about monkey patching thing, and I wanted to avoid it.

I don’t consider this monkey patching. You are extending - there is  
nothing  
wrong with that. I just wanted to give a reference to the topic where  
this  
example was used.

Monkey patching would be to change behaviour of methods to fit your  
needs  
(or to fix bugs that are not yet fixed).

> sounds reasonable too 🙂  
> That is absolutely true. A comparison is defined in both ways, so  
> there are many ways to express it. So extending Comparable was the  
> right way to start.

If you really think you are lacking a way to express yourself, help  
yourself to it.  
Reformulating sentences doesn’t help, though ;).

Greetings  
Florian G.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 12, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/a-method-that-checks-if-an-object-is-in-a-range/131720/5 "2008-03-12T21:21:06Z")

</div>

Florian,

Yeah, I know about monkey patching thing, and I wanted to avoid it.

Well, I certainly have chosen somewhat wrong name for the method; all  
I wanted to do is to check whether object is in range. Within or,  
Between are better ones (I knew about that one Daniel, it’s just I  
prefer ranges over min/max :).

I couldn’t agree more with your approach to object/group; but  
conversations like  
“is 13 between 0 and 59”  
“is that date between 1.week.ago and 1.week.from\_now”  
sounds reasonable too 🙂

I think I’ll add support for ranges to #between? anyway 🙂

Thanks for replies!
