From: jonathan@... Date: 2019-07-07T21:04:03+00:00 Subject: [ruby-core:93594] [Ruby master Feature#15897] `it` as a default block parameter Issue #15897 has been updated by joallard (Jonathan Allard). Eregon (Benoit Daloze) wrote: > I think this ambiguity is fairly intuitive and easy to resolve: > > [...] if there are no arguments, it's always the variable/default block parameter, otherwise it's a method call with the given arguments. > > I think it's going to be extremely rare for `it` to be confusing in practice, once people know about the default block parameter feature. I would echo Benoit's comments here. As an avid RSpec user, I employ `it "does something"` without a block fairly often for prototyping. However, I virtually never use a completely naked `it`, no arguments or block, which seems to be the issue here. Even then, if I really need a naked 'it', I could always write `it()`. The following, taken from Jon Rowe's example above, makes total sense to me: ``` RSpec.describe do it "will do the thing" do it # without arguments, we're referring to a thing whose meaning is given by the context end it # meaning given by context: first block argument it() # empty, description-less example (not affected) it{} # also unaffected end ``` While I do empathize with our RSpec folks, I don't see the value in preventing an expressive keyword to exist (which would be useful in a lot of cases) in order to save the ability to use the empty `it`, which has a pretty limited use. Especially considering if one really needs the latter, they may just write `it()`, a simple workaround. The value of an expressive, language-based keyword is something I value and makes sense to me in Ruby's design based on natural language. With that said, I'd support finding an alternative keyword, but it seems that based on previous discussions, we've not been able to find one that makes better consensus. I haven't either. Besides, we could make similar criticisms about those other keywords (`item`, `this/that`, `one/other`, ...) ---------------------------------------- Feature #15897: `it` as a default block parameter https://bugs.ruby-lang.org/issues/15897#change-79186 * Author: mame (Yusuke Endoh) * Status: Open * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: ---------------------------------------- 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: