Sha256: ca247b0c1d6db842abe49f9ea54a38df7944fb6e8ea2617891155b05f50d8911

Contents?: true

Size: 655 Bytes

Versions: 1

Compression:

Stored size: 655 Bytes

Contents

class FunctionalHash < Hash
  def [](*args)
    key = args.shift
    if fetch(key).is_a? Proc
      if fetch(key).arity == 0
        fetch(key).call
      else
        args.unshift(self)
        fetch(key).call(*args)
      end
    else
      super(key)
    end
  end

  def self.enable!
    unless Hash.respond_to? :fn
      Hash.class_eval do
        def fn
          FunctionalHash.new.tap do |fh|
            self.each do |key, val|
              fh[key] = val
            end
          end
        end
      end
    end
  end

  def self.disable!
    if Hash.instance_methods.include? :fn
      Hash.class_eval { undef_method :fn }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
functional_hash-0.2.0 lib/functional_hash.rb