Created
April 14, 2012 02:06
-
-
Save mungruby/2381530 to your computer and use it in GitHub Desktop.
Metaprogramming 001
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
attributes = %w[name species] | |
MyClass = Class.new do | |
puts attributes | |
attr_accessor *attributes | |
define_method :initialize do |data| | |
puts attributes | |
puts data | |
attributes.each_with_index do |attribute, idx| | |
puts "#{idx} : #{attribute} : #{data[idx]}" | |
self.__send__ "#{attribute}=", data[idx] | |
end | |
end | |
end | |
v = MyClass.new(["Fred", "Wombat"]) | |
puts v.methods(false) | |
puts v.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment