Skip to content

Instantly share code, notes, and snippets.

@tarcieri
Created March 12, 2012 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tarcieri/2018986 to your computer and use it in GitHub Desktop.
Save tarcieri/2018986 to your computer and use it in GitHub Desktop.
require 'celluloid'
class ConcurrentNestedHash
include Celluloid
def initialize
@outer = {}
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
end
@digitalextremist
Copy link

@tarcieri, how can sort_by be carried over here? I know a block must be passed, often with 1 or 2 parameters on the block. I've tried several ways of passing a block, and parameters into the block, for this gist:

https://gist.github.com/digitalextremist/5406223

Latest attempt: def sort_by( *args ); @outer.sort_by yield( args ) end

@digitalextremist
Copy link

Wait I got it. def sort_by( &block ); @outer.sort_by &block end

Missed the second ampersand inside the method, not just in the definition (when passing a block and not *args).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment