Sha256: e793f08124c338c36478311d229b9840e75d4cce5bd102aa2645fb2f2693d2ce

Contents?: true

Size: 723 Bytes

Versions: 1

Compression:

Stored size: 723 Bytes

Contents

class Hash

  # Constructs a Proc object from a hash such that
  # the parameter of the Proc is assigned the hash
  # keys as attributes.
  #
  #   h = { :a => 1 }
  #   p = h.to_proc
  #   o = OpenStruct.new
  #   p.call(o)
  #   o.a  #=> 1
  #
  # If +response+ is set to +true+, then assignment
  # will only occur if receiver responds_to? the
  # writer method.
  #
  # CREDIT: Trans

  def to_proc(response=false)
    if response
      lambda do |o|
        self.each do |k,v|
          ke = "#{k}="
          o.__send__(ke, v) if respond_to?(ke)
        end
      end
    else
      lambda do |o|
        self.each do |k,v|
          ke = "#{k}="
          o.__send__(ke, v)
        end
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/hash/to_proc.rb