Forked from tarcieri/concurrent_nested_hash.rb
Last active
December 16, 2015 08:29
-
-
Save digitalextremist/5406223 to your computer and use it in GitHub Desktop.
Oldie but a goodie, with more of the standard methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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