[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

a method that checks if an object is in a range

wojtek

3/12/2008 5:57:00 PM

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
4 Answers

Florian Gilcher

3/12/2008 6:54:00 PM

0



On Mar 12, 2008, at 6:59 PM, wojtek@m4chu.net wrote:

> 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
>


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

2) "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 Gilcher

Daniel Brumbaugh Keeney

3/12/2008 7:00:00 PM

0

On Wed, Mar 12, 2008 at 12:59 PM, <wojtek@m4chu.net> wrote:
> 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

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

Daniel Brumbaugh Keeney

wojtek

3/12/2008 8:17:00 PM

0

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!

Florian Gilcher

3/12/2008 9:01:00 PM

0


On Mar 12, 2008, at 9:20 PM, wojtek@m4chu.net 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).

>
> 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 :)

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 Gilcher