How to deal with NULL value

I often run into a problem of getting error because on the NULL value in
MySQL. One example is when asking for .length of a string or strftime
for datetime. Would it be better not to use NULL as default value, for
example strftime? What’s the actual benefit of NULL?

On Aug 19, 7:22 am, Pål Bergström [email protected]
wrote:

I often run into a problem of getting error because on the NULL value in
MySQL. One example is when asking for .length of a string or strftime
for datetime. Would it be better not to use NULL as default value, for
example strftime? What’s the actual benefit of NULL?

Well the beauty of ruby is that you could just define the method on
nil to be a no-op (or even implement method_missing on nil).
Personally I think that’s a terrible idea as it could so easily cover
up mistakes: ruby has let you know that you’re trying to do something
nonsensical, ignore it at you peril

Fred

Frederick C. wrote:

On Aug 19, 7:22�am, P�l Bergstr�m [email protected]
wrote:

I often run into a problem of getting error because on the NULL value in
MySQL. One example is when asking for .length of a string or strftime
for datetime. Would it be better not to use NULL as default value, for
example strftime? What’s the actual benefit of NULL?

Well the beauty of ruby is that you could just define the method on
nil to be a no-op (or even implement method_missing on nil).
Personally I think that’s a terrible idea as it could so easily cover
up mistakes: ruby has let you know that you’re trying to do something
nonsensical, ignore it at you peril

Fred

( I realize now that this is more of a Ruby question. :slight_smile: )

How do I define the method on nil to be a no-op (and what do you mean
with that)?

On 19 Aug 2008, at 09:30, Pål Bergström wrote:

for
( I realize now that this is more of a Ruby question. :slight_smile: )

How do I define the method on nil to be a no-op (and what do you mean
with that)?
by no-op i mean ‘does nothing’

class NilClass
def foo
nil
end
end

nil.foo #=> nil

class NilClass
def method_missing(*args)
nil
end
end

nil.bananarama #=> nil

Of course this does mean that if you wrote @customer instead of
@current_customer or @logged_in_customer it would just fail silently
instead of raising an error.

Fred

Frederick C. wrote:

class NilClass
def foo
nil
end
end

nil.foo #=> nil

class NilClass
def method_missing(*args)
nil
end
end

nil.bananarama #=> nil

Thanks.

Probably a stupid question, but where do I add this in RoR? In the
application_controller or?

On 19 Aug 2008, at 10:19, Pål Bergström wrote:

Probably a stupid question, but where do I add this in RoR? In the
application_controller or?

Well it could go anywhere as long as that somewhere is loaded by your
app (and it needs to be at the top level of a file). I’d stick it in a
file in lib and require it from an initializer (or rather I wouldn’t
do that at all because I think it’s a terrible idea).

Fred

You can always check to see if the column returned a NULL value in
Ruby using column.nil? or you can rescue the exception.

Example:

x=some_function_that_fails_on_null_parameter(NULL_value_column) rescue
value_in_case_of_exception

It is safer to add exception handlers on a case-by-case basis than to
generalize.

On Aug 19, 11:22 am, Pål Bergström [email protected]

Mukund wrote:

You can always check to see if the column returned a NULL value in
Ruby using column.nil? or you can rescue the exception.

Example:

x=some_function_that_fails_on_null_parameter(NULL_value_column) rescue
value_in_case_of_exception

It is safer to add exception handlers on a case-by-case basis than to
generalize.

On Aug 19, 11:22�am, P�l Bergstr�m [email protected]

I think I understand how to use column.nil?, but rescue is new to me. Is
this correct?

column.length unless column.nil?

x = column.length unless column.nil?
x = column.length rescue nil

will both give you the same results. Alternatively,

x = (column.nil?)? 0: column.length;
x = column.length rescue 0

will give you zeroes instead of nil.

On Aug 19, 3:42 pm, Pål Bergström [email protected]

Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.

Mukund wrote:

Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.

So this will not work?

x = column.length unless column.nil?

Pål Bergström wrote:

Mukund wrote:

Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.

So this will not work?

x = column.length unless column.nil?

I think the relevant question is what are you going to do with x?

Your code statement is valid, but think about what x is when column is
nil, and how are you planning to use x?

The merits of setting x to zero when column.nil? as suggested by Mukund
depends on what you’re going to do with x.

On Aug 19, 1:22 am, Pål Bergström [email protected]
wrote:

I often run into a problem of getting error because on the NULL value in
MySQL. One example is when asking for .length of a string or strftime
for datetime. Would it be better not to use NULL as default value, for
example strftime? What’s the actual benefit of NULL?

The benefit of a Null is that it doesn’t figure in counts, maxes,
mins, and other operations. It really means “I don’t know” to the
database and behaves accordingly. You can certainly default to other
values like spaces or 0. Just know that the behavior is somewhat
different.

You can always handle null, if string.length.nil? length = 0 if
that’s what you want

Mukund wrote:

Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.

I’m not sure you are saying what you intend to say. In Ruby, nil
evaluates to false. In fact, only nil and false evaluate to false. Since
Ruby uses nil instead of null, I assume you meant nil.

These two are functional equivalent:

do_something if my_var

do_something unless my_var.nil?

Peace,
Phillip

Jorg L. wrote:

You can always handle null, if string.length.nil? length = 0 if
that’s what you want

That’s what I’m after. Thanks.

Though it should be if string.nil? length = 0
string.length was your problem :slight_smile:

On Aug 19, 8:17 am, Pål Bergström [email protected]

I was referring to nil in Ruby and NULL in MySql. :slight_smile:

nil as an object doesn’t equate to false in Ruby.

irb(main):001:0> x=nil
=> nil
irb(main):002:0> x==false
=> false
irb(main):003:0> x.nil?
=> true

You need to watch out for that while handling variables that can
contain nil values in comparison statements. The my_var.nil? function
takes the nil object and gives you a boolean result.

On Aug 19, 6:11 pm, Phillip K. [email protected]

Mukund wrote:

I was referring to nil in Ruby and NULL in MySql. :slight_smile:

nil as an object doesn’t equate to false in Ruby.

Ah, we’re using different words. What you say is true. A nil object and
a false object do not equate. But what I said is true as well. A nil
object and a false object evaluate to false in a boolean expression.
Maybe not in a direct comparison as in your example, but definitely in
the example I posted above.

You need to watch out for that while handling variables that can
contain nil values in comparison statements. The my_var.nil? function
takes the nil object and gives you a boolean result.

Agreed. When using variables in direct comparisons, definitely be
careful of nil. But when testing a variable for whether it is or is not
nil/false, just a simple

if my_var

will suffice.

Peace,
Phillip