Add private method to Object and call it in instance class

2019-06-19 20:04栏目:技术创新
TAG:

class Object
  private
  def set_instance_variables(binding, *variables)
    variables.each do |var|
      eval('@#{var} = #{var}', binding)
    end
    #instance_variable_set('@#{var}', var)
  end
end

class RGBColor
  def initialize(red=0, green=0, blue=0)
    set_instance_variables(binding, *local_variables)
  end
end

RGBColor.new(10, 200, 300)
# => #<RGBColor:0xb7c22fc8 @red=10, @blue=300, @green=200>

版权声明:本文由新浦京娱乐站_新浦京娱乐场官网_新浦京娱乐发布于技术创新,转载请注明出处:Add private method to Object and call it in instance class