[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby's Approach: How To Coerce?

allan.m.miller@gmail.com

4/24/2006 3:18:00 PM

I'm relatively new to Ruby, and trying to understand Ruby's type
coercion mechanism.

More specifically, assuming one is writing a + operator, would it be
more correct "idiomatically" in Ruby to:

(1) Check for a specific type (e.g., using object.class ==) in the
implementation of def +(arg), and raise an exception if an unsupported
arg type is passed (this seems incorrect for Ruby, more suitable for a
statically typed language).

(2) Assume that if the object arg doesn't implement/respond to coerce,
then it is incapable of being "added" (this seems more correct).

(3) In the implementation of coerce in the arg's class, either check
object.class and implement the specific coercion based on the class of
arg, raise an exception otherwise.

I don't see how to get around (3) - if you leave it at (2) all you know
is that the arg can be "coerced" - but the question remains - can it be
coerced in a meaningful way to the class of the receiver object?

Thanks.

3 Answers

Robert Klemme

4/24/2006 3:39:00 PM

0

allan.m.miller@gmail.com wrote:
> I'm relatively new to Ruby, and trying to understand Ruby's type
> coercion mechanism.
>
> More specifically, assuming one is writing a + operator, would it be
> more correct "idiomatically" in Ruby to:
>
> (1) Check for a specific type (e.g., using object.class ==) in the
> implementation of def +(arg), and raise an exception if an unsupported
> arg type is passed (this seems incorrect for Ruby, more suitable for a
> statically typed language).

IMHO you should try to coerce the arg if you cannot add it directly or
add it after some conversion. I'm not 100% sure about the last bit but
coercion should be done.

> (2) Assume that if the object arg doesn't implement/respond to coerce,
> then it is incapable of being "added" (this seems more correct).
>
> (3) In the implementation of coerce in the arg's class, either check
> object.class and implement the specific coercion based on the class of
> arg, raise an exception otherwise.

You should definitively do that.

> I don't see how to get around (3) - if you leave it at (2) all you know
> is that the arg can be "coerced" - but the question remains - can it be
> coerced in a meaningful way to the class of the receiver object?
>
> Thanks.

Does this help:

http://www.rubygarden.org/ruby?CoerceE...

Kind regards

robert

samovar

9/29/2010 3:47:00 AM

0

On 28 sep, 20:59, yp <y...@hotmail.com> wrote:
> On Sep 28, 7:08 pm, samovar <hbaud...@videotron.ca> wrote:
>
>
>
> > On 28 sep, 10:16, yp <y...@hotmail.com> wrote:
>
> > > Parlons-en d'une façon logique en utilisant le syllogisme suivant:
>
> > > Les Québécois sont des Latins (en majorité).
> > > Les Latins sont plus corrompus que les Anglo-Saxons (voir par exemple
> > > l'Amérique latine).
> > > Donc les Québécois sont les plus corrompus au Canada, car dans le
> > > reste du Canada la majorité des gens ne sont pas des Latins).
>
> > > Voila, tout est expliqué.
>
> > J'aime votre humour... peut-être involontaire.
> > Etudions la corruption aux US, et on reparle après ?
>
> > RV
>
> La "corruption" aux Etats-Unis est légale et transparente. La Cour
> Suprême a récemment déclaré que les compagnies peuvent financer les
> partis politiques sans limite. Aussi les candidats peuvent dépenser
> sans limite leur propre argent. C'est leur système.

Oh ! S'il n'y avait que cela...

RV

pierredesrochers46@yahoo.ca

9/29/2010 11:14:00 AM

0

On 28 sep, 20:59, yp <y...@hotmail.com> wrote:
> On Sep 28, 7:08 pm, samovar <hbaud...@videotron.ca> wrote:
>
>
>
>
>
> > On 28 sep, 10:16, yp <y...@hotmail.com> wrote:
>
> > > Parlons-en d'une façon logique en utilisant le syllogisme suivant:
>
> > > Les Québécois sont des Latins (en majorité).
> > > Les Latins sont plus corrompus que les Anglo-Saxons (voir par exemple
> > > l'Amérique latine).
> > > Donc les Québécois sont les plus corrompus au Canada, car dans le
> > > reste du Canada la majorité des gens ne sont pas des Latins).
>
> > > Voila, tout est expliqué.
>
> > J'aime votre humour... peut-être involontaire.
> > Etudions la corruption aux US, et on reparle après ?
>
> > RV
>
> La "corruption" aux Etats-Unis est légale et transparente. La Cour
> Suprême a récemment déclaré que les compagnies peuvent financer les
> partis politiques sans limite. Aussi les candidats peuvent dépenser
> sans limite leur propre argent. C'est leur système.

Il y a une distinction à faire entre "la corruption" et le financement
des partis politiques.
Il peut y avoir, cependant, une relation.