From: Aaron Patterson Date: 2011-09-28T03:46:51+09:00 Subject: [ruby-core:39743] Re: [Ruby 1.9 - Feature #5372][Open] Promote blank? to a core protocol --ZGiS0Q5IWpPtfppv Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Sep 27, 2011 at 06:18:19PM +0900, Alex Young wrote: >=20 > Issue #5372 has been reported by Alex Young. >=20 > ---------------------------------------- > Feature #5372: Promote blank? to a core protocol > http://redmine.ruby-lang.org/issues/5372 >=20 > Author: Alex Young > Status: Open > Priority: Normal > Assignee:=20 > Category: core > Target version: 1.9.4 >=20 >=20 > I don't think there's been a project I've used that hasn't made use of th= is pattern: >=20 > if thing.nil? || thing.empty? >=20 > somewhere in its source. This is necessary because it is idiomatic to re= turn nil where other languages might return a null object, and there is no = universal test for nullity which a user-implemented class can provide witho= ut issues. >=20 > Facets (and ActiveSupport) define a #blank? protocol which allows for the= above to be replaced with: >=20 > if thing.blank? >=20 > Being able to type this on a first iteration saves forgetting one of the = cases and having to come back to fix a bug later. For projects where I can= not directly use Facets or ActiveSupport, I always find that I rewrite a ve= rsion for myself. It would be very convenient not to have to do this every= time, and this is clearly a common case, so I propose that #blank? be impl= emented on the following classes: >=20 > Object: to return false > String: aliased to #empty? > NilClass: to return true > TrueClass: to return false > FalseClass: to return true > Array: aliased to #empty? > Hash: aliased to #empty? > Fiber: to return !alive? > Numeric: aliased to #zero? > IO: aliased to #closed? > MatchData: to return #to_s.blank? > Process::Status: aliased to #exited? > Range: to return self.include?(self.begin) > Struct: subclass instances to return values.blank? > Thread: to return !alive? > ThreadGroup: to return list.blank? >=20 > Some of these uses aren't described by the word "blank?" very well (and A= ctiveSupport's String#blank? is somewhat different), so as a sub-feature I'= d like to suggest "null?" as an alternative method name. >=20 > Apologies if this has been proposed and rejected before, but a quick sear= ch of redmine didn't show anything relevant. "empty?" implies that you're performing the operation on a set. Why would you treat these other objects as sets? It doesn't make sense to me. Not to mention defining the method on every possible object seems bad. What if my function shouldn't be dealing with Thread objects? I'd rather a NoMethodError be raised. --=20 Aaron Patterson http://tenderlovemaking.com/ --ZGiS0Q5IWpPtfppv Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (Darwin) iQEcBAEBAgAGBQJOghodAAoJEJUxcLy0/6/GQ7AIAIXqxqXwr3oF6GWjvtT6i3IX G3AQ0nT/Fm9eNQzujUDe2DwBnyrG8V0DVsMT4DMnK54ysb6ZHo+wAyCDESJGbNCZ /7CatcXkr62wT9OOnVozoUO85tZXfWzFA1jWXPyS7Doh6QVm/zNNziOURRpMTQN1 FwiH3ju/+exfPbzccjhI0xCWKF8H0dShXHxDD3d9Z/OrNOho9POdoKwTobEDUw0j z9hNy7l0hJDpADyyoeU8xaE/hAPap/153bOKVpZKnD4cACvfU9J3IovGcciYvwLm xQQ1oS6Q6wdaGYi8r2QFKb+HmH7hRT+1RWFZ8b1NiPXg2b9q9NmnwncMAclUqnM= =XDiJ -----END PGP SIGNATURE----- --ZGiS0Q5IWpPtfppv--