Sha256: 0dcd1e8720b8ace2fddc16858cfc902d348e1bd219a8bfb9432f215c3bfad3b0

Contents?: true

Size: 735 Bytes

Versions: 15

Compression:

Stored size: 735 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
  #
  # CREDIT: Trans

  def to_proc
    lambda do |o|
      self.each do |k,v|
        ke = "#{k}="
        o.__send__(ke, v)
      end
    end
  end

  # A fault-tolerent version of #to_proc.
  #
  # It works just like #to_proc, but the block will make
  # sure# the object responds to the assignment.
  #
  # CREDIT: Trans

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

end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
facets-2.8.2 lib/core/facets/hash/to_proc.rb
facets-2.8.1 lib/core/facets/hash/to_proc.rb
facets-2.8.0 lib/core/facets/hash/to_proc.rb
facets-2.7.0 lib/core/facets/hash/to_proc.rb
facets-2.6.0 lib/core/facets/hash/to_proc.rb
facets-2.4.0 lib/facets/hash/to_proc.rb
facets-2.4.1 lib/facets/hash/to_proc.rb
facets-2.4.4 lib/core/facets/hash/to_proc.rb
facets-2.4.2 lib/core/facets/hash/to_proc.rb
facets-2.4.3 lib/core/facets/hash/to_proc.rb
facets-2.5.0 lib/core/facets/hash/to_proc.rb
facets-2.4.5 lib/core/facets/hash/to_proc.rb
facets-2.5.1 lib/core/facets/hash/to_proc.rb
facets-2.5.2 lib/core/facets/hash/to_proc.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/hash/to_proc.rb