Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Created February 5, 2019 11:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ujihisa/06137a27272b64f47a4def610a8465ab to your computer and use it in GitHub Desktop.
Save ujihisa/06137a27272b64f47a4def610a8465ab to your computer and use it in GitHub Desktop.
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
@ujihisa
Copy link
Author

ujihisa commented Feb 5, 2019

update_in { users[:ujihisa][:money] = 24 }

@ujihisa
Copy link
Author

ujihisa commented Feb 5, 2019

local_variable_setするときはthread safetyに注意

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