ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "jeremyevans0 (Jeremy Evans)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:105600] [Ruby master Feature#13560] Module#attr_ methods return reasonable values
Date: Thu, 07 Oct 2021 20:42:53 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-94072.20211007204252.12680@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-13560.20170512201616.12680@ruby-lang.org

Issue #13560 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Open to Closed

Support for this was added in Ruby 3.0.

----------------------------------------
Feature #13560: Module#attr_ methods return reasonable values
https://bugs.ruby-lang.org/issues/13560#change-94072

* Author: dunrix (Damon Unrix)
* Status: Closed
* Priority: Normal
----------------------------------------
Hi,

I'm suggesting all `Module#attr_` [methods](http://ruby-doc.org/core-2.4.1/Module.html#method-i-attr_accessor) 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`](http://ruby-doc.org/core-2.4.1/Module.html#method-i-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
```




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

           reply	other threads:[~2021-10-07 20:42 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <redmine.issue-13560.20170512201616.12680@ruby-lang.org>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-94072.20211007204252.12680@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).