ruby - Predicate method syntax -


i made class these methods:

def promotion_available?   promotion.present? end  def promotion   @promotion ||= promotionuser.user(@user.id).available.first end 

then, colleague removed promotion method, , changed predicate promotion_available? in way:

def promotion_available?   @promotion ||= promotionuser.user(@user.id).available.first end 
  • can set instance variable directly on predicate method?
  • can predicate method return entire object instead of true/false (i think no, colleague says opposite)?

can set instance variable directly on predicate method?

yes, there's nothing wrong it. often.

can predicate method return entire object instead of tre/false (i think no, colleague says opposite)

yes, common. works if use idiomatic ruby truthiness checks.

if obj.promotion_available? # if obj.promotion_available? == true # bad! 

remember, nil , false in ruby falsey values. else true. why returning object or nil works kinda returning true or false.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -