Project

General

Profile

Actions

Feature #13560

closed

Module#attr_ methods return reasonable values

Added by dunrix (Damon Unrix) almost 7 years ago. Updated over 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:81129]

Description

Hi,

I'm suggesting all Module#attr_ methods should return array of generated method names, instead of useless nil.
Make them more concise across Module API, corresponding to logic of Module#define_method, allow pass as arguments to visibility public/protected/private methods etc.

Concerned method names:

  • attr_accessor
  • attr_reader
  • attr_writer

Demonstration example:

class LookBeforeYouLeap
  api_meths = attr_accessor :ruby_api
  # api_meths == [:ruby_api, :ruby_api=]
  private *api_meths

  # Or you can pass generated methods directly
  # protected *attr_reader(:ruby_papi, :ruby_cext)
end

# Assignment to temporary local variable `api_meths` does not pollute class'es
# namespace.

I'm aware attr_* methods also create corresponding instance variables, but Module API has no use for them, unlike created accessor methods.

In current state, where attr_* methods return nil, it makes class definition more prone to errors, especially at code refactoring:

class ExistingWay
  attr_accessor :ruby_api  # result is nil

  # Need write method names manually
  private :ruby_api, :rby_api=  # throws NameError exception when executed

  # Any change of generated accessor methods names require manual rewrite
  # at each visibility method call also.
end

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #11541: Let attr_accessor, _reader & _writer return symbols of the defined methodsClosedActions
Is duplicate of Ruby master - Feature #9453: Return symbols of defined methods for `attr` and friendsRejectedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0