Issue #16120 has been updated by Dan0042 (Daniel DeLorme).


I think there's a misunderstanding because this proposal doesn't collide wi=
th existing parser behavior. `[].each{ .method=A0}` is currently a SyntaxEr=
ror.

osyo (manga osyo) wrote:
> However, I think it is difficult to parse in the following cases.

It parses just like this:
 =

```ruby
[10, 20, 30].map{ |v| v
  # 42.to_s(16)
  # or
  # pp 42
  # argument1.to_s(16)
  pp 42
  .to_s(16)
}
```

In other words the block argument is not used, and `.to_s(16)` applies to `=
42`, just like regular method chaining.

----------------------------------------
Feature #16120: Implicit block argument if block starts with dot-method call
https://bugs.ruby-lang.org/issues/16120#change-80955

* Author: Dan0042 (Daniel DeLorme)
* Status: Open
* Priority: Normal
* Assignee: =

* Target version: =

----------------------------------------
How about considering this syntax for implicit block parameter:
```
[10, 20, 30].map{ .to_s(16) }  #=3D> ["a", "14", "1e"]
```
Infinite thanks to @maedi for [the idea](https://bugs.ruby-lang.org/issues/=
15723#note-19)

This proposal is related to #4475, #10829, #12115, #15302, #15483, #15723, =
#15897 (and probably many others) which I feel are all trying to solve the =
same "problem". So I strongly believe all these feature requests should to =
be considered together in order to make a decision.

This "problem" can be more-or-less stated thus:
* There is a very common pattern in ruby: `posts.map{ |post| post.author.na=
me }`
* In that line, the three 3 "post" in close proximity feel redundant and no=
t DRY.
* To reduce the verbosity, people tend to use a meaningless one-character v=
ariable in the block
* But even so `posts.map{ |p| p.author.name }` still feels redundant.
* This "problem" is felt by many in the ruby community, and is the reason p=
eople often prefer `posts.map(&:author)`
* But that only works for one method with no arguments.
* This results in many requests for a block shorthand than can do more.

I realize that many people feel this is not a problem at all and keep sayin=
g "just use regular block syntax". But the repeated requests over the years=
, as well as the widespread usage of `(&:to_s)`, definitely indicate this i=
s a wish/need for a lot of people.

Rather than adding to #15723 or #15897, I chose to make this a separate pro=
posal because, unlike `it` or `@` implicit variables, it allows to simplify=
 **only** `{ |x| x.foo }`, not `{ |x| foo(x) }`. This is on purpose and, in=
 my opinion, a desirable limitation.

The advantages are (all in my opinion, of course)
* Extremely readable: `posts.map{ .author.name }`
   * Possibly even more than with an explicit variable.
* Of all proposals this handles the most important use-case with the most e=
legant syntax.
   * It's better to have a beautiful shorthand for 90% of cases than a non-=
beautiful shorthand for 100% of cases.
   * A shorthand notation is less needed for `{ |x| foo(x) }` since the two=
 `x` variables are further apart and don't feel so redundant.
* No ascii soup
* No potential incompatibility like `_` or `it` or `item`
* Very simple to implement; there's just an implicit `|var| var` at the beg=
inning of the block.
* In a way it's similar to chaining methods on multiple lines:

        posts.map{ |post| post
          .author.name
        }

It may be interesting to consider that the various proposals are not *neces=
sarily* mutually exclusive. You *could* have `[1,2,3].map{ .itself + @ + @1=
 }`. Theoretically.

I feel like I've wanted something like this for most of the 16 years I've b=
een coding ruby. Like... **this** is what I wanted that `(&:to_s)` could on=
ly deliver half-way. I predict that if this syntax is accepted, most people=
 using `(&:to_s)` will switch to this.




-- =

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

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