Sha256: 7c9b768f71dde88dd49cef23f6e8aa290a63112f49f55048090dd301f2257eaa

Contents?: true

Size: 754 Bytes

Versions: 10

Compression:

Stored size: 754 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.
  #
  #   c = Class.new do
  #     attr_accessor :a
  #   end
  #
  #   h = {:a => 1}
  #   o = c.new
  #   h.to_proc.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

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/hash/to_proc.rb
facets-3.1.0 lib/core/facets/hash/to_proc.rb
facets-3.0.0 lib/core/facets/hash/to_proc.rb
facets-2.9.3 lib/core/facets/hash/to_proc.rb
facets-2.9.2 src/core/facets/hash/to_proc.rb
facets-2.9.2 lib/core/facets/hash/to_proc.rb
facets-2.9.1 lib/core/facets/hash/to_proc.rb
facets-2.9.0 lib/core/facets/hash/to_proc.rb
facets-2.9.0.pre.2 lib/core/facets/hash/to_proc.rb
facets-2.9.0.pre.1 lib/core/facets/hash/to_proc.rb