Add ability to seed/merge with a hash-like#10
Conversation
| # Each value is wrapped in a `proc` and registered under the key. | ||
| # Note that Canister doesn't differentiate between symbols and | ||
| # strings for keys, so if your hashlike has keys of, e.g. both | ||
| # `"a"` and `:a` it won't work. |
There was a problem hiding this comment.
I would recommend expanding on this and making explicit the fact that keys symbolizing to the same value will overwrite one another in unpredictable ways. It's hard to imagine a case in which this would be the expected or in any way legit usage so I would even put something like this in merge_from_hashlike
if hashlike.keys.map(&:to_sym).uniq.count != hashlike.count
raise "you're gonna lose info!"
end
| end | ||
| end | ||
| iter.each do |k, v| | ||
| blk = proc { v } |
There was a problem hiding this comment.
What if v is a Proc? I can pass a proc like canister.register(:foo, &my_proc) but in a hashlike my proc is just going to be stored, and returned, as a proc -- maybe not what I was expecting. This may be a can of worms re developer intent.
I forgot about the hello example above, which is an argument for leaving procs alone instead of trying to "dereference" them inside Canister. I retract this comment.
There was a problem hiding this comment.
Two comments I left inline, the one about symbol collisions should be addressed if only via an expanded comment. The second is worth thinking about and I'd welcome an opportunity to discuss it since we use the crap out of Canister but we don't often get a chance to talk about it.
A hash-like is defined in the code to be something that responds to either
#each_pairor both of#keysand#[].The PR allows you seed a call to
#newwith a hash-like, for putting in initial values that you maybe got from somewhere else. It also allows merging in a hash like (where the argument overwrites whatever was there).Canister itself fulfills the criteria for a hash-like, so you can also merge with another Canister.
Note that Canister (at least currently) doesn't allow numeric keys, so the incoming data can't, either.
[Also update rake version to address security update]