Forum: Ruby on Rails How to deal with NULL value

3275da7fdbd73cb4e7956fd0d29164de?d=identicon&s=25 Paul Bergstrom (palb)
on 2008-08-19 08:22
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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-08-19 10:23
(Received via mailing list)
On Aug 19, 7:22 am, Pål Bergström <rails-mailing-l...@andreas-s.net>
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
3275da7fdbd73cb4e7956fd0d29164de?d=identicon&s=25 Paul Bergstrom (palb)
on 2008-08-19 10:30
Frederick Cheung wrote:
> On Aug 19, 7:22�am, P�l Bergstr�m <rails-mailing-l...@andreas-s.net>
> 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. :-) )

How do I define the method on nil to be a no-op (and what do you mean
with that)?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-08-19 11:13
(Received via mailing list)
On 19 Aug 2008, at 09:30, Pål Bergström wrote:

>>> for
> ( I realize now that this is more of a Ruby question. :-) )
>
> 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
3275da7fdbd73cb4e7956fd0d29164de?d=identicon&s=25 Paul Bergstrom (palb)
on 2008-08-19 11:19
Frederick Cheung 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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-08-19 11:46
(Received via mailing list)
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
29ebf90af6107d2eb39b587c7972639c?d=identicon&s=25 Mukund (Guest)
on 2008-08-19 12:21
(Received via mailing list)
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 <rails-mailing-l...@andreas-s.net>
3275da7fdbd73cb4e7956fd0d29164de?d=identicon&s=25 Paul Bergstrom (palb)
on 2008-08-19 12:42
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 <rails-mailing-l...@andreas-s.net>

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

column.length unless column.nil?
29ebf90af6107d2eb39b587c7972639c?d=identicon&s=25 Mukund (Guest)
on 2008-08-19 13:04
(Received via mailing list)
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 <rails-mailing-l...@andreas-s.net>
29ebf90af6107d2eb39b587c7972639c?d=identicon&s=25 Mukund (Guest)
on 2008-08-19 13:07
(Received via mailing list)
Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.
3275da7fdbd73cb4e7956fd0d29164de?d=identicon&s=25 Paul Bergstrom (palb)
on 2008-08-19 13:21
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?
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2008-08-19 14:34
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.
B890455aec24c271759fb48049473a9f?d=identicon&s=25 Jorg Lueke (Guest)
on 2008-08-19 15:06
(Received via mailing list)
On Aug 19, 1:22 am, Pål Bergström <rails-mailing-l...@andreas-s.net>
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
E3ba60e3dcb813f8abcd7732350e74cf?d=identicon&s=25 Phillip Koebbe (pkoebbe)
on 2008-08-19 15:11
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
3275da7fdbd73cb4e7956fd0d29164de?d=identicon&s=25 Paul Bergstrom (palb)
on 2008-08-19 15:17
Jorg Lueke 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.
B890455aec24c271759fb48049473a9f?d=identicon&s=25 Jorg Lueke (Guest)
on 2008-08-19 15:51
(Received via mailing list)
Though it should be if string.nil? length = 0
string.length was your problem :-)

On Aug 19, 8:17 am, Pål Bergström <rails-mailing-l...@andreas-s.net>
29ebf90af6107d2eb39b587c7972639c?d=identicon&s=25 Mukund (Guest)
on 2008-08-20 11:10
(Received via mailing list)
I was referring to nil in Ruby and NULL in MySql. :)

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 Koebbe <rails-mailing-l...@andreas-s.net>
E3ba60e3dcb813f8abcd7732350e74cf?d=identicon&s=25 Phillip Koebbe (pkoebbe)
on 2008-08-20 12:56
Mukund wrote:
> I was referring to nil in Ruby and NULL in MySql. :)
>
> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.