Skip to content

Instantly share code, notes, and snippets.

@lyndsysimon
Created September 6, 2017 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyndsysimon/3ff4e6df02702ef4b4565bf1012e4962 to your computer and use it in GitHub Desktop.
Save lyndsysimon/3ff4e6df02702ef4b4565bf1012e4962 to your computer and use it in GitHub Desktop.
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