Issue #13207 has been updated by Nobuyoshi Nakada.

Status changed from Open to Feedback

`Binding#local_variable_get` is for that purpose.

Or do you have any concrete proposal?

----------------------------------------
Feature #13207: Allow keyword local variable names like `class` or `for`
https://bugs.ruby-lang.org/issues/13207#change-62954

* Author: Kasper Timm Hansen
* Status: Feedback
* 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:)
  %(<label class=""#{class.camelize}>#{text}</label>")
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: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>