Florian Gilcher
3/12/2008 6:54:00 PM
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