ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:69134] [Ruby trunk - Feature #11141] [Open] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
@ 2015-05-12  0:45 ` manolet
  2015-05-12  1:31 ` [ruby-core:69137] [Ruby trunk - Feature #11141] [Rejected] " matz
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manolet @ 2015-05-12  0:45 UTC (permalink / raw
  To: ruby-core

Issue #11141 has been reported by Arnold Roa.

----------------------------------------
Feature #11141:  new syntax suggestion for abbreviate definition on block parameters in order
https://bugs.ruby-lang.org/issues/11141

* Author: Arnold Roa
* Status: Open
* Priority: Normal
* Assignee: ruby-core
----------------------------------------
One of the most commons things I do in Ruby are small block definitions:

~~~
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 shorcut 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:

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

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

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

This syntax will allow us to use:

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

So:

~~~
 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:

~~~
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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69137] [Ruby trunk - Feature #11141] [Rejected] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
  2015-05-12  0:45 ` [ruby-core:69134] [Ruby trunk - Feature #11141] [Open] new syntax suggestion for abbreviate definition on block parameters in order manolet
@ 2015-05-12  1:31 ` matz
  2015-05-12  3:19 ` [ruby-core:69140] [Ruby trunk - Feature #11141] " nobu
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: matz @ 2015-05-12  1:31 UTC (permalink / raw
  To: ruby-core

Issue #11141 has been updated by Yukihiro Matsumoto.

Status changed from Open to Rejected

We cannot use $1 etc. as they are already taken for Regexp match.
Short hand notation for block parameter itself is a nice idea though.

Matz.


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

* Author: Arnold Roa
* Status: Rejected
* Priority: Normal
* Assignee: ruby-core
----------------------------------------
One of the most commons things I do in Ruby are small block definitions:

~~~
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 shorcut 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:

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

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

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

This syntax will allow us to use:

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

So:

~~~
 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:

~~~
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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69140] [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
  2015-05-12  0:45 ` [ruby-core:69134] [Ruby trunk - Feature #11141] [Open] new syntax suggestion for abbreviate definition on block parameters in order manolet
  2015-05-12  1:31 ` [ruby-core:69137] [Ruby trunk - Feature #11141] [Rejected] " matz
@ 2015-05-12  3:19 ` nobu
  2015-05-12 13:53 ` [ruby-core:69144] " manolet
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: nobu @ 2015-05-12  3:19 UTC (permalink / raw
  To: ruby-core

Issue #11141 has been updated by Nobuyoshi Nakada.

Description updated

What about `@1`?

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

* 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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69144] [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-05-12  3:19 ` [ruby-core:69140] [Ruby trunk - Feature #11141] " nobu
@ 2015-05-12 13:53 ` manolet
  2015-05-12 14:20 ` [ruby-core:69145] " nobu
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manolet @ 2015-05-12 13:53 UTC (permalink / raw
  To: ruby-core

Issue #11141 has been updated by Arnold Roa.


Yukihiro Matsumoto wrote:
> We cannot use `$1` etc. as they are already taken for `Regexp` match.
> Short hand notation for block parameter itself is a nice idea though.
> 
> Matz.

Yes, I just use $1 as an example to explain the idea, by no means I think $1 would be a good choice as $ is for global variables and $1..2..3 are reserved for Regexp. The idea I wanted to point here is the short-hand syntax. 

@1 is a good idea, but @ is used for instance variables. So I'm not sure if is the ideal.

On method definition we can use *args for multiple arguments, so what about *1, *2, *3?

~~~
x.method{ *1 - *2 }
~~~


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

* 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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69145] [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-05-12 13:53 ` [ruby-core:69144] " manolet
@ 2015-05-12 14:20 ` nobu
  2015-05-12 14:59   ` [ruby-core:69146] " Austin Ziegler
  2015-05-12 15:33 ` [ruby-core:69147] " mail
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 10+ messages in thread
From: nobu @ 2015-05-12 14:20 UTC (permalink / raw
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69146] Re: [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order
  2015-05-12 14:20 ` [ruby-core:69145] " nobu
@ 2015-05-12 14:59   ` Austin Ziegler
  0 siblings, 0 replies; 10+ messages in thread
From: Austin Ziegler @ 2015-05-12 14:59 UTC (permalink / raw
  To: Ruby developers

[-- Attachment #1: Type: text/plain, Size: 2701 bytes --]

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, <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 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

[-- Attachment #2: Type: text/html, Size: 3913 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69147] [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2015-05-12 14:20 ` [ruby-core:69145] " nobu
@ 2015-05-12 15:33 ` mail
  2015-05-13  6:34 ` [ruby-core:69162] " hanmac
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mail @ 2015-05-12 15:33 UTC (permalink / raw
  To: ruby-core

Issue #11141 has been updated by Jan Lelis.


Nobuyoshi Nakada wrote:
> What about `@1`?

I liked this one at first glance, but it might confuse people, because you could think the scoping would be similar to @instance variables.

Some brainstorming:

     ->{ puts $a, $b } # <- reasoning: $ does not mean global scope anyway

     ->{ puts 1st, 2nd } # <- interesting syntax trick

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

* 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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69162] [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2015-05-12 15:33 ` [ruby-core:69147] " mail
@ 2015-05-13  6:34 ` hanmac
  2015-05-13 15:04 ` [ruby-core:69165] " mail
  2015-05-14 10:54 ` [ruby-core:69196] " ko1
  8 siblings, 0 replies; 10+ messages in thread
From: hanmac @ 2015-05-13  6:34 UTC (permalink / raw
  To: ruby-core

Issue #11141 has been updated by Hans Mackowiak.


my problem i got with that new syntax is what does it do when i have blocks inside of blocks, specially with different arity count ...

like 

~~~
{key1 => value1, key2 => value2}.each { |key, value|
  [obj1, obj2, obj3].each { |obj|
    
  }
}
~~~

when using that syntax:

~~~
{key1 => value1, key2 => value2}.each {
  [obj1, obj2, obj3].each {
    puts p1, p2 # what are the values of p1 and what specially are the values of p2 ?
  }
}
~~~


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

* 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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69165] [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2015-05-13  6:34 ` [ruby-core:69162] " hanmac
@ 2015-05-13 15:04 ` mail
  2015-05-14 10:54 ` [ruby-core:69196] " ko1
  8 siblings, 0 replies; 10+ messages in thread
From: mail @ 2015-05-13 15:04 UTC (permalink / raw
  To: ruby-core

Issue #11141 has been updated by Jan Lelis.


Hans Mackowiak wrote:
> my problem i got with that new syntax is what does it do when i have blocks inside of blocks, specially with different arity count ...

I'd say, such a implicit block parameters should only be possible for the most inner block. Somehow accessing the arguments of an outer block probably does not make the code better readable. However, having simple (unnested) blocks with implicit variables *can* improve the readability, because it is more concise. Like, for example, it is already possible with gsub:

~~~
"Ruby".gsub(/(.)(.)/){ $1.downcase + $2.upcase } #=> "rUbY"
~~~

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

* 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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:69196] [Ruby trunk - Feature #11141] new syntax suggestion for abbreviate definition on block parameters in order
       [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2015-05-13 15:04 ` [ruby-core:69165] " mail
@ 2015-05-14 10:54 ` ko1
  8 siblings, 0 replies; 10+ messages in thread
From: ko1 @ 2015-05-14 10:54 UTC (permalink / raw
  To: ruby-core

Issue #11141 has been updated by Koichi Sasada.


FYI:
Kazuki Tanaka-san proposed a library Kasen.

[ruby-list:50120] [ANN] Kasen(下線) v0.1.1
Github: https://github.com/gogotanaka/_
Rubygems: https://rubygems.org/gems/kasen

It introduces special method "_" (underscore, Kasen in Japanese) and enable us to write such code.

```ruby
[1, 2, 3].map &(_ + 1).to_s

# means
[1, 2, 3].map { |n| (n + 1).to_s }
```

Matz said he favorites this idea.


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

* 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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-05-14 10:39 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-11141.20150512004557@ruby-lang.org>
2015-05-12  0:45 ` [ruby-core:69134] [Ruby trunk - Feature #11141] [Open] new syntax suggestion for abbreviate definition on block parameters in order manolet
2015-05-12  1:31 ` [ruby-core:69137] [Ruby trunk - Feature #11141] [Rejected] " matz
2015-05-12  3:19 ` [ruby-core:69140] [Ruby trunk - Feature #11141] " nobu
2015-05-12 13:53 ` [ruby-core:69144] " manolet
2015-05-12 14:20 ` [ruby-core:69145] " nobu
2015-05-12 14:59   ` [ruby-core:69146] " Austin Ziegler
2015-05-12 15:33 ` [ruby-core:69147] " mail
2015-05-13  6:34 ` [ruby-core:69162] " hanmac
2015-05-13 15:04 ` [ruby-core:69165] " mail
2015-05-14 10:54 ` [ruby-core:69196] " ko1

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).