Skip to content

Instantly share code, notes, and snippets.

@alebian
Created May 28, 2018 16:09
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 alebian/0cff0ca5a97d4de61430a5c04a083d3c to your computer and use it in GitHub Desktop.
Save alebian/0cff0ca5a97d4de61430a5c04a083d3c to your computer and use it in GitHub Desktop.
class BasicStruct
ATTRIBUTES = %i[]
def initialize(options = {})
attributes = options.with_indifferent_access
self.class::ATTRIBUTES.each do |attribute|
instance_variable_set("@#{attribute}", attributes[attribute])
class_eval { attr_reader attribute }
end
end
def to_h
self.class::ATTRIBUTES.each_with_object({}) do |attribute, hash|
hash[attribute] = instance_variable_get("@#{attribute}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment