Skip to content

Instantly share code, notes, and snippets.

@dminuoso
Last active November 6, 2017 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminuoso/86d1cc63a03af77803d18d14300a1898 to your computer and use it in GitHub Desktop.
Save dminuoso/86d1cc63a03af77803d18d14300a1898 to your computer and use it in GitHub Desktop.
class Hash
def self.setter(*args)
-> (paths, val, obj) {
obj.deep_dup.tap do |o|
*dir, final = paths
if dir.empty?
o.store(final, val)
else
o.dig(*dir).store(final, val)
end
end
}.curry[*args]
end
end
a = { foo: 23 }
Hash.setter [:foo], 42, a
p a #=> {:foo=>42}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment