public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class User
def initiaize(profile)
@profile = profile
end
%w(doctor patient).each do |profile_type|
self.send(:define_method, "#{profile_type}?".to_sym) do
return @profile == profile_type
end
end
 
end
 
# Some of the benefits of using this approach to meta-programming are:
# * get respond_to?(:doctor?) works as expected
# * speed improvements as the methods are defined at compile time rather than the check logic run every time method_missing is run
# * In my opinion, cleaner code.
# Some other ideas that I agree with when it comes to meta-prgramming: http://patshaughnessy.net/2011/12/20/learning-from-the-masters-part-2-three-metaprogramming-best-practices

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.