require 'facets/core/hash/to_h'
require 'facets/core/proc/to_h'

module Kernel

  # Similiar to #set_with, but ignores missing setters.

  def populate( data=nil, &yld )

    if data
      data.to_h.each do |k,v|
        send( "#{k}=", v ) rescue nil
      end
    end

    if yld
      yld.to_h.each do |k,v|
        send( "#{k}=", v ) rescue nil
      end
    end

    # If the context of the error could be known
    # this could be used instead of converting the
    # block to a hash.

    #begin
    #  yield self
    #rescue NoMethodError => e
    #  if e.context == self and e.name.to_s =~ /=$/
    #    resume
    #  else
    #    raise e
    #  end
    #end

    self
  end

end
