Issue #15723 has been updated by marcandre (Marc-Andre Lafortune).


I would like a notation to replace `{|x| ...}`, and *only* to replace this.

I strongly disagree with current implementation:
1) that allows multiple unnamed parameters
2) where `{@1}` actually doesn't replace `{|x| ...}` but `{|x, | ...}`

* Rationale *

1) allowing multiple unnamed parameters is a mistake
- it usually wouldn't make code clear
- it's not that useful (see stats at the end)
- the conciseness gain of allowing to write a number in `@2`, ... is negated by having to write "1" for the simple case `@1` (see stats at the end)

2) `{ @1 ... }` (or whichever shorthand we use) should replace `{|x| ...}`, not `{|x, | ...}`
- the `|x|` form is used 128x times more than the `|x, |` (see stats at the end). That's basically always what we want to say.
- the `|x, |` can lead to errors that can be hard to detect.

A typical example is anything that handles heterogenous objects:

```
  def process(*args)
    args.each { _process_single(@1) }
  end
```

This will appear to work fine, until one argument is an array and this will call `_process_single` on the first element of the array instead of the array itself.

* Stats *

```
493693 (78 %): {}
115049 (18 %): {|x|}
17685 (2.8 %): {|x, y|}
2330 (0.37 %): {|x, y, z|}
1053 (0.17 %): {|*rest|}
901  (0.14 %): {|x,|}
890  (0.14 %): {|x, (...)}
558  (0.09 %): {|w, x, y, z|}
      (0.14%): others
```

These are for 633k block signatures of the 400+ top gems.

Note that stats vary depending on the project style, e.g. mongoid has 96% of its blocks without arguments, compared to 71% for Rails, but the totals on 400+ projects should give a pretty good image.

Gain for `{|x, y| x + y} => {@1 + @2}` is at most 5 characters
The extra "1" for `{|x| x} => {@1}` costs (at least) 1 character compared to `{@}`. Assuming we use the shorthand all the time, this cost is incurred 6.5 times more often than the "gain" for the `@1 + @2` so is a net loss compared to simply `@`!

* Conclusion *

Let's first agree that a shorthand notation would only simplify `{|x| foo(x) }` and no other block signature.

Then we can discuss if it should be `@1`, `@`, `it`, or my personal favorite (that I havn't seen discussed): `_`.



----------------------------------------
Misc #15723: Reconsider numbered parameters
https://bugs.ruby-lang.org/issues/15723#change-77486

* Author: sos4nt (Stefan Schler)
* Status: Feedback
* Priority: Normal
* Assignee: 
----------------------------------------
I just learned that *numbered parameters* have been merged into Ruby 2.7.0dev.

For readers not familiar with this feature: it allows you to reference block arguments solely by their *index*, e.g.

```ruby
[1, 2, 3].each { |i| puts i }

# can become

[1, 2, 3].each { puts @1 }
```

I have an issue with this new feature: I think **it encourages sloppy programming** and results in **hard to read code**.

---

The [original proposal](https://bugs.ruby-lang.org/issues/4475) was to include a special variable (or keyword) with a **readable name**, something like:

```ruby
[1, 2, 3].each { puts it }

# or

[1, 2, 3].each { puts this }
```

Granted, that looks quite lovely and it actually speaks to me  I can *understand* the code. And it fits Ruby: (quoting the website)

> [Ruby] has an elegant syntax that is natural to read and easy to write.

But the proposed `it` / `this` has limited application. It's only useful when dealing with a single argument. You can't have multiple `it`-s or `this`-es. That's why `@1`, `@2`, `@3` etc. were chosen instead.

However, limiting the usefulness to a single argument isn't bad at at. In fact, a single argument seem to be the limit of what makes sense:
```
h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }

# vs

h = Hash.new { @1[@2] = "Go Fish: #{@2}" }
```
Who wants to read the latter? That looks like an archaic bash program (no offense). We already discourage Perl style `$`-references: (from [The Ruby Style Guide](https://github.com/rubocop-hq/ruby-style-guide#no-perl-regexp-last-matchers))

> Don't use the cryptic Perl-legacy variables denoting last regexp group matches (`$1`, `$2`, etc). Use `Regexp.last_match(n)` instead.

I don't see how our code can benefit from adding `@1` and `@2`.

Naming a parameter isn't useless  it gives context. With more than one parameter, naming is crucial. And yes, naming is hard. But avoiding proper naming by using indices is the wrong way.

So please reconsider numbered parameters.

Use a readable named variable (or keyword) to refer to the first argument or ditch the feature entirely.



-- 
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>