PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > a method that checks if an object is in a range
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
a method that checks if an object is in a range

Réponse
 
LinkBack Outils de la discussion
Vieux 12/03/2008, 18h57   #1
wojtek@m4chu.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut a method that checks if an object is in a range

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
  Réponse avec citation
Vieux 12/03/2008, 19h54   #2
Florian Gilcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a method that checks if an object is in a range



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

  Réponse avec citation
Vieux 12/03/2008, 19h59   #3
Daniel Brumbaugh Keeney
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a method that checks if an object is in a range

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

  Réponse avec citation
Vieux 12/03/2008, 21h17   #4
wojtek@m4chu.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a method that checks if an object is in a range

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!
  Réponse avec citation
Vieux 12/03/2008, 22h00   #5
Florian Gilcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a method that checks if an object is in a range


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,
yourself to it.
Reformulating sentences doesn't , though .

Greetings
Florian Gilcher

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h52.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13133 seconds with 13 queries