Issue #14025 has been updated by frankpimenta (Frank Pimenta).


~~~ ruby
 module ObjectInitialization
    def self.included base
       base.class_eval do
         extend ClassMethods
       end
    end
    module ClassMethods
      def attr_initializer default_arguments={}
         raise RuntimeError.new "No args provided to ::init" if default_arguments.empty?

         @@__default_arguments__ = default_arguments

         class_eval do
           define_method :initialize do |args=nil|
             arguments = args.nil? ? @@__default_arguments__ :  @@__default_arguments__.merge(args)
             arguments.each_pair do |name, value|
               instance_variable_set "@#{name.to_s}", value
             end
           end
         end
      end
    end
  end

  class Object
    include ObjectInitialization
  end

  class Person
    attr_initializer name: 'person', age: 18, address: "somewhere"
  end

  p0 = Person.new
  p1 = Person.new name: "frank"
  p2 = Person.new name: "adam", profession: "developer"

~~~


----------------------------------------
Feature #14025: #initialize with ivars
https://bugs.ruby-lang.org/issues/14025#change-67308

* Author: frankpimenta (Frank Pimenta)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
For:

  class Person
    def initialize name = 'person'
      @name = name
    end
  end

I thought that the following would be nice:

class Person
  initialize :name
 end

class Person
  initialize name: 'person', age: 18
end

class Person
  def initialize @name="person", @age=18
    puts "initializing...something to run before"
  end
end

--
 B., Frank.



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>