Created
January 7, 2021 01:04
-
-
Save jaredcwhite/99ec81f4dda901bbbabe03fb2a18fdc1 to your computer and use it in GitHub Desktop.
Custom object destructuring in Ruby 3
This file contains hidden or 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
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