[ruby-core:93198] [Ruby trunk Feature#15897] `it` as a default block parameter
From:
janosch84@...
Date:
2019-06-17 09:03:45 UTC
List:
ruby-core #93198
Issue #15897 has been updated by janosch-x (Janosch M=FCller).
sawa (Tsuyoshi Sawada) wrote:
> I propose to use a new keyword `item`.
I think that is a great proposal.
`it` is nice to read when passed to methods of other objects or when used w=
ith binary operators:
```ruby
strings.each { puts it }
pathnames.map { File.read it }
numbers.map { it + 2 }
```
unfortunately, `it` is quite awkward to read when calling its own methods (=
which is probably the more common case in Ruby):
```ruby
strings.each { it.chomp!('foo') }
pathnames.map { it.read }
numbers.map { it.next.next }
```
`item` works well for both cases:
```ruby
strings.each { puts item }
pathnames.map { File.read item }
numbers.map { item + 2 }
strings.each { item.chomp!('foo') }
pathnames.map { item.read }
numbers.map { item.next.next }
```
----------------------------------------
Feature #15897: `it` as a default block parameter
https://bugs.ruby-lang.org/issues/15897#change-78643
* 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 } #=3D> ["1", "2", "3"]
```
If you are familiar with Ruby's parser, this explanation is more useful: NO=
DE_VCALL to "it" is considered as a keyword.
Examples:
```
public def it(x =3D "X")
x
end
[1, 2, 3].map { it.to_s } #=3D> ["1", "2", "3"]
[1, 2, 3].map { self.it } #=3D> ["X", "X", "X"] # a method call because =
of a receiver
[1, 2, 3].map { it() } #=3D> ["X", "X", "X"] # a method call because =
of parentheses
[1, 2, 3].map { it "Y" } #=3D> ["Y", "Y", "Y"] # a method call because =
of an argument
[1, 2, 3].map { it=3D"Y"; it } #=3D> ["Y", "Y", "Y"] # there is a variable =
named "it" in this scope
it =3D "Z"
[1, 2, 3].map { it.to_s } #=3D> ["Z", "Z", "Z"] # there is a variable na=
med "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" require=
s 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. T=
his is a bad news, but, IMO, a variable named "it" is not so often used. I=
f this proposal is accepted, I guess people will gradually avoid the variab=
le name "it" (like "p").
The dirtiness is the most serious problem for me. Thus, I don't like my ow=
n 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 anoth=
er 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 pref=
erable.
---Files--------------------------------
its.patch (4.92 KB)
-- =
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>