Issue #4475 has been updated by gkemmey (Gray Kemmey).


Is there a reason to do this other than to save some typing? I'm not sure we've proposed any terribly real-world cases here where this made code better.

I feel like we already have some single-character patterns around blocks with one or two arguments, that aren't that onerous to type and still convey meaning. For example:

```ruby
10.times  { |i| ... } # i for index
10.times { |n| ... } # n for number

[1, 2, 3].map { |n| ... } # n for number
[1, 2, 3].map { |e| ... } # e for element

['one', 'two'].map { |s| ... } # s for string

[1, 2, 3].each_with_index { |e, i| ... }  # e for element, i for index

{ }.each { |k, v| ... }  # k for key, v for value

[[], []]each { |r, c| ... } # two-d arrays: r for row, c for column

File.open('') { |f| ... } # f for file
```

For anything more complex, is it bad too *have* to name your arguments?

In no particular order, some downsides I see:

1. It adds some cognitive load to remember the position of arguments. Like in the `{}.each` example -- is `@1` the key or the value? It's obvious when using `k` and `v` which is which.

2. It blurs the line between instance variables.

3. I don't know that we use simple blocks like this *most* of the time, which means, as a newcomer, you won't see this regularly, and it'll be confusing when you do. Think about the first time you saw something like `[1,2,3].reduce(&:+)`. Was that clear to anyone? (*Though in the `&` case, certainly worth the complexity.*)

4. It's always a little strange when things like this aren't 0-based, right?

5. Personally, find it a little jarring to iterate over an array and then use `@1` to reference each element. "At one" just *feels* positional (like an index).

Anyway, this seems like it adds a fair amount of complexity and sacrifices a lot of readability, just to save ~2 characters. Happy to hear why I'm wrong though...

----------------------------------------
Feature #4475: default variable name for parameter
https://bugs.ruby-lang.org/issues/4475#change-77146

* Author: jordi (jordi polo)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
=begin

There is a very common pattern in Ruby:

 object.method do |variable_name|
  variable_name doing something  
 end

 Many times in fact the name of the object is so self explanatory that we don't care about the name of the variable of the block. It is common to see things like :

 @my_sons.each { |s| s.sell_to_someone }

or

 Account.all.each { |a|  my_account << a.money }


 People tend to choose s or a because we have the class or the object name just there to remind you about the context. 


I would like to know if can be a good idea to have a default name for that parameter. I think it is Groovy that does something like:

  Account.all.each { my_account << it.money } 

Where it is automagically filled and it doesn't need to be declared.  

I think it is as readable or more (for newbies who don't know what is ||) and we save some typing :)


=end




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