Skip to content

Instantly share code, notes, and snippets.

@lyndsysimon
Created Sep 6, 2017
Embed
What would you like to do?
Ruby object: hashing public attributes
class MyClass
PUBLIC_ATTRS = [ :alpha, :bravo, :charlie ].freeze
attr_reader *PUBLIC_ATTRS
def to_h
PUBLIC_ATTRS.map do |var|
[var, instance_variable_get("@#{var}")]
end.to_h.with_indifferent_access
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment