The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Object#blank?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Object#blank? Posted: Mar 7, 2005 10:03 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Object#blank?
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

So, I’d like to submit Object#blank? as an RCR. I use this code in nearly all of my web application projects.

 class Object
   def blank?
     if respond_to? :empty?
       empty?
     elsif respond_to? :zero?
       zero?
     else
       !self
     end
   end
 end

This has been begging for addition into Ruby for years. A bit of background:

  • 2000: Jim Menard suggests nil.empty?.
  • 2002: Dave Thomas starts to further work up nil.empty?. Opposition brews. (Note that the possibility of nil becoming an Enumerable is kicked around!)
  • 2004: Zero is true … whoda thunk? thread delves in and out of the character of nil, 0, "" and so on. However, the discussion really revolves around whether zero could be treated as false in a future Ruby incarnation.
  • 2005: Just last month, Dan Fitzpatrick revisited the same nil.empty? pitch we’ve seen in years past.

In all, the two major points of backlash have been:

  1. Naming the thing. Some feel that empty? implies a container. A container which isn’t there for nil and 0. Another suggestion was vapid? but, uh, no.
  2. That there is no case in which 0 should be grouped with nil and "". I suppose many people are more commonly looking for "0" to belong to this group as well. Which means you have to use .to_i.zero?. Very similar to .to_s.empty?.

I’m thinking Object#blank? is a good solution. The name doesn’t imply a container. It implies valuelessness, which is what we are testing for.

This reduces the common case of:

 if venue['neighborhood'].to_s.empty?

To:

 if venue['neighborhood'].blank?

I’ve grouped 0 in there as well. It’s useful for YAML. Since YAML loads types naturally, you rarely run into a string like "0". And since I’m calling zero?, it works for floats as well.

Given this YAML invoice:

 sku:   BSK115
 price: 15.60
 tax:   0.00
 total: 15.60

I can easily check the tax, covering the case in which the tax is completely left out.

 print "Tax-free" if invoice['tax'].blank?

So, what do you think? Worthwhile RCR? Am I off on the name? Has our time come for this at last?

I’ve just submitted this proposal as RCR 295, which also weighs the possibility of a to_b conversion. This may kill the RCR, but I’d like to vote and ultimately find the best solution. I still prefer Object#blank?. But either is a move ahead.

Read: Object#blank?

Topic: Adding constants to Dir Previous Topic   Next Topic Topic: Bruce Tate discovers Ruby on Rails

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use