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

Ruby on-RailsRubyRuby on-Rails-2

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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 .

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeorge PanayiView Question on Stackoverflow
Solution 1 - Ruby on-RailsPavel VellerView Answer on Stackoverflow
Solution 2 - Ruby on-RailslesceView Answer on Stackoverflow
Solution 3 - Ruby on-RailsChloeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJoshua CheekView Answer on Stackoverflow
Solution 5 - Ruby on-RailsVasanth SaminathanView Answer on Stackoverflow