barsoom / attr_extras

Takes some boilerplate out of Ruby with methods like attr_initialize.
MIT License
560 stars 31 forks source link

Ruby 3.0 and support for chaining `private` with `attr_*` methods #43

Open radarek opened 2 years ago

radarek commented 2 years ago

Hello.

I'm not familiar with this library (I only scanned the README) but I think you might be interested in the fact, that as of Ruby 3.0, it supports chaining private call with methods that defines attribute accessors (I implemented this, see https://bugs.ruby-lang.org/issues/17314). Having this feature, it is unnecessary to have method like attr_private and I think it would be good idea to mention that in the README file.

Here is example usage.

class Foo
  private attr_accessor :foo, :bar
  # this the same as:
  attr_accessor :foo, :bar
  private :foo, :foo=, :bar, :bar=
end

This works with any method which returns defined method name (symbol) or defined method names (array of symbols). As of Ruby 3.0 attr_{reader,writer,accessor} returns array of defined method names:

class Foo
  p attr_accessor :foo, :bar #=> [:foo, :foo=, :bar, :bar=]
end