[#92063] [Ruby trunk Misc#15723] Reconsider numbered parameters — zverok.offline@...
Issue #15723 has been updated by zverok (Victor Shepelev).
3 messages
2019/03/31
[ruby-core:91865] [Ruby trunk Feature#4475] default variable name for parameter
From:
shevegen@...
Date:
2019-03-18 11:42:48 UTC
List:
ruby-core #91865
Issue #4475 has been updated by shevegen (Robert A. Heiler).
I think @1 @2 is ok, syntax-wise; it reminds me of $1 $2 for regex matching.
The main two advantages I see is that it is short to type; and also easy
to remember.
There is only one thing that I would like to add and this came from examples
I saw elsewhere; in that the suggestion seems to allow us to completely
omit specifying parameters in blocks, like:
collection.each {|a, b, c|
}
versus
collection.each {
}
Can we use @1 @2 in both variants?
I think I may have had a slightly similar suggestion to the one listed
here above 8 years ago, but my thought was that we'd still have to
specify the names to the parameter (variables), so I thought we would
use something like:
collection.each {|long_name_a, long_name_b, long_name_c|
pp @1
pp @3
}
I do not mind either way, by the way - but when it comes to documentation
and also examples, I think it would help to mention it up front which
variant(s) people could use. The best may be to allow flexibility in both
variants, e. g. to specify the name of the parameters, but to also be able
to omit it, while still being able to use the positional targeting here,
such as through @1 @2 and so forth. And, if this is the case, to also
give an example or two in the main documentation for this functionality.
Thanks.
----------------------------------------
Feature #4475: default variable name for parameter
https://bugs.ruby-lang.org/issues/4475#change-77134
* Author: jordi (jordi polo)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version:
----------------------------------------
=begin
There is a very common pattern in Ruby:
object.method do |variable_name|
variable_name doing something
end
Many times in fact the name of the object is so self explanatory that we don't care about the name of the variable of the block. It is common to see things like :
@my_sons.each { |s| s.sell_to_someone }
or
Account.all.each { |a| my_account << a.money }
People tend to choose s or a because we have the class or the object name just there to remind you about the context.
I would like to know if can be a good idea to have a default name for that parameter. I think it is Groovy that does something like:
Account.all.each { my_account << it.money }
Where it is automagically filled and it doesn't need to be declared.
I think it is as readable or more (for newbies who don't know what is ||) and we save some typing :)
=end
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>