Open vlad-pisanov opened 1 year ago
I guess the intention in all of those is foo&.bar
. I don't see the need of presence
, even though the check is for present?
/ blank?
and not merely nil.
@Darhazer yeah, one case where this is common is whitespace inputs in web forms:
age = params[:age].present? ? : params[:age].to_i : nil
If params[:age]
is " "
:
age = params[:age]&.to_i # 0 ❌
age = params[:age].presence&.to_i # nil ✔️
Yeah, It is better not to omit presence
and safe navigation.
nil.to_json # => "null"
nil.presence.to_json # => "null"
nil.presence&.to_json # => nil
I often see the following pattern (especially when controller parameters are processed), which could be written more compactly with
.presence
: