Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Created May 8, 2014 11:52
Show Gist options
  • Save ifyouseewendy/e5a6ceca136604ac8b50 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/e5a6ceca136604ac8b50 to your computer and use it in GitHub Desktop.
You have a method whose implementation steps are so common that they can safely be hidden away.
module CustomInitializers
def hash_initializer(*attribute_names)
define_method(:initialize) do |*args|
data = args.first
attribute_names.each do |attr|
instance_variable_set "@#{attr}", data[attr]
end
end
self.class_eval do
attr_reader *attribute_names
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment