Search code examples
rubyconstructorinitializerattr-accessor

Ruby: Is there a gem with method attr_initialize?


Is there some popular ruby gem that defines attr_initialize class method, which behaves like following:

attr_initialize :attr1, :@attr2, [:@attr3, 1] do |arg4|
  post_initialization(arg4)
end

would eval to something equivalent to:

attr_accessor :attr1

def initialize(attr1, attr2, attr3 = 1, arg4 = nil)
  @attr1 = attr1
  @attr2 = attr2
  @attr3 = attr3
  post_initialization(arg4)
end

Solution

  • Our attr_extras is close.

    It intentionally doesn't handle default arguments, optional arguments or more than assignment – if you want that, you should probably just define a regular method.