Thank you Mr. Darcy

Oct 07, 2008 17:42

I thought I'd seen almost everything, but this may take the cake. I was perusing my 'Learning Ruby' book last night and was skimming the section on reflection when I saw this piece of code.

class Meta
%w{ jane elizabeth mary kitty lydia }.each do |n|
define_method(n) { puts "My name is #{n.capitalize} Bennet." }
end
end

Meta.instance_methods - Object.instance_methods # => ["jane", "elizabeth", "mary", "kitty", "lydia"]

meta = Meta.new

meta.elizabeth # => My name is Elizabeth Bennet.

How friggin' weird is that!?
Previous post Next post
Up