Feature #4475

default variable name for parameter

Added by jordi polo about 3 years ago. Updated over 1 year ago.

[ruby-core:<unknown>]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:next minor

Description

=begin

There is a very common pattern in Ruby:

object.method do |variablename|
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 :

@mysons.each { |s| s.sellto_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

History

#1 Updated by Tom Wardrop about 3 years ago

=begin
I like the suggestion. The magic variable I'd use for this pattern would be 'this'. For example:

posts.each { this.author = 'Santa Clause' }

The rule would be: The first argument of any block would be accessible from within the block through the special variable 'this'.
=end

#2 Updated by Hiroshi Nakamura about 2 years ago

  • Description updated (diff)
  • Category set to core
  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

#3 Updated by Yusuke Endoh over 1 year ago

  • Target version set to next minor

Also available in: Atom PDF