3 ms·
No, your conception of methods suffixed with ? is false. Methods suffixed with ? are expected to return a truthy/falsy value. nil and false are perfectly falsy
by Xylakant 14y ago
No, your conception of methods suffixed with ? is false. Methods suffixed with ? are expected to return a truthy/falsy value. nil and false are perfectly falsy in comparisons and any string will evaluate to true. So returning the type of the object in question and nil in the false case is perfectly valid and expected. It's not the only method to work that way.
- gfodor 14y agoAs stated elsewhere in the thread, this is a clever retort but in practice defined? is the only major example I've seen where the ? suffix results in a non-boolean return value. You're arguing over a convention, and I think most people writing Ruby would be surprised to learn defined? returns a String.
- Xylakant 14y agoIt's a common misconception that ruby developers think that predicates must return a boolean value. It's still a misconception. This blog post linked elsewhere in the discussion is a good summary: http://blog.leshill.org/blog/2012/03/25/a-question-of-truth.html http://blog.leshill.org/blog/2012/03/25/a-question-of-truth.... Another notable example is nonzero? which returns the value of the number if nonzero and nil otherwise: jruby-1.6.8 :097 > 1.nonzero? => 1 jruby-1.6.8 :098 > 0.nonzero? => nil See also File#world_writable? [1], File#world_readable? [2], File#size? [3], Kernel#autoload? [4], Encoding#compatible? [5] If you rely on predicates always returning true/false you're in for a nasty surprise. [1] http://ruby-doc.org/core-1.9.3/File.html#method-c-world_writable-3F [2] http://ruby-doc.org/core-1.9.3/FileTest.html#method-i-world_readable-3F [3] http://ruby-doc.org/core-1.9.3/File.html#method-c-size-3F [4] http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-autoload-3F [5] http://ruby-doc.org/core-1.9.3/Encoding.html#method-c-compatible-3F