BACKTRACE

(condense)

Back to Posts List

GET

The Boolean connection

Few people understand the true mentality of dealing with a Boolean variable.
The normal programmer will panic, and immediately attempt to compare it to a Boolean constant.

when using Rails, This is wrong.

When migrating a boolean typed column using Rails, the actual column type created is “tinyint(1)” (a single digit number presentation which means that the actual value being saved is 0’s or 1’s.
Where is the problem? in Ruby for a change.
In Ruby, everything except nil and false is considered to a true value. In C, Python and many other languages, 0 and possibly other values, such as empty lists, are considered as false.

which brings us to the inevitable conclusion, that simplay comparing boolean values to true or false will not do.

Rails solves this tiny issue for us, for each boolean attribute, Rails implements a “query method” as i prefer to call them. These query methods are simply called by issuing the boolean attribute name with a “?” suffix.

for example, the mail_message</span> instance object has a <span style="font-weight: bold;">is_important</span> boolean attribute, so instead of doing:<br /><br /><code><br />mail_message.is_important == true

or whatever, we can simply ask rails by:
<br />@mail_message.is_important?<br />

beautiful… isn’t it?

POST

blog comments powered by Disqus

I Don't have cookies.

ELAD ENV

Variable Value
LINKEDIN
TWITTER
FACEBOOK
GITHUB
WWR
IRC
{ 'irc.freenode.net' => [ '#rubyonrails', '#railsbridge', '#ruby', '#mootools' ]}
SKYPE
eladmeidar

You're seeing this error because I think it is funny.