From: Brian Candler Date: 2009-08-04T21:59:21+09:00 Subject: Re: Inconsistency in Array#[] Rob Biedenharn wrote: > Just take a look at the definition of blank? (and present?) from the > ActiveSupport gem > > activesupport-2.3.2/lib/active_support/core_ext/blank.rb I think that demonstrates a problem - there are different ideas of what "notempty?" means. In the case of ActiveSupport, a string which consists entirely of whitespace is "blank?", even though it is not "empty?" > I typically add a NilClass#nonzero? just so nil plays nicely with > Numeric#nonzero? myself. I find it useful how Javascript treats 0 and "" as false (although strangely not an empty array). You can write things like emit(doc.foo || null); to make things consistent - null and empty string both map to null. Maybe there's a case for something that you can rely on being present at the Object level: class Object; def true?; true; end; end class NilClass; def true?; false; end; end class FalseClass; def true?; false; end; end class String; def true?; !empty?; end; end class Array; def true?; !empty?; end; end class Numeric; def true?; !zero?; end; end Then 'true?' could be called automatically in boolean contexts, like 'if' and 'and'. The latter would be a big language change though. I think the problem with this idea is the same as above: people would disagree as to what's the natural definition of 'not empty', as it would vary from one application to another. -- Posted via http://www.ruby-forum.com/.