Class Class
In: lib/extensions/class.rb
Parent: Object

Methods

autoinit  

Public Instance methods

A shorthand for the common chore of assigning initialize’s parameters to instance variables. For example:

  class Circle

    attr_reader :radius, :location, :area

    autoinit(:radius, :location) do
      @area = Math::PI * @radius ** 2
    end

  end

A TypeError is raised unless all the arguments to autoinit are strings or symbols.

[Source]

# File lib/extensions/class.rb, line 31
    def autoinit(*args, &block) # :yield:
      unless args.all? { |a| Symbol === a or String === a }
        raise TypeError, "All arguments must be symbols or strings"
      end
      block = proc {} if block.nil?
      define_method(:__init_proc) { block }
      params = args.join(", ")
      vars = args.map { |a| "@#{a}" }.join(", ")

      code = %{
          def initialize(#{params})
            #{vars} = #{params}
            instance_eval(&__init_proc)
          end
        }
      class_eval code
    end

[Validate]