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