2 ms·
supports_feature? contains gratuitous punctuation. A question mark is useful on predicates when they are nouns. For instance type tests. Here is why: widget(x)
by kazinator 3y ago
supports_feature? contains gratuitous punctuation.
A question mark is useful on predicates when they are nouns. For instance type tests. Here is why: widget(x) looks like a constructor: it's making a widget out of x. If we add a question mark: widget?(x) then we know it's a test whether x is a widget. We have other choices there like is_widget(x).
"Supports feature" has a verb and object. The verb is not in imperative form; it's in a statement form. A question mark is not required.
- graypegg 3y agoI think this is a normal ruby pattern though. A ? prefix is just a best-practice habit to indicate a method returns a real TrueClass or FalseClass instance. You’re always running Ruby statements in some scope, so there’s always an object you’re running methods from. That makes your constructor and reflection examples a bit odd for Ruby. Constructors would be a method on a class, normally the Class instance method #new: Widget.new(x) Checking if y is a Widget would mean calling a method on that instance, normally the Object instance method #is_a?: y.is_a?(Widget) I think that’s adequately distinct.
- deleted 3y ago[deleted]
- skgough 3y agoI always understood the question mark to mean that the method returns a boolean value, eg. the include? method on Arrays returns a bool.