I have been puzzled or confused with the method nil?, empty?, blank? And present?
How do you do it differently?
nil? -> This can be used with any object will return true (true) when the object is not nil and false (false) if the object has something in it.
Example:.
empty? -> This can not be used with any object, this is valid for using with object of class array , Hashes and String . This will return true (true) if the object does not have any value at all.
Whenempty? is used for nil object it will throw an error "NoMethodError" .
When used withstrings , arrays and hashes
String length == 0.
Array length == 0.
Hash length == 0.
Example:.
empty?
# => false .
blank? ->
This can be used for any object and This will returns How do you do it differently?
Example:.
nil. nil? # => true. [].nil? # => false. "".nil? # => false. " ".nil? # => false.
When
When used with
Array length == 0.
Hash length == 0.
Example:.
[].empty? # => true. nil.empty? # => Undefined method. "".empty? # => true. " ".empty? # => false
Example:.
[]. blank? # => true. nil.blank? # => true. "".blank? # => true. " ".blank? # => true. false.blank? # => true
Example:.
[].present? # => false. nil.present? # => false. "".present? # => false. " ".present? # => false. false.present? # => false.Summary:
Finally, With different conditions you can choose best suited methods.
Note:
Related posts:
Difference between update_attribute and update_column
What is different in update_all, update, update_attribute, update_attributes methods of Active Record
Difference between .nil?, .empty?, .blank?, .present?
render vs redirect_to in Ruby on Rails
About
1 comment:
Post a Comment