Issue #16120 has been updated by shevegen (Robert A. Heiler).


The idea is very interesting to me, purely from a conceptual point of view
alone. So from this point of view, I like the idea itself, or the thoughts
behind the idea.

HOWEVER had, at the same time, I actually dislike the syntax.

My brain wants to associate the ".something" method call with some specific
object, and in the proposal we would then have a situation where the method
invocation is not directly attached to an object. Again, I understand the
intent, but I dislike it from a syntax point of view.


> In a way it's similar to chaining methods on multiple lines:

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

Yes, I get the intent, but it is not really similar in this sense; it is
actually different because we would then be allowed to omit the reference
to the object "magically".

So if I were to have a say, my personal opinion is -1 on the proposal as
such, even though oddly enough I also like it at the same time (that has
not happened often before where I was against something even though 
liking it in some ways).

Jeremy wrote:

> I totally agree that "saving" a single character makes no difference.

In the context of e. g. numbered parameters, I agree. But when it comes
to Symbols versus Strings, to me the single character matters a lot. :-)

I understand that this has not been why matz added Symbols, but I like
them and use them a lot, even simply as shortcut "identifier" in 
method colours e. g:

    disable :colours

(It's actually convenient to me more generally than e. g. passing a
'string', even though symbols and strings have a different
meaning/purpose.)

> You could have [1,2,3].map{ .itself + @ + @1 }

Now I happily admit that I dislike itself, but I think I dislike 
it even more in a .itself variant, together with @ and @1. Actually,
@1 is better than @. I think @ alone is actually the worst here.

But we should not really tie a suggestion such as this one here
to other not-directly related suggestions such as numbered parameter.
(I think matz went for another syntax meanwhile, so suggestions
may become outdated, too. :))

I agree that:

    posts.map{ .author.name }

is, oddly enough, very readable. I like that part. Still I am against 
it; I completely understand it where we may refer to whatever is 
"held" inside the block, but I don't like free floating method calls
in ruby code to be honest.

Last but not least about numbered paramaters; I bring this due to the
brief discussion about between mame and jeremy.

I think people will always focus about different parts about numbered
parameters. For me the biggest advantage is that we can quickly use
it to debug longer "data structures" without having to remember the
name as-is. I don't think I want it to remain in my own code though,
since it is not super-elegant; on the other hand I am also fine with
it being in code, in principle. But this opinion does not seem to be
the one that you can read most often. People either love it, or 
hate it; sort of. It's strange to me. :-)

Even then I think one last thing should not be forgotten about ALL
changes to ruby in the last ~3 or 4 years or so - specific changes
were made that gave new options. Even though I myself dislike the
syntax part of some suggestions, or my poor eye sight in regards
to object.:method, we should include the possibility of new features
leading to scenarios where less code (or simpler code) might be
use; a good example for that is the safe navigation / lonely 
person staring at a dot, change. 

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

* 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) }  #=> ["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, #8987, #9076, #10318, #10394, #10829, #12115, #15302, #15483, #15723, #15799, #15897, #16113 (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. And then closed together.

This "problem" can be more-or-less stated thus:
* There is a very common pattern in ruby: `posts.map{ |post| post.author.name }`
* In that line, the three 3 "post" in close proximity feel redundant and not DRY.
* To reduce the verbosity, people tend to use a meaningless one-character variable 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 people 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 saying "just use regular block syntax". But the repeated requests over the years, as well as the widespread usage of `(&:to_s)`, definitely indicate this is a wish/need for a lot of people.

Rather than adding to #15723 or #15897, I chose to make this a separate proposal 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 elegant 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 beginning 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 *necessarily* 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 been coding ruby. Like... **this** is what I wanted that `(&:to_s)` could only 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=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>