How I can check if an object is null in ruby on rails 2?

George Panayi picture George Panayi · Apr 29, 2012 · Viewed 135k times · Source

I want to check when my object @objectname is not equal to null to show the values of the @objectname else to show that no values found.

I tried this:

<% if (@objectname != null) then %>

but I'm getting an error.

Answer

Pavel Veller picture Pavel Veller · Apr 29, 2012

it's nilin Ruby, not null. And it's enough to say if @objectname to test whether it's not nil. And no then. You can find more on if syntax here:

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures#if