Skip to content

Instantly share code, notes, and snippets.

@codeprimate
Created March 9, 2014 02:50
Show Gist options
  • Save codeprimate/9442248 to your computer and use it in GitHub Desktop.
Save codeprimate/9442248 to your computer and use it in GitHub Desktop.
class FooStruct
def initialize(attrs)
# Create a Singleton Class (Anonymous Class) inheriting from FooStruct
klass = ( class << self; self; end )
# Loop through the attributes
attrs.each do |attr|
# Within the context of the anonymous class...
klass.class_eval do
# Define accessors for each of the attributes
attr_accessor attr.to_sym
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment