Is there a Ruby, or Ruby-ism for not_nil? opposite of nil? method?

berkes picture berkes · Oct 25, 2010 · Viewed 59.8k times · Source

I am not experienced in Ruby, so my code feels "ugly" and not idiomatic:

def logged_in?
  !user.nil?
end

I'd rather have something like

def logged_in?
  user.not_nil?
end

But cannot find such a method that opposites nil?

Answer

lwe picture lwe · Oct 25, 2010

when you're using ActiveSupport, there's user.present? http://api.rubyonrails.org/classes/Object.html#method-i-present%3F, to check just for non-nil, why not use

def logged_in?
  user # or !!user if you really want boolean's
end