Project

General

Profile

Feature #15991

Allow questionmarks in variable names

Added by aquaj (Jérémie Bonal) about 1 year ago. Updated 12 months ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:93613]

Description

Hi,

I thought such an issue would've already been discussed but no number of searches allowed me to find a similar request. Feel free to close if I missed a previous refusal.

From time to time, especially when trying to clear up complex conditional logic, I find myself wishing I could add ? to variable names, since I got used to it while naming methods.

For example, currently:

if (node? && terminal?) || (halting && (value == halting))
  # ...
end

becomes

last_node = self.node? && self.terminal?
halt_on_node = halting && (value == halting)
if last_node || halt_on_node
  # ...
end

halt_on_node is clear enough, but last_node feels like it would contain a node, instead of expressing its actual purpose ("is the node the last one?").
Right now a developer would have two options as I see them:
1 - extract the conditional to a method def last_node? which can be a bit much if it's the only place this code is called.
2 - rename the variable something like is_last_node, which feels a bit silly since we're in ruby and used to seeing ?s for predicates.

Trying to assign to a questionmarked variable (a? = true) raises a SyntaxError. IMHO, it would make for more coherent design to allow it, just like we do in method names.

I was afraid that variable? would be already parsed as beginning a ternary expression (variable?1:3) but this isn't parsed either, the only thing it's used for is for method calls (a?5 <==> a?(5)), so this change wouldn't disrupt any current behavior, the expression would just be looked up like any other call instead of only looking up methods.

The only thing I can see with this is that it might raise the issue of allowing !s in variable names too, which I'm not sure makes a lot of sense (unlike ? which denotes "booleanness", a trait shared by variables and methods alike, I can't see how a variable would be "dangerous").


Related issues

Related to Ruby master - Feature #12046: Allow attr_reader :foo? to define instance method foo? for accessing @fooRejectedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #5781: Query attributes (attribute methods ending in `?` mark)Assignedmatz (Yukihiro Matsumoto)Actions

Also available in: Atom PDF