From: jonathan@... Date: 2020-04-13T03:31:34+00:00 Subject: [ruby-core:97863] [Ruby master Feature#15897] `it` as a default block parameter Issue #15897 has been updated by joallard (Jonathan Allard). jeremyevans0 (Jeremy Evans) wrote in #note-39: > I'm going to close this feature since numbered block parameters were introduced to address it. Noted. But I think there might have been some confusion. In my opinion, the spirit of this ticket was to adopt `it` as a syntax rather than solve the underlying function with any combination of non-word symbols (eg. _1, @1, or $_0, _[0], ...) It seems to me like there's a fair amount of agreement about it. Having slept on it for a few months now (pun intended) and as a daily user of Ruby for soon 10 years ����� and seeing it's in usage in other language ���, I can say that adopting the keyword `it` is still relevant and a good idea to me. If I may argue on the substance just a bit more, I see the very essence of Ruby in `it`. Not in `_1`, unfortunately. Just like I see Ruby in `array.first` rather than `array[0]`. I am reminded of Ruby's readability-before-purity philsophy and "Write the code you wish you could". Moreover, the one-arg block (`obj.method{ |one| one... }`) pattern is, in my experience, used quite frequently. `it` could make that both shorter and more expressive. That is why I find importance in this proposal. When I asked whether there was anything needed to move forward, that's what it was about. The `_1` syntax does not address the needs for a natural, expressive, terse block param syntax. Of course, seeing the current resolution, I'm not having unrealistic expectations, but I just wanted to be clear with what the proposal was for and make the case. If the right way is to open another ticket or there's another way forward, I'm happy to walk the walk or help. ---------------------------------------- Feature #15897: `it` as a default block parameter https://bugs.ruby-lang.org/issues/15897#change-85088 * Author: mame (Yusuke Endoh) * Status: Closed * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) ---------------------------------------- How about considering "it" as a keyword for the block parameter only if it is the form of a local varaible reference and if there is no variable named "it"? ``` [1, 2, 3].map { it.to_s } #=> ["1", "2", "3"] ``` If you are familiar with Ruby's parser, this explanation is more useful: NODE_VCALL to "it" is considered as a keyword. Examples: ``` public def it(x = "X") x end [1, 2, 3].map { it.to_s } #=> ["1", "2", "3"] [1, 2, 3].map { self.it } #=> ["X", "X", "X"] # a method call because of a receiver [1, 2, 3].map { it() } #=> ["X", "X", "X"] # a method call because of parentheses [1, 2, 3].map { it "Y" } #=> ["Y", "Y", "Y"] # a method call because of an argument [1, 2, 3].map { it="Y"; it } #=> ["Y", "Y", "Y"] # there is a variable named "it" in this scope it = "Z" [1, 2, 3].map { it.to_s } #=> ["Z", "Z", "Z"] # there is a variable named "it" in this scope ``` Pros: * it is the best word for the feature (according to @matsuda) * it is reasonably compatible; RSpec won't break because their "it" requires an argument Cons: * it actually brings incompatibility in some cases * it is somewhat fragile; "it" may refer a wrong variable * it makes the language semantics dirty Fortunately, it is easy to fix the incompatible programs: just replace `it` with `it()`. (Off topic: it is similar to `super()`.) Just inserting an assignment to a variable "it" may affect another code. This is a bad news, but, IMO, a variable named "it" is not so often used. If this proposal is accepted, I guess people will gradually avoid the variable name "it" (like "p"). The dirtiness is the most serious problem for me. Thus, I don't like my own proposal so much, honestly. But it would be much better than Perlish `@1`. (Note: I don't propose the removal of `@1` in this ticket. It is another topic.) In any way, I'd like to hear your opinions. An experimental patch is attached. The idea is inspired by @jeremyevans0's [proposal of `@`](https://bugs.ruby-lang.org/issues/15723#note-98). P.S. It would be easy to use `_` instead of `it`. I'm unsure which is preferable. ---Files-------------------------------- its.patch (4.92 KB) mame_its_proposal.patch (5.26 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: