Luiz Felipe Garcia Pereira
2013-Aug-01 22:19 UTC
ActiveRecord::Base.exists? returning 1 instead of true
Hi, I know 1 is truthy and nil is falsy, so it still does as described in the method documentation, but in previous versions it returned true/false (as I expect by its signature). Is there a reason this has changed? Thanks! -- Luiz Felipe G. Pereira -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Steve Klabnik
2013-Aug-01 23:24 UTC
Re: ActiveRecord::Base.exists? returning 1 instead of true
Ruby methods don''t have signatures. ;) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Xavier Noria
2013-Aug-01 23:26 UTC
Re: ActiveRecord::Base.exists? returning 1 instead of true
On Fri, Aug 2, 2013 at 12:19 AM, Luiz Felipe Garcia Pereira < luiz.felipe.gp@gmail.com> wrote: Hi, I know 1 is truthy and nil is falsy, so it still does as described in> the method documentation, but in previous versions it returned true/false > (as I expect by its signature). >In Ruby you should not expect true/false from a method whose name ends in ? In Java they are the only allowed values, but in Ruby (as in Perl, C, and many others) *any* value has an interpretation in boolean context. Therefore you exploit that feature of the language. From a method you have to expect what its contract guarantees. Is there a reason this has changed?>The change happened here: https://github.com/rails/rails/commit/ef64c6ba8c06f89995b4328d3468df9914e3c6b6 and made into 4.0.0 (3-2-stable still has the ternary). The reason Rails in general does not commit to singletons in predicates is that we do not want to write artificial code like the one in 3-2-stable. It is ugly an unnecessary. Doing select_value we are done, so return right there. Documenting a generic predicate interface allows you that freedom. So you use the predicate this way: do_foo if exists? and you test it this way assert exists? or exists?.should be_true You never do do_foo if exists? == true or exists?.should == true If the caller needs a singleton, given our contracts, it is its responsibility to get one out of the predicate: (!!exists?).to_json or whatever. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.