Issue #15991 has been updated by noniq (Stefan Daschek).


For me personally that I often write code like this:

```ruby
class Foobar
  attr_reader :completed

  def completed?
    @completed
  end
end
```

This feels unnecessary complex to me. In #12046 it was discussed to allow `attr_reader :foo?` which would internally access `@foo`. This change has been rejected by Matz:

> as a rule, attr_reader x creates an instance variable @x, but we cannot have @x?.

Im personally not very interested in having / using instance variables ending in `?`, but if allowing such variable names would result in a feature like #12046 becoming possible, Im very much in favour of this proposal.

----------------------------------------
Feature #15991: Allow questionmarks in variable names
https://bugs.ruby-lang.org/issues/15991#change-79237

* Author: aquaj (Jrmie Bonal)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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").




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>