Add private method to Object and call it in instance class : object Creation « Class « Ruby






Add private method to Object and call it in instance class


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>

 








Related examples in the same category

1.Creating an Object
2.Alias method name