Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Methods Method and Its Friends (MOTM 2009.08)

588 views

Published on

Ruby Method of the Month 2009.08. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

The Methods Method and Its Friends (MOTM 2009.08)

  1. 1. THE METHODS METHOD AND ITS FRIENDS Method of the Month - Kevin Munc - @muncman
  2. 2. Class.methods.grep(/method/) => ["private_class_method", "method", "public_methods", "public_instance_methods", "method_defined?", "methods", "protected_instance_methods", "public_method_defined?", "singleton_methods", "private_instance_methods", "private_method_defined?", "protected_methods", "instance_method", "protected_method_defined?", "public_class_method", "instance_methods", "private_methods"]
  3. 3. o = Object.new => #<Object:0x39cd94> o.methods.grep(/method/) => ["method", "public_methods", "methods", "singleton_methods", "protected_methods", "private_methods"]
  4. 4. o.methods.grep(/method/).sort => ["method", "methods", "private_methods", "protected_methods", "public_methods", "singleton_methods"]
  5. 5. Method Methods Object#method Object#methods Object#public_methods Object#protected_methods Object#private_methods Object#singleton_methods
  6. 6. class Motm < String def first_method puts ‘First!’ end def second_method puts ‘Second!’ end end
  7. 7. #methods & #public_methods motm = Motm.new => "" motm.public_methods(false) => ["first_method", "second_method"]
  8. 8. #methods & #public_methods (motm.methods - "".methods).length => 2
  9. 9. Question for the Experts # Why this difference?!? motm.public_methods(true).length == motm.methods(true).length => true # but... motm.public_methods(false).length == motm.methods(false).length => false (2 vs. 0)
  10. 10. #protected_methods class Motm def protect_this puts 'Protected!' end protected :protect_this end
  11. 11. #protected_methods motm = Motm.new => "" motm.protected_methods(false) => ["protect_this"]
  12. 12. #private_methods class Motm def my_privates puts 'Private!' end private :my_privates end motm.private_methods(false) => ["my_privates"]
  13. 13. #singleton_methods 1. 2. 3. 4.
  14. 14. #singleton_methods 1. module Crb 2. def brigade puts ‘This’ end end 3. 4.
  15. 15. #singleton_methods 1. module Crb 2. class Motm def brigade def self.short puts ‘This’ puts ‘is’ end end end 3. end 4.
  16. 16. #singleton_methods 1. module Crb 2. class Motm def brigade def self.short puts ‘This’ puts ‘is’ end end end 3. motm = Motm.new end 4. def motm.part_one puts ‘Spinal’ end
  17. 17. #singleton_methods 1. module Crb 2. class Motm def brigade def self.short puts ‘This’ puts ‘is’ end end end 3. motm = Motm.new end 4. class << motm def motm.part_one include Crb puts ‘Spinal’ def part_two end puts ‘Tap’ end end
  18. 18. #singleton_methods Motm.singleton_methods(false) => ["short"] motm.singleton_methods => ["part_two", "brigade", "part_one"] motm.singleton_methods(false) => ["part_two", "part_one"]
  19. 19. #singleton_methods motm2 = Motm.new => #<Motm:0x578ce4> motm2.singleton_methods => []
  20. 20. #method meth = motm.method(:protect_this) => #<Method: MyClass#protect_this> meth.call Protected! => nil
  21. 21. QUESTIONS? ANSWERS? Photo Sources http://www.library.usyd.edu.au/libraries/rare/medicine/barrough.html http://www.dnrmusicstore.com/graphics/HL%20guitar.JPG http://img1.jurko.net/wall/paper/method_man_2.jpg

×