From: Austin Ziegler Date: 2015-05-12T10:59:20-04:00 Subject: [ruby-core:69146] Re: [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order --001a11c11446747f000515e3bed6 Content-Type: text/plain; charset=ISO-8859-1 It gets a bit more line-noisy, but why not a couple of sigils? Maybe: `x.method { @[1] - @[2] }` On Tue, May 12, 2015 at 10:20 AM, wrote: > Issue #11141 has been updated by Nobuyoshi Nakada. > > > Arnold Roa wrote: > > On method definition we can use `*args` for multiple arguments, so what > about `*1`, `*2`, `*3`? > > > > ~~~ruby > > x.method{ *1 - *2 } > > ~~~ > > It has obvious ambiguity. > How will you interpret `foo(*1)`, a splat or the short-hand syntax? > > ---------------------------------------- > Feature #11141: new syntax suggestion for abbreviate definition on block > parameters in order > https://bugs.ruby-lang.org/issues/11141#change-52400 > > * Author: Arnold Roa > * Status: Rejected > * Priority: Normal > * Assignee: ruby-core > ---------------------------------------- > One of the most commons things I do in Ruby are small block definitions: > > ~~~ruby > x.each{|a| a} > ~~~ > > One useful syntax introduced was the **`&:method`** that allows calling a > method on a block if only one param is expected. It's a shortcut for > **`a.each{|x|x.method}`**. I think it would be nice if Ruby had a syntax > that allows me to not define the params that block would receive, but > instead access them in order. For example: > > ~~~ruby > x.each { $1 } > ~~~ > > Let's suppose the block is waiting for two params, I normally do: > > ~~~ruby > x.method {|a,b| a - b } > ~~~ > > This syntax will allow us to use: > > ~~~ruby > x.method{ $1 - $2 } > ~~~ > > So: > > ~~~ruby > x.each { p1.stg } > x.each {|p1| p1.stg} > x.each &:stg > ~~~ > > would be the same. > > Please consider `$1` and `$2` just as an example. I don't like the fact > that they are global variables. It could be `_1` or `p1`, for example: > > ~~~ruby > x.method{ p1 - p2 } > x.each{ p1 - p2 } == x.each {|p1, p2| p1 - p2 } > ~~~ > > Or, as blocks already uses **`&:method`** it could be **`&:1`**. Or any > other thing that you may consider more appropriated. > > I think this syntax would be very nice for short block definitions, the > downside is that it allows for bad practice on longer methods, but in the > end, that's a decision that a programer should make. > > Maybe this is not a valid reason, but I would like to point out that Regex > is actually creating global vars as the results of match: $x vars. (for > perl's historical reasons) > > So why not introduce this into Ruby's syntax? > > Personally I don't like either `$1` nor `p1`. They are just the first > quick things that come to my mind. > > > > > -- > https://bugs.ruby-lang.org/ > -- Austin Ziegler * halostatue@gmail.com * austin@halostatue.ca http://www.halostatue.ca/ * http://twitter.com/halostatue --001a11c11446747f000515e3bed6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
It gets a bit more line-noisy, but why not a couple of sig= ils? Maybe:

`x.method { @[1] - @[2] }`



On Tue, May 12, 2015 at 10:20 AM, <nobu@ruby-lang.org> wrote:
Issue #11141 has been updated = by Nobuyoshi Nakada.


Arnold Roa wrote:
> On method definition we can use `*args` for multiple arguments, so wha= t about `*1`, `*2`, `*3`?
>
> ~~~ruby
> x.method{ *1 - *2 }
> ~~~

It has obvious ambiguity.
How will you interpret `foo(*1)`, a splat or the short-hand syntax?

----------------------------------------
Feature #11141:  new syntax suggestion for abbreviate definition on bl= ock parameters in order
https://bugs.ruby-lang.org/issues/11141#change-52400

* Author: Arnold Roa
* Status: Rejected
* Priority: Normal
* Assignee: ruby-core
----------------------------------------
One of the most commons things I do in Ruby are small block definitions:
~~~ruby
x.each{|a| a}
~~~

One useful syntax introduced was the **`&:method`** that allows calling= a method on a block if only one param is expected. It's a shortcut for= **`a.each{|x|x.method}`**. I think it would be nice if Ruby had a syntax t= hat allows me to not define the params that block would receive, but instea= d access them in order. For example:

~~~ruby
x.each { $1 }
~~~

Let's suppose the block is waiting for two params, I normally do:

~~~ruby
x.method {|a,b| a - b }
~~~

This syntax will allow us to use:

~~~ruby
 x.method{ $1 - $2 }
~~~

So:

~~~ruby
 x.each { p1.stg }
 x.each {|p1| p1.stg}
 x.each &:stg
~~~

would be the same.

Please consider `$1` and `$2` just as an example. I don't like the fact= that they are global variables. It could be `_1` or `p1`, for example:

~~~ruby
x.method{ p1 - p2 }
x.each{ p1 - p2 } =3D=3D x.each {|p1, p2| p1 - p2 }
~~~

Or, as blocks already uses **`&:method`** it could be **`&:1`**. Or= any other thing that you may consider more appropriated.

I think this syntax would be very nice for short block definitions, the dow= nside is that it allows for bad practice on longer methods, but in the end,= that's a decision that a programer should make.

Maybe this is not a valid reason, but I would like to point out that Regex = is actually creating global vars as the results of match: $x vars. (for per= l's historical reasons)

So why not introduce this into Ruby's syntax?

Personally I don't like either `$1` nor `p1`. They are just the first q= uick things that come to my mind.




--
https://bugs.ruby= -lang.org/



--
=
--001a11c11446747f000515e3bed6--