44uk
8/18/2012 - 6:00 AM

Ruby Class Initialize with attributes

Ruby Class Initialize with attributes

class Hoe
  # attr_accessor

  def initialize(attributes = nil)
    assign_attributes(attributes) if attributes             
    yield self if block_given?
  end

  def assign_attributes(new_attributes)
    new_attributes.each do |k, v|
      send("#{k.to_s}=", v) if respond_to?("#{k.to_s}=")
    end
  end
end