Skip to content

Instantly share code, notes, and snippets.

@mungruby
Created April 14, 2012 02:06
Show Gist options
  • Save mungruby/2381530 to your computer and use it in GitHub Desktop.
Save mungruby/2381530 to your computer and use it in GitHub Desktop.
Metaprogramming 001
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