present?method to check whether a key-value pair is present is an input hash. The code would be something like the following.
o = Model.where(:id => params[:id].to_i).first
o.field = params[:field_key] if params[:field_key].present?
This works fine when field is most cases. However, when the field is a
booleanfield, there is a subtle with this code.
params[:field_key]is true, the code works fine; but whenever it is
false, the code fails. The intent is to see if the key-value pair is present and if present the value should be assigned to the field. Now, the value can be both
falseand both of those should be assigned to field. However, when the value is
present?function returns false and the field is not set any more. So the field value stays
trueand with the above code can not be flipped to
false. The correction is of course simple.
o.field = params[:field_key] unless params[:field_key].nil?
This behaviour did not seem intuitive to me. So, I looked up documentation and source for
present?. The definition of
present?is quite simple.
Clearly all it does is to negate the result of
blank?. So, I looked up the definition for that too.
respond_to?(:empty?) ? empty? : !self
When I tested
false.respond_to?(:empty?), it returned
false. So, it is clearly executing the
!selfpart, which translates to