Sha256: 2bbc876d910e73ec82ba15c6a950e9d0bc3310b03388ec1f73e5a9e366d5ab15

Contents?: true

Size: 600 Bytes

Versions: 10

Compression:

Stored size: 600 Bytes

Contents

class Hash

  # Convert a hash into a module.
  #
  #   {:a=>1, :b=>2}.to_mod
  #
  # Can take a block accepting key, value pairs which will be
  # evaluated in the context of the module.
  #
  #   h = {:a=>1, :b=>2}
  #   m = h.to_mod{ |k,v| module_function k }
  #   m.a #=> 1
  #   m.b #=> 2
  #
  # CREDIT: Jay Fields
  #--
  # TODO: Consider #to_obj?
  #++

  def to_mod(&block)
    hash = self
    Module.new do
      hash.each do |key, value|
        define_method key do
          value #.to_object
        end
        instance_exec(key, value, &block) if block
      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_mod.rb
facets-3.1.0 lib/core/facets/hash/to_mod.rb
facets-3.0.0 lib/core/facets/hash/to_mod.rb
facets-2.9.3 lib/core/facets/hash/to_mod.rb
facets-2.9.2 src/core/facets/hash/to_mod.rb
facets-2.9.2 lib/core/facets/hash/to_mod.rb
facets-2.9.1 lib/core/facets/hash/to_mod.rb
facets-2.9.0 lib/core/facets/hash/to_mod.rb
facets-2.9.0.pre.2 lib/core/facets/hash/to_mod.rb
facets-2.9.0.pre.1 lib/core/facets/hash/to_mod.rb