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.