Created
May 8, 2014 11:52
-
-
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.
This file contains 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
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