Is There a Better Way of Checking Nil or Length == 0 of a String in Ruby?
When I'm not worried about performance, I'll often use this:
if my_string.to_s == ''
# It's nil or empty
end
There are various variations, of course...
if my_string.to_s.strip.length == 0
# It's nil, empty, or just whitespace
end
Testing for empty or nil-value string
The second clause does not need a !variable.nil?
check—if evaluation reaches that point, variable.nil
is guaranteed to be false (because of short-circuiting).
This should be sufficient:
variable = id if variable.nil? || variable.empty?
If you're working with Ruby on Rails, Object.blank?
solves this exact problem:
An object is blank if it’s false, empty, or a whitespace string. For example,
""
," "
,nil
,[]
, and{}
are all blank.
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
Ruby way to check if a string is not blank?
string = ""
unless string.to_s.strip.empty?
# ...
end
Checking if a variable is not nil and not zero in ruby
unless discount.nil? || discount == 0
# ...
end
What is a better way to check for a nil object before calling a method on it?
Personally, I would use the or
operator/keyword:
(financial_document.assets or []).length
Either way, .length
is called on an array, giving you 0
if nil
.
Rails Model method check if field nil, if so return empty string
You can, although this might not be what you wanted, simply remove any nil
objects from the address Array using the #compact
method, so that #empty?
will work...:
def full_address
[self.address1, self.address2, self.address3,
self.city, self.state, self.zipcode].compact.reject!(&:empty?).join(', ')
end
You should be aware that this WILL return a partial address if SOME of the fields exist.
I would probably void any fault addresses (in this example I require just the first address line and the city fields):
def full_address
return "" unless self.address1 && self.city && !self.address1.empty? && !self.city.empty?
[self.address1, self.address2, self.address3,
self.city, self.state, self.zipcode].compact.reject!(&:empty?).join(', ')
end
Disregarding the few edits I made to the code, I would probably go with @Joseph's #present?
together with the validation, since you're using Rails:
def full_address
return "" unless self.address1 && self.city && self.address1.present? && self.city.present?
[self.address1, self.address2, self.address3,
self.city, self.state, self.zipcode].reject!(&:present?).join(', ')
end
How to check if controller action variable is empty
I would just go with
if @post.url.nil?
which returns true
when @post.url
is nil
but false
when @post.url
is an empty string.
Alternatively, you can use
if @post.url.blank?
which returns true
in both cases when @post.url
is nil
or an empty string.
Related Topics
Rails Respond_To Format.Js API
How to Remove Lines of Data in the Middle of a Text File with Ruby
Find the Newest Record in Rails 3
Ruby: Parsing a String Representation of Nested Arrays into an Array
Ruby:Difference Between Instance and Local Variables in Ruby
Shortcut to Make Case/Switch Return a Value
Cron Job Not Working in Whenever Gem
"Rake Assets:Precompile" Gives Punc Error
How to Implement a Friendship Model in Rails 3 for a Social Networking Application
Converting an Array of Objects to Activerecord::Relation
How to Group by Day Instead of Date
Read Binary File as String in Ruby
Class ≪≪ Self VS Self.Method With Ruby: What's Better
When Do I Need to Restart Server in Rails
How to Calculate the Day of the Week of a Date in Ruby