Why can't a variable name end with `?` while a method name can?

前端 未结 2 1571
迷失自我
迷失自我 2020-12-05 17:18

A method name can end with a question mark ?

def has_completed?
  return count > 10
end

but a variable name cannot.

2条回答
  •  天涯浪人
    2020-12-05 17:35

    You'd have to ask Matz to get an authoritative answer. However,

    • Ruby is an untyped programming language and a variable like finished? would imply a specific type (boolean) which appears somewhat contradictory to me.
    • A question somewhat requires a receiver (who can answer the question). A method must have a receiver (the object the method is called on), so a question mark makes sense. A variable on the other hand has no receiver, it's a mere container.

提交回复
热议问题