Created
February 5, 2019 11:12
-
-
Save ujihisa/06137a27272b64f47a4def610a8465ab to your computer and use it in GitHub Desktop.
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
def give_ujihisa_money(users, value) | |
# users[:ujihisa][:money] = 24 | |
update_in2(-> { users[:ujihisa][:money] }, 24) | |
# put_in(users[:ujihisa][:money], 24) | |
# dig <-> bury | |
end | |
def update_in2(f, value) | |
pp RubyVM::InstructionSequence.of(f).to_a | |
nil | |
end | |
def update_in(hash, keys, value) | |
if keys.empty? | |
value | |
else | |
key, *keys = *keys | |
hash.merge(key => update_in(hash[key], keys, value)) | |
end | |
end | |
users = {ujihisa: {money: 23}.freeze, joker: {money: 0}.freeze}.freeze | |
users = give_ujihisa_money(users, 24) | |
p users | |
local_variable_setするときはthread safetyに注意
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
update_in { users[:ujihisa][:money] = 24 }