From: kaspth@...
Date: 2017-02-11T15:47:09+00:00
Subject: [ruby-core:79502] [Ruby trunk Feature#13207] Allow keyword local variable names like `class` or `for`
Issue #13207 has been reported by Kasper Timm Hansen.
----------------------------------------
Feature #13207: Allow keyword local variable names like `class` or `for`
https://bugs.ruby-lang.org/issues/13207
* Author: Kasper Timm Hansen
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
Sometimes when trying to write expressive Ruby you enevitably hit a case
that would sound just right if the variable name matches a Ruby keyword.
E.g. writing a method to output HTML tags:
```ruby
def label_tag(text, class:)
%(")
end
```
Or a method to generate a representation `for` a specific purpose:
```ruby
def to_gid(for:)
for ||= :universal
GlobalID.generate(self.class.name, id, for)
end
```
Currently Ruby's keywords get in the way of the type of code we'd like to write.
Instead we have to use variable names like `klass` or `modjule`:
```ruby
[ A::B, C::D ].each { |klass| puts klass.name }
```
It would make me a happier programmer if I could write more naturally instead of
worrying about keywords clashing with local variable names.
It also stands to me that far more often when there is a potential clash I want
the variable name to win out. It's unlikely I will be defining classes or
modules within a method say. In those rare cases where I do,
we could expose keywords like this: `Keyword.class`, `Keyword.for` etc.
I propose that renaming a keyword is fair game anywhere except the root scope and
that a rename follows local variable scoping.
NOTE: I think this could also remove the `self` currently needing to be
prepended to `self.class`.
I hope this can be yet another case in Ruby's quest to go a bit out of its way
to make programmers lives happier. Thanks!
--
https://bugs.ruby-lang.org/
Unsubscribe: