ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: hanmac@gmx.de
To: ruby-core@ruby-lang.org
Subject: [ruby-core:94612] [Ruby master Feature#16120] Omitted block argument if block starts with dot-method call
Date: Tue, 27 Aug 2019 15:13:04 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-81088.20190827151304.87a5894d98964db2@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16120.20190823152201@ruby-lang.org

Issue #16120 has been updated by Hanmac (Hans Mackowiak).


@Dan0042 in your list about implicit and explicit you forgot `{ foo }` depending on the method who gets the block, it might does an instance_eval thing where the block self is the block variable

i know that would need to change of the method, but this one might be possible too

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

* 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, #9076, #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.

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/

  parent reply	other threads:[~2019-08-27 15:13 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16120.20190823152201@ruby-lang.org>
2019-08-23 15:22 ` [ruby-core:94503] [Ruby master Feature#16120] Implicit block argument if block starts with dot-method call daniel
2019-08-23 15:36 ` [ruby-core:94505] " manga.osyo
2019-08-23 23:42 ` [ruby-core:94516] " shannonskipper
2019-08-24  0:59 ` [ruby-core:94519] " daniel
2019-08-24  1:57 ` [ruby-core:94520] " mame
2019-08-26 19:34 ` [ruby-core:94588] " daniel
2019-08-26 23:49 ` [ruby-core:94592] " mame
2019-08-27  0:18 ` [ruby-core:94593] " nobu
2019-08-27  1:39 ` [ruby-core:94594] " daniel
2019-08-27  2:16 ` [ruby-core:94595] " merch-redmine
2019-08-27 14:43 ` [ruby-core:94610] [Ruby master Feature#16120] Omitted " daniel
2019-08-27 15:13 ` hanmac [this message]
2019-08-27 17:36 ` [ruby-core:94614] " daniel
2019-09-20  7:22 ` [ruby-core:94998] " shevegen
2019-09-23 17:05 ` [ruby-core:95044] " daniel
2019-09-24 22:08 ` [ruby-core:95068] " daniel
2019-10-16 15:58 ` [ruby-core:95368] " daniel
2019-10-17  8:42 ` [ruby-core:95390] " matz
2019-10-17 13:27 ` [ruby-core:95393] " daniel
2019-10-18  7:01 ` [ruby-core:95414] " nobu
2019-10-18  8:19 ` [ruby-core:95416] " fg
2019-10-18  8:30 ` [ruby-core:95417] " hanmac

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-81088.20190827151304.87a5894d98964db2@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).