On May 31, 2012, at 8:58 AM, Maksym Melnychok wrote: > Does anyone else finds attribute query methods semantically confusing? > > Hi, > > Consider this code: > > post = Post.new(:visible => true, :url => "http://com") > > if post.visible? > puts "ima visible!" > end > > if post.url? > puts "ima url! (wait wat? o_0)" > end > > Does this feel right to you? In case with post.url? i read it as "Hey post > object, are you an url?" which is apparently not what the code will do. > > So it seems like semantically perfect flag checks go together with totally > confusing(for a reader) way of checking whether an attribute is present or > not. > > I would generate attribute query methods only for boolean attributes.
One use that hasn't been mentioned previously - passing method names to things like :if, :unless etc in validations. With the ? version, this can be pretty short: validates_presence_of :some_attribute, :unless => :some_other_attribute? The no-? version doesn't do the right thing in many cases (since empty strings evaluate to true), and using present? instead requires a Proc. Not certain if this is a sufficient argument for the existence of the ? suffix, but worth thinking about. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.