Skip to content

Instantly share code, notes, and snippets.

@jaredcwhite
Created January 7, 2021 01:04
Show Gist options
  • Save jaredcwhite/99ec81f4dda901bbbabe03fb2a18fdc1 to your computer and use it in GitHub Desktop.
Save jaredcwhite/99ec81f4dda901bbbabe03fb2a18fdc1 to your computer and use it in GitHub Desktop.
Custom object destructuring in Ruby 3
class TestPunning
attr_accessor :x, :y
def deconstruct_keys(keys)
values = {}
keys.each do |key|
values[key] = send(key)
end
values
end
end
punning = TestPunning.new
punning.x = 123
punning.y = "abc"
punning => {x:, y:}
# x == 123, y == "abc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment