Skip to content

Instantly share code, notes, and snippets.

@molawson
Created December 7, 2023 17:30
Show Gist options
  • Save molawson/436c5e86da8fc1b4de41575a48726a40 to your computer and use it in GitHub Desktop.
Save molawson/436c5e86da8fc1b4de41575a48726a40 to your computer and use it in GitHub Desktop.
Destructuring Ruby objects with pattern matching
class Thing
attr_reader :name, :type
def initialize(name, type)
@name = name
@type = type
end
def deconstruct_keys(_keys)
{ name:, type: }
end
end
thing = Thing.new("foo", "bar")
thing => { name:, type: }
name # => "foo"
type # => "bar
@molawson
Copy link
Author

molawson commented Dec 7, 2023

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