[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

true.to_i

Leslie Viljoen

10/30/2006 3:00:00 PM

Hello

I am processing database rows and I see that there are "bit" columns
that come out of ActiveRecord as true or false. I wanted binary digits
so I did this:

class FalseClass
def to_i
0
end
end

class TrueClass
def to_i
1
end
end

..which may or may not be really dumb. So is it really dumb?


--
Man's unfailing capacity to believe what he prefers to be true rather
than what the evidence shows to be likely and possible has always
astounded me. We long for a caring Universe which will save us from
our childish mistakes, and in the face of mountains of evidence to the
contrary we will pin all our hopes on the slimmest of doubts. God has
not been proven not to exist, therefore he must exist.

- Prokhor Zakharov

5 Answers

Amos King

10/30/2006 3:17:00 PM

0

I don't see a problem with it. There is no to_i for either class so
you wouldn't be overwriting any functionality there. I would say gtg.

On 10/30/06, Leslie Viljoen <leslieviljoen@gmail.com> wrote:
> Hello
>
> I am processing database rows and I see that there are "bit" columns
> that come out of ActiveRecord as true or false. I wanted binary digits
> so I did this:
>
> class FalseClass
> def to_i
> 0
> end
> end
>
> class TrueClass
> def to_i
> 1
> end
> end
>
> ..which may or may not be really dumb. So is it really dumb?
>
>
> --
> Man's unfailing capacity to believe what he prefers to be true rather
> than what the evidence shows to be likely and possible has always
> astounded me. We long for a caring Universe which will save us from
> our childish mistakes, and in the face of mountains of evidence to the
> contrary we will pin all our hopes on the slimmest of doubts. God has
> not been proven not to exist, therefore he must exist.
>
> - Prokhor Zakharov
>
>


--
Amos King
USPS
Programmer/Analyst
St. Louis, MO

Kalman Noel

10/30/2006 4:25:00 PM

0

Leslie Viljoen:
> I am processing database rows and I see that there are "bit" columns
> that come out of ActiveRecord as true or false. I wanted binary digits
> so I did this:
>
> class FalseClass
> def to_i
> 0
> end
> end
>
> class TrueClass
> def to_i
> 1
> end
> end
>
> .which may or may not be really dumb. So is it really dumb?

It isn't really dumb, but you can get into problems when debugging code
independent of your ActiveRecord. The issue is basically that you place the
solution of a problem of your record into another class, where it does not
belong. I'd suggest something like (untested)

class MyRecord
def my_column_as_int
my_column? ? 1 : 0
end
end

or even (untested as well)

class MyRecord

alias my_column_as_bool my_column
def my_column
my_column_as_bool ? 1 : 0
end

alias my_column_as_bool= my_column=
def my_column=(val)
val = false if val == 0
self.my_column_as_bool = val
end

end

Kalman

Robert Klemme

10/30/2006 4:39:00 PM

0

On 30.10.2006 16:00, Leslie Viljoen wrote:
> Hello
>
> I am processing database rows and I see that there are "bit" columns
> that come out of ActiveRecord as true or false. I wanted binary digits
> so I did this:
>
> class FalseClass
> def to_i
> 0
> end
> end
>
> class TrueClass
> def to_i
> 1
> end
> end
>
> .which may or may not be really dumb. So is it really dumb?

Remember that a lot of other values are considered true so converting
them will either not work (exception) or create all sorts of other weird
effects if you try to generalize this pattern.

I would prefer a custom conversion function

def bit_int(x) x ? 1 : 0 end

Kind regards

robert

pandora

5/25/2007 2:15:00 AM

0

(article not available)

pandora

5/27/2007 2:05:00 AM

0

On Fri, 25 May 2007 01:10:13 -0700, mrsil96 wrote:

> On May 24, 9:15 pm, pandora <pand...@peak.org> wrote:
>> On Wed, 23 May 2007 13:33:28 -0700, mrsil96 wrote:
>> > On May 22, 8:43 pm, pandora <pand...@peak.org> wrote:
>> >> On Fri, 18 May 2007 06:59:59 -0700, mrsil96 wrote:
>> >> >> But hey, if our words are so wrong, why are we so hated on
>> >> >> soc.men???
>>
>> >> > I don't hate you guys one bit.
>>
>> >> I don't hate anyone either. However, the "guys" here are rather
>> >> amusing and some are downright sociopaths. But whatever.
>>
>> >> I think women need to
>> >> take the time to
>>
>> >> > learn about men and what they are really all about.
>>
>> >> Women have no more duty to learn about men than men do about women.
>> >> Individual men and women may wish to learn about a particular
>> >> woman/man in order to have a relationship but that's about it.
>>
>> >> > After all the bible
>> >> says that women need to learn how to be good wives
>> >> > and mothers, because if we handle our role properly the little boys
>> >> > we raise will turn out like gentlemen.
>>
>> >> Balderdash to your *bible* and it's crapola.
>>
>> > So are you an athiest too?
>>
>> Indeed I am. - Hide quoted text -
>
> So why are you an atheist?

Because it seems the logical thing to be given the information at my
disposal. I don't believe in fairy tales and I don't believe in some all
powerful being. Enough said.