Skip to content

Instantly share code, notes, and snippets.

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