Checking if a variable is not nil and not zero in ruby
unless discount.nil? || discount == 0
# ...
end
Check if not nil and not empty in Rails shortcut?
There's a method that does this for you:
def show
@city = @user.city.present?
end
The present?
method tests for not-nil
plus has content. Empty strings, strings consisting of spaces or tabs, are considered not present.
Since this pattern is so common there's even a shortcut in ActiveRecord:
def show
@city = @user.city?
end
This is roughly equivalent.
As a note, testing vs nil
is almost always redundant. There are only two logically false values in Ruby: nil
and false
. Unless it's possible for a variable to be literal false
, this would be sufficient:
if (variable)
# ...
end
This is preferable to the usual if (!variable.nil?)
or if (variable != nil)
stuff that shows up occasionally. Ruby tends to wards a more reductionist type of expression.
One reason you'd want to compare vs. nil
is if you have a tri-state variable that can be true
, false
or nil
and you need to distinguish between the last two states.
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
Best ruby idiom for nil or zero
Objects have a nil? method.
if val.nil? || val == 0
[do something]
end
Or, for just one instruction:
[do something] if val.nil? || val == 0
Is there a Ruby, or Ruby-ism for not_nil? opposite of nil? method?
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
Checking if variable is nil or empty in a single line using or operator
It's a matter of operator precedence. ||
has a higher precedence than or
.
You should write it like:
str = nil
puts (str.nil? or str.empty?)
puts str.nil? || str.empty?
See more in Difference between "or" and || in Ruby?
For the issue of FPM, I have tried under Ruby 1.8.6, 1.9.3, 2.0.0, and did not get error with or
expression.
Ruby way to check if a string is not blank?
string = ""
unless string.to_s.strip.empty?
# ...
end
Checking if an answer is empty or not
use below code:
puts "Enter your age."
age = Integer(gets.chomp) rescue nil
while age.nil?
age = Integer(gets.chomp) rescue nil
end
How to idiomatically check if a Hash is not nil and so get value of a key in one line in Ruby on Rails?
A typical way would be val = hash && hash[key]
or val = hash[key] if hash
.
You can also use the safe navigation operator, like val = hash&.dig(key)
(see Hash#dig) or val = hash&.[](key)
.. I wouldn't really recommend that last one since it's not very readable
All of those examples will set val
to nil if the hash is nil. If the key must exist and you want to raise an error if it doesn't, you can use val = hash&.fetch(key)
(see Hash#fetch)
Related Topics
How to Debug Http of Ruby Google-Api-Client
How to Export Environment Variable Permanently Using Ruby
How to Build a Ruby Hash Out of Two Equally-Sized Arrays
Ruby Working on Array Elements in Groups of Four
How to Call Expire_Fragment from Rails Observer/Model
Rails: Testing Named Scopes with Rspec
How to Take Screenshots of Web Pages Using Ruby and a Unix Server
Modifying Database Ids from Rails Console
Rails 4.0.1 on Heroku, Can't Create Database
How to Call Java API from Ruby 1.8 or 1.9
Troubleshooting Ssl Certificates, Ruby, MAC Os X Yosemite
Manually Retry Job in Delayed_Job
When to Use Struct Instead of Hash in Ruby
Accessing Config from Application.Rb in Controller (Rails 3)
Why Should I Care About Rvm's Gemset Feature When I Use Bundler
Phusion Passenger Error: You Have Activated Rack 1.2.1, But Your Gemfile Requires Rack 1.2.2