[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

can you enable coercion between types?

ljw1001@gmail.com

3/18/2006 7:48:00 PM

I have a ruby class called Duration that represents a unit of time.
It's main attribute is a number that represents the duration in
seconds.

If I try to save it to a DB using Rails, I get a "Duration can not be
coerced into Float" error. Is there a way to modify the definition of
Duration to enable automatic coercion into a float? (I'm thinking of
something like adding a "to_float" method to the class.)

thanks in advance
larry

16 Answers

Trans

3/18/2006 8:23:00 PM

0

Did you try #to_f?

T.

Ara.T.Howard

3/18/2006 11:12:00 PM

0

Trans

3/19/2006 1:15:00 AM

0


ara.t.howard@noaa.gov wrote:
> try this:
>
> harp:~ > cat a.rb
> class Duration
> def initialize seconds
> @seconds = seconds
> end
> def coerce other
> if other.class == @seconds.class
> [@seconds, other]
> else
> [Float(@seconds), Float(other)]
> end
> end
> end

Could you explain what this is doing? I don't get the effect.

Thanks,
T.

Markus

3/19/2006 5:20:00 AM

0

On Sat, 2006-03-18 at 17:11, ara.t.howard@noaa.gov wrote:

> try this:
>
> harp:~ > cat a.rb
> class Duration
> def initialize seconds
> @seconds = seconds
> end
> def coerce other
> if other.class == @seconds.class
> [@seconds, other]
> else
> [Float(@seconds), Float(other)]
> end
> end

There's a gottcha here. Coerce is used to implement double dispatch and
so needs to return the coerced values in the opposite order (see Pick
Axe)--(other,self), not (self,other).

The way you wrote it works fine for addition & multiplication, but
you'll pull your hair out the first time you try to subtract or divide.

--MarkusQ

P.S. And I think (as Tom noted) that "to_f" was the answer to the
original question.





Ara.T.Howard

3/19/2006 3:20:00 PM

0

sugarless anko

5/19/2010 9:41:00 AM

0

On 5?19?, ??2:20, rst0wxyz <rst0w...@yahoo.com> wrote:
> On May 18, 9:56 pm, Neo <residualselfimage1...@gmail.com> wrote:
>
>
>
>
>
> > On May 17, 10:14 pm, RichAsianKid <RichAsian...@hotmail.com> wrote:
>
> > > Forget the first few minutes (though not unimportant in America's
> > > eventual prognosis), listen to the quote from 3'16" at the end from
> > > Alexis de Tocqueville way back from 1835:
>
> > >      http://www.youtube.com/watch?v=p...
>
> > > Same quote from wiki:
>
> > > "Not only does democracy make every man forget his ancestors, but also
> > > clouds their view of their descendants and isolates them from their
> > > contemporaries. Each man is forever thrown back on himself alone, and
> > > there is danger that he may be shut up in the solitude of his own heart."
>
> > > We're not equal in life.
>
> > > But we're equal in death
>
> > While each of us is different
> >    and therefore not equal in this life
> > but Democracy asserts that each one of us
> >      is equally *important* in this life.
>
> > which reminds me of  the following ....
>
> > No man is an Island, entire of itself; every man is a piece of the
> > Continent, a part of the main; if a clod be washed away by the sea,
> > Europe is the less, as well as if a promontory were, as well as if a
> > manor of thy friends or of thine own were; any man's death diminishes
> > me, because I am involved in Mankind; And therefore never send to know
> > for whom the bell tolls; It tolls for thee.
>
> > John Donne, Meditation XVII
> > English clergyman & poet (1572 - 1631)
>
> > A civilization is not based on how well its people treats those at the
> > very top
> > but how well its people treats those at the very bottom.
>
> Ever since civilization begins, the people at the very bottom were
> treated like filthy dirt, like animals, like any beast of burden, and
> continue to be treated the same way.  Powerful nations continue to
> bully weak and smaller nations,...  This is the way of life for all

It's only applied to your brutal and retarded slavers world of Anglo-
Saxon savages.
NOT to any cilvilized world.

Fucking Komin the Pedophile Turd

5/19/2010 10:31:00 AM

0

On May 19, 5:40 pm, sugarless anko <serbet...@gmail.com> wrote:
> On 5?19?, ??2:20, rst0wxyz <rst0w...@yahoo.com> wrote:
>
>
>
>
>
> > On May 18, 9:56 pm, Neo <residualselfimage1...@gmail.com> wrote:
>
> > > On May 17, 10:14 pm, RichAsianKid <RichAsian...@hotmail.com> wrote:
>
> > > > Forget the first few minutes (though not unimportant in America's
> > > > eventual prognosis), listen to the quote from 3'16" at the end from
> > > > Alexis de Tocqueville way back from 1835:
>
> > > >      http://www.youtube.com/watch?v=p...
>
> > > > Same quote from wiki:
>
> > > > "Not only does democracy make every man forget his ancestors, but also
> > > > clouds their view of their descendants and isolates them from their
> > > > contemporaries. Each man is forever thrown back on himself alone, and
> > > > there is danger that he may be shut up in the solitude of his own heart."
>
> > > > We're not equal in life.
>
> > > > But we're equal in death
>
> > > While each of us is different
> > >    and therefore not equal in this life
> > > but Democracy asserts that each one of us
> > >      is equally *important* in this life.
>
> > > which reminds me of  the following ....
>
> > > No man is an Island, entire of itself; every man is a piece of the
> > > Continent, a part of the main; if a clod be washed away by the sea,
> > > Europe is the less, as well as if a promontory were, as well as if a
> > > manor of thy friends or of thine own were; any man's death diminishes
> > > me, because I am involved in Mankind; And therefore never send to know
> > > for whom the bell tolls; It tolls for thee.
>
> > > John Donne, Meditation XVII
> > > English clergyman & poet (1572 - 1631)
>
> > > A civilization is not based on how well its people treats those at the
> > > very top
> > > but how well its people treats those at the very bottom.
>
> > Ever since civilization begins, the people at the very bottom were
> > treated like filthy dirt, like animals, like any beast of burden, and
> > continue to be treated the same way.  Powerful nations continue to
> > bully weak and smaller nations,...  This is the way of life for all
>
> It's only applied to your brutal and retarded slavers world of Anglo-
> Saxon savages.
> NOT to any cilvilized world.

Baka-san you Chikan-shit.

What do you know about civilisation mate - you're a Japanese savage
yourself.

RichAsianKid

5/20/2010 1:31:00 AM

0

On 19/05/2010 12:56 AM, Neo wrote:
> On May 17, 10:14 pm, RichAsianKid<RichAsian...@hotmail.com> wrote:
>> Forget the first few minutes (though not unimportant in America's
>> eventual prognosis), listen to the quote from 3'16" at the end from
>> Alexis de Tocqueville way back from 1835:
>>
>> http://www.youtube.com/watch?v=p...
>>
>> Same quote from wiki:
>>
>> "Not only does democracy make every man forget his ancestors, but also
>> clouds their view of their descendants and isolates them from their
>> contemporaries. Each man is forever thrown back on himself alone, and
>> there is danger that he may be shut up in the solitude of his own heart."
>>
>> We're not equal in life.
>>
>> But we're equal in death
>
>
> While each of us is different
> and therefore not equal in this life
> but Democracy asserts that each one of us
> is equally *important* in this life.
>
> which reminds me of the following ....
>
> No man is an Island, entire of itself; every man is a piece of the
> Continent, a part of the main; if a clod be washed away by the sea,
> Europe is the less, as well as if a promontory were, as well as if a
> manor of thy friends or of thine own were; any man's death diminishes
> me, because I am involved in Mankind; And therefore never send to know
> for whom the bell tolls; It tolls for thee.
>
> John Donne, Meditation XVII
> English clergyman& poet (1572 - 1631)
>
>
> A civilization is not based on how well its people treats those at the
> very top
> but how well its people treats those at the very bottom.
>
>
> Civilization is not measured by the degree which it hold on to each
> human life but
> by the degree by which each of person is allowed to hold on to our own
> humanity.
>
>

Well, as Alex Pope also once said, "order is heaven's first great law,
some are and must be greater than the rest." Or as the French anarchist
Proudhon also understood, "if all the world is my brother, then I have
no brother."

But the real folly is you failed to recognize Hamiltonian principle:
that the individual is little more than mere carrier of our ancestor's
genes; that helping out a genetic and by extension racial and ethnic kin
is not identical as promiscuously helping out someone more distant. That
is, so-called 'prejudice', 'ethnocentricism', 'nationalism', 'racism'
are actually very *moral* and *justifiable* phenomena: charity begins at
home.

And btw it's precisely of their being hysterically denounced as being
some modern moral failing and mortal sin that we understand their raw,
fundamental power.

Once again: the principle of biological concentricity. I love my own
family first, and all factors equal I will make sacrifices for them that
I will not make for those who are less kin than mine. Period.

The whole framework based on the value of some atomized 'individual' is
precisely what Alexis de Tocqueville in the OP warned us about. And in
Darwinian terms, nature 'cares' not about the shell not carrier; life is
about genetic continuity, about ancestry and descendants, about that
timeless and time-honored vertical dimension of genetic propagation,
about downstream outcome - and NOT some snapshot of some cross-section
of some standstill of time.


rst0wxyz

5/23/2010 3:35:00 AM

0

On May 19, 2:40 am, sugarless anko <serbet...@gmail.com> wrote:
> On 5?19?, ??2:20, rst0wxyz <rst0w...@yahoo.com> wrote:
>
>
>
>
>
> > On May 18, 9:56 pm, Neo <residualselfimage1...@gmail.com> wrote:
>
> > > On May 17, 10:14 pm, RichAsianKid <RichAsian...@hotmail.com> wrote:
>
> > > > Forget the first few minutes (though not unimportant in America's
> > > > eventual prognosis), listen to the quote from 3'16" at the end from
> > > > Alexis de Tocqueville way back from 1835:
>
> > > >      http://www.youtube.com/watch?v=p...
>
> > > > Same quote from wiki:
>
> > > > "Not only does democracy make every man forget his ancestors, but also
> > > > clouds their view of their descendants and isolates them from their
> > > > contemporaries. Each man is forever thrown back on himself alone, and
> > > > there is danger that he may be shut up in the solitude of his own heart."
>
> > > > We're not equal in life.
>
> > > > But we're equal in death
>
> > > While each of us is different
> > >    and therefore not equal in this life
> > > but Democracy asserts that each one of us
> > >      is equally *important* in this life.
>
> > > which reminds me of  the following ....
>
> > > No man is an Island, entire of itself; every man is a piece of the
> > > Continent, a part of the main; if a clod be washed away by the sea,
> > > Europe is the less, as well as if a promontory were, as well as if a
> > > manor of thy friends or of thine own were; any man's death diminishes
> > > me, because I am involved in Mankind; And therefore never send to know
> > > for whom the bell tolls; It tolls for thee.
>
> > > John Donne, Meditation XVII
> > > English clergyman & poet (1572 - 1631)
>
> > > A civilization is not based on how well its people treats those at the
> > > very top
> > > but how well its people treats those at the very bottom.
>
> > Ever since civilization begins, the people at the very bottom were
> > treated like filthy dirt, like animals, like any beast of burden, and
> > continue to be treated the same way.  Powerful nations continue to
> > bully weak and smaller nations,...  This is the way of life for all
>
> It's only applied to your brutal and retarded slavers world of Anglo-
> Saxon savages.

Asians were the worst. Beheading was the common practice, even for
the smallest dislike of the master.

> NOT to any cilvilized world.

When it comes to killing, the Japanese were the worst, then the
Chinese.

baldeagle

5/23/2010 3:42:00 AM

0

On May 23, 11:35 am, rst0wxyz <rst0w...@yahoo.com> wrote:

>
> Asians were the worst.  Beheading was the common practice, even for
> the smallest dislike of the master.
>
> When it comes to killing, the Japanese were the worst, then the
> Chinese.

Bullshit.

How many persons..have the Japanese beheaded ?

The USA killed hundreds of thousands when it nuked
Japan...the US killed almost a millions when it invaded Iraq.