empty?, nil?, blank?, & present? (MOTM 2010.05)

4,259 views
3,982 views

Published on

Ruby Method of the Month 2010.05. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,259
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
9
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide












  • empty?, nil?, blank?, & present? (MOTM 2010.05)

    1. 1. empty?, nil?, blank? & present? Method of the Month Kevin Munc @muncman
    2. 2. empty? There’s a lot of empty?! 20 matches in my local copy of RailsAPI. Including versions on: Array, Hash, Set, String, and more.
    3. 3. empty? The Gist: returns true if the type is empty. (Surprise!)
    4. 4. empty? The Gist: returns true if the type is empty. (Surprise!) “”.empty? == true {}.empty? == true [].empty? == true
    5. 5. empty? The Gist: returns true if the type is empty. (Surprise!) “ ”.empty? “”.empty? == true == false {}.empty? == true [nil,nil].empty? == false [].empty? == true {:k=>nil}.empty? == false
    6. 6. nil? nil? is kinda minimal. Only two occurrences in my copy of the RailsAPI. On NilClass and Object. (Both from the object.c source file.) (http://ruby-doc.org/doxygen/1.8.4/object_8c-source.html)
    7. 7. nil? “Only the object nil responds true to nil?” - RailsAPI
    8. 8. nil? “Only the object nil responds true to nil?” - RailsAPI nil.nil? == true anything_else.nil? == false
    9. 9. blank? blank? is filled in by Rails. Added by ActiveSupport. On Object.
    10. 10. blank? blank? is true if the target is false, empty?, nil?, or all whitespace.
    11. 11. blank? blank? is true if the target is false, empty?, nil?, or all whitespace. “”.blank? == true {}.blank? == true [].blank? == true
    12. 12. blank? blank? is true if the target is false, empty?, nil?, or all whitespace. “ “.blank? == true “”.blank? == true “t”.blank? == true {}.blank? == true nil.blank? == true [].blank? == true false.blank? == true
    13. 13. present? present? is another gift from ActiveSupport. It appears in one class in RailsAPI. On Object.
    14. 14. present? The opposite of blank? (That’s not a question.)
    15. 15. present? The opposite of blank? (That’s not a question.) # File activesupport/lib/active_support/core_ext/object/blank.rb, line 17 # (Ruby 1.9) 17: def present? 18: !blank? 19: end
    16. 16. http://www.flickr.com/photos/st3f4n/3556697439/in/set-72157616350171741// Questions?

    ×