Basically just a check to make sure a url param was set. How I'd do it in PHP:
if(isset($_POST['foo']) && isset($_POST['bar'])){}
Is this the rough/best equivalent to isset() in RoR?
if(!params['foo'].nil? && !params['bar'].nil?) end
The equivalent of isset($var) for a function return value is func() === null .
The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
The isset() function is a built-in function of PHP, which is used to determine that a variable is set or not. If a variable is considered set, means the variable is declared and has a different value from the NULL. In short, it checks that the variable is declared and not null.
isset() is best for radios/checkboxes. Use empty() for strings/integer inputs. when a variable contains a value, using isset() will always be true. you set the variable yourself, so it's not a problem.
The closer match is probably #present?
# returns true if not nil and not blank params['foo'].present?
There are also a few other methods
# returns true if nil params['foo'].nil? # returns true if nil or empty params['foo'].blank?
You can also use defined?
See example from: http://www.tutorialspoint.com/ruby/ruby_operators.htm
foo = 42 defined? foo # => "local-variable" defined? $_ # => "global-variable" defined? bar # => nil (undefined)
Many more examples at the linked page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With