61

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
George Panayi
  • 1,768
  • 6
  • 26
  • 42

5 Answers5

76

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

Pavel Veller
  • 6,085
  • 1
  • 26
  • 24
  • 2
    It would also be advisable to play with `rails console` to try out statements on the fly – Jonathan Apr 29 '12 at 11:28
  • im having this one if @reviews.nil? then and always is true even there is a review or not – George Panayi Apr 29 '12 at 11:34
  • 1
    there should be no `then` in your `if`. You might also want to explain a bit more about how you populate the `@reviews` (if it is in fact `.nil?` all the time) for us to give you some more ideas. – Pavel Veller Apr 29 '12 at 11:42
  • ~Pavel Veller, the then is optional, but I wouldn't say that there should be no then used. – Marlin Pierce Apr 29 '12 at 13:15
  • @reviews is probably an array, and an empty array is still true. try `empty?`, or as said above, `present?` or `blank?` – DGM Apr 29 '12 at 13:16
  • ~George, print out reviews (before the if) to verify that it's not nil. <%= @reviews.inspect %> – Marlin Pierce Apr 29 '12 at 13:19
45

You can check if an object is nil (null) by calling present? or blank? .

@object.present?

this will return false if the project is an empty string or nil .

or you can use

@object.blank?

this is the same as present? with a bang and you can use it if you don't like 'unless'. this will return true for an empty string or nil .

lesce
  • 6,224
  • 5
  • 29
  • 35
  • `#blank?` and `#present?` have other implications than just whether the object is `nil`. And `then`, while uncommon, is fine in Ruby if statements. – Joshua Cheek Apr 29 '12 at 14:24
  • 1
    Should also be noted that `#present?` and `#blank?` are only available in Rails and don't exist in vanilla Ruby. –  Jul 15 '20 at 19:26
19

Now with Ruby 2.3 you can use &. operator ('lonely operator') to check for nil at the same time as accessing a value.

@person&.spouse&.name

https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators#Other_operators


Use #try instead so you don't have to keep checking for nil.

http://api.rubyonrails.org/classes/Object.html#method-i-try

@person.try(:spouse).try(:name)

instead of

@person.spouse.name if @person && @person.spouse
Chloe
  • 25,162
  • 40
  • 190
  • 357
5

In your example, you can simply replace null with `nil and it will work fine.

require 'erb'

template = <<EOS
<% if (@objectname != nil) then %>
  @objectname is not nil
<% else %>
  @objectname is nil
<% end %>
EOS

@objectname = nil
ERB.new(template, nil, '>').result # => "  @objectname is nil\n"

@objectname = 'some name'
ERB.new(template, nil, '>').result # => "  @objectname is not nil\n"

Contrary to what the other poster said, you can see above that then works fine in Ruby. It's not common, but it is fine.

#blank? and #present? have other implications. Specifically, if the object responds to #empty?, they will check whether it is empty. If you go to http://api.rubyonrails.org/ and search for "blank?", you will see what objects it is defined on and how it works. Looking at the definition on Object, we see "An object is blank if it’s false, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are all blank." You should make sure that this is what you want.

Also, nil is considered false, and anything other than false and nil is considered true. This means you can directly place the object in the if statement, so a more canonical way of writing the above would be

require 'erb'

template = <<EOS
<% if @objectname %>
  @objectname is not nil
<% else %>
  @objectname is nil
<% end %>
EOS

@objectname = nil
ERB.new(template, nil, '>').result # => "  @objectname is nil\n"

@objectname = 'some name'
ERB.new(template, nil, '>').result # => "  @objectname is not nil\n"

If you explicitly need to check nil and not false, you can use the #nil? method, for which nil is the only object that will cause this to return true.

nil.nil?          # => true
false.nil?        # => false
Object.new.nil?   # => false
Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
4

You can use the simple not flag to validate that. Example

if !@objectname

This will return true if @objectname is nil. You should not use dot operator or a nil value, else it will throw

*** NoMethodError Exception: undefined method `isNil?' for nil:NilClass

An ideal nil check would be like:

!@objectname || @objectname.nil? || @objectname.empty?