Skip to content

Instantly share code, notes, and snippets.

@digitalextremist
Forked from tarcieri/concurrent_nested_hash.rb
Last active December 16, 2015 08:29
Show Gist options
  • Save digitalextremist/5406223 to your computer and use it in GitHub Desktop.
Save digitalextremist/5406223 to your computer and use it in GitHub Desktop.
Oldie but a goodie, with more of the standard methods.
require 'celluloid'
class ConcurrentNestedHash
include Celluloid
def initialize( starting = {} ); @outer = starting end
def [](*keys); keys.inject(@outer) { |h,k| h[k] } end
def []=(*args)
value = args.pop
raise ArgumentError, "wrong number of arguments (1 for 2)" if args.empty?
key = args.pop
hash = args.inject(@outer) { |h,k| h[k] ||= {} }
hash[key] = value
end
def has_key?( key ); @outer.has_key? key end
def merge( h ); @outer.merge h end
def merge!( h ); @outer.merge! h end
def delete( key ); @outer.delete key end
def sort_by( &block ); @outer.sort_by &block end
def count; @outer.count end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment