Is there a Rails equivalent to PHP's isset()? Is there a Rails equivalent to PHP's isset()? ruby ruby

Is there a Rails equivalent to PHP's isset()?


The closer match is probably #present?

# returns true if not nil and not blankparams['foo'].present?

There are also a few other methods

# returns true if nilparams['foo'].nil?# returns true if nil or emptyparams['foo'].blank?


You can also use defined?

See example from: http://www.tutorialspoint.com/ruby/ruby_operators.htm

foo = 42defined? foo    # => "local-variable"defined? $_     # => "global-variable"defined? bar    # => nil (undefined)

Many more examples at the linked page.


Yes. .nil? is the equivalent of isset() in that case when checking the existence of a key in a Hash.

You should use Hash's key? method, which returns true if the given key is present in the receiver:

if(params.key?('foo') && params.key?('bar')) end


matomo