Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

As 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.


It'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....

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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: