Ruby: "If !Object.Nil" or "If Object"

Ruby: if !object.nil? or if object

There are differences. For example:

false.nil?
# => false

So:

if !false.nil?
'foo'
end
# => "foo"

if false
'foo'
end
# => nil

As @tokland suggested, in most cases using !obj.nil? construction is unnecessary.

Ruby: check if object is nil

You seem to have a few questions here, so I'll take a stab at what seems to be the main one:

If you want to see if something is nil, just use .nil? - so in your example, you can just say request.nil?, which returns true if it is nil and false otherwise.

How to check for nil and compare if != nil on Hashes?

If you want to check if an object/value is nil you can do it like this:

object.nil?

in your case (the value of a hash is something like): your_hash[key].nil?, with that said if you want to do something if the value is not nil you can do:

if !my_hash[my_key].nil?
end

or the ruby way:

unless my_hash[my_key].nil?
end

Also if you want to know whether a hash contains or not a key you can do it as follow:

your_hash.keys.include? key_to_search

If you want to compare 2 strings: str1 == str2, if you want to know if str1 contains to str2 as substring then str1.include? str2

Is !object.nil? == object.present? in Rails?

.present? is the negation of .blank?, which is similar but decidedly unique from .nil?.

.nil? only evaluates whether an object is NilClass.

.blank? evaluates to true when evaluating a string that is not empty and may (or may not) contain whitespace.

It's superfluous to your question, but it's probably handy to note that .blank? and .present? are Rails methods, whereas .nil? is pure Ruby.

EDIT:

If, in your example, object is equal to false, then no, it's not safe to assume that !object.nil? == object.present?. However, if object is actually a Ruby/Rails object, then it's safe to assume that the statement will evaluate to true.

!false.nil? == false.present? #=> false

How to check if an object is nil in a view in Ruby?

How about:

<% if !@foo.nil? && !@foo.new_record? %>
Hello!
<% end %>

First off, you need to be using AND logic rather than OR logic here, since any ActiveRecord object meets at least one the requirements of "not nil" or "not a new record".

Second, checking for nil first ensures that the second check isn't run if the first one fails. The error is thrown because you can't use #new_record? on an object that doesn't support it, so checking for nil first ensures that that method is never run on a nil.

rails if object.nil? then magic '' in views?

What about:

<%= tax_payment.user.name if tax_payment.user %>

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

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

Check if Object is nil and needed attribute is nil or empty

What about this?

data = response.try(:[], 'data')
raise Exception, "Response is not valid" if data.nil? || data.empty?

As @ksol correctly mentions in the comments, try helper comes from ActiveSupport. But it is not difficult at all to re-implement.

class Object
def try method, *args
if respond_to? method
send method, *args
else
nil
end
end
end

class Foo
def hello name
"hello #{name}"
end
end

f = Foo.new
f.try(:bar) # => nil
f.try(:hello, 'world') # => "hello world"
nil.try(:wat) # => nil

Alternatives

Here's Object#andand if you don't want to drag along the entire activesupport and don't feel like writing code that's already written.

data = response.andand['data']
raise Exception, "Response is not valid" if data.nil? || data.empty?


Related Topics



Leave a reply



Submit