The Methods Method and Its Friends (MOTM 2009.08)

507 views
468 views

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
507
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Continuing quest to lower the bar and discuss beginner-level topics in bite-sized chunks.
    irb
    Used methods method to choose a new MOTM...

  • Too Many! Instance methods, not Class methods!
    (The Method of Physick is the first textbook on medicine published in English)
  • Grep!
    Array of method names you can call on the instance.
  • Sort!
  • Though not in that order...
  • Starting with this class...
    Note 2 methods defined *at this level*.
  • Synonymous (more or less).
    Array of accessible method names you can call.
    With false!
    Maybe show ‘true’ result in irb here.
  • Subtraction instead of ‘false’!
  • Supposed to be synonymous, according to PickAxe...
    (Ruby 1.8.6, at least.)
    methods(false) acts like singleton_methods(false).
    Don’t delay too long - maybe for discussion at the bar later...
    irb demonstration?
    def motm.another_method
    puts ‘Another!’
    end

  • Note: didn’t have to pass false to protected_methods since String has none.

  • Note: now have to pass false.
  • Don’t rush this slide (LOTS!).
    http://www.rubyist.net/~slagell/ruby/singletonmethods.html
    “A method given only to a single object is called a singleton method.”


  • Don’t rush this slide (LOTS!).
    http://www.rubyist.net/~slagell/ruby/singletonmethods.html
    “A method given only to a single object is called a singleton method.”


  • Don’t rush this slide (LOTS!).
    http://www.rubyist.net/~slagell/ruby/singletonmethods.html
    “A method given only to a single object is called a singleton method.”


  • Don’t rush this slide (LOTS!).
    http://www.rubyist.net/~slagell/ruby/singletonmethods.html
    “A method given only to a single object is called a singleton method.”


  • These methods are only on this *instance*.
    http://osdir.com/ml/lang.ruby.core/2005-04/msg00006.html
    File is a good class for testing this stuff.
    singleton_methods(true) ~ “singleton_and_inherited_methods”
    "singleton_methods(true)" means "singleton methods on this object or,
    if the object is a Class, this object and its ancestral classes."
  • No methods on this *instance*.
  • Different than the other methods covered... (no array).
  • ri Object#methods
    http://www.ruby-doc.org/

  • 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

    ×