-
-
Save lyndsysimon/3ff4e6df02702ef4b4565bf1012e4962 to your computer and use it in GitHub Desktop.
Ruby object: hashing public attributes
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 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