SlideShare uma empresa Scribd logo
1 de 21
THE METHODS METHOD
  AND ITS FRIENDS
   Method of the Month - Kevin Munc - @muncman
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"]
o = Object.new
         => #<Object:0x39cd94>


        o.methods.grep(/method/)
=> ["method", "public_methods", "methods",
"singleton_methods", "protected_methods",
           "private_methods"]
o.methods.grep(/method/).sort
=> ["method", "methods", "private_methods",
  "protected_methods", "public_methods",
           "singleton_methods"]
Method Methods
Object#method

Object#methods

Object#public_methods

Object#protected_methods

Object#private_methods

Object#singleton_methods
class Motm < String

 def first_method
  puts ‘First!’
 end

 def second_method
  puts ‘Second!’
 end

end
#methods &
      #public_methods

        motm = Motm.new
               => ""
    motm.public_methods(false)
=> ["first_method", "second_method"]
#methods &
     #public_methods


(motm.methods - "".methods).length
              => 2
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)
#protected_methods
   class Motm

    def protect_this
     puts 'Protected!'
    end

    protected :protect_this

   end
#protected_methods
      motm = Motm.new
             => ""


 motm.protected_methods(false)
      => ["protect_this"]
#private_methods
  class Motm
    def my_privates
     puts 'Private!'
    end
    private :my_privates
  end

  motm.private_methods(false)
      => ["my_privates"]
#singleton_methods
1.           2.




     3.           4.
#singleton_methods
1. module Crb      2.
    def brigade
     puts ‘This’
    end
   end 3.               4.
#singleton_methods
1. module Crb      2. class Motm
    def brigade         def self.short
     puts ‘This’         puts ‘is’
    end                 end
   end 3.             end     4.
#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
#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
#singleton_methods
   Motm.singleton_methods(false)
           => ["short"]

      motm.singleton_methods
=> ["part_two", "brigade", "part_one"]

   motm.singleton_methods(false)
    => ["part_two", "part_one"]
#singleton_methods
  motm2 = Motm.new
 => #<Motm:0x578ce4>


motm2.singleton_methods
         => []
#method
meth = motm.method(:protect_this)
=> #<Method: MyClass#protect_this>


             meth.call
            Protected!
              => nil
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

Mais conteúdo relacionado

Semelhante a The Methods Method and Its Friends (MOTM 2009.08)

Metaprogramming 101
Metaprogramming 101Metaprogramming 101
Metaprogramming 101
Nando Vieira
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the Trenches
Xavier Noria
 
Benchmarking Perl Lightning Talk (NPW 2007)
Benchmarking Perl Lightning Talk (NPW 2007)Benchmarking Perl Lightning Talk (NPW 2007)
Benchmarking Perl Lightning Talk (NPW 2007)
brian d foy
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
Nando Vieira
 

Semelhante a The Methods Method and Its Friends (MOTM 2009.08) (20)

Dsl
DslDsl
Dsl
 
Metaprogramming 101
Metaprogramming 101Metaprogramming 101
Metaprogramming 101
 
Deciphering the Ruby Object Model
Deciphering the Ruby Object ModelDeciphering the Ruby Object Model
Deciphering the Ruby Object Model
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Ruby tricks2
Ruby tricks2Ruby tricks2
Ruby tricks2
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming Rails
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Metaprogramming and Folly
Metaprogramming and FollyMetaprogramming and Folly
Metaprogramming and Folly
 
Securing Rails Keep Ruby Weird
Securing Rails Keep Ruby WeirdSecuring Rails Keep Ruby Weird
Securing Rails Keep Ruby Weird
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the Trenches
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
 
Benchmarking Perl Lightning Talk (NPW 2007)
Benchmarking Perl Lightning Talk (NPW 2007)Benchmarking Perl Lightning Talk (NPW 2007)
Benchmarking Perl Lightning Talk (NPW 2007)
 
Metaprogramming code-that-writes-code
Metaprogramming code-that-writes-codeMetaprogramming code-that-writes-code
Metaprogramming code-that-writes-code
 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)
 
Lightning talk
Lightning talkLightning talk
Lightning talk
 
The Black Magic of Ruby Metaprogramming
The Black Magic of Ruby MetaprogrammingThe Black Magic of Ruby Metaprogramming
The Black Magic of Ruby Metaprogramming
 
Mock it right! A beginner’s guide to world of tests and mocks, Maciej Polańczyk
Mock it right! A beginner’s guide to world of tests and mocks, Maciej PolańczykMock it right! A beginner’s guide to world of tests and mocks, Maciej Polańczyk
Mock it right! A beginner’s guide to world of tests and mocks, Maciej Polańczyk
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
 
Steady with ruby
Steady with rubySteady with ruby
Steady with ruby
 

Mais de Kevin Munc

Mais de Kevin Munc (15)

Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013
 
NaN, Zero, & Infinities
NaN, Zero, & InfinitiesNaN, Zero, & Infinities
NaN, Zero, & Infinities
 
Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setInterval
 
Shellwords
ShellwordsShellwords
Shellwords
 
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)
 
Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)
 
Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)
 
Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 

The Methods Method and Its Friends (MOTM 2009.08)

Notas do Editor

  1. Continuing quest to lower the bar and discuss beginner-level topics in bite-sized chunks. irb Used methods method to choose a new MOTM...
  2. Too Many! Instance methods, not Class methods! (The Method of Physick is the first textbook on medicine published in English)
  3. Grep! Array of method names you can call on the instance.
  4. Sort!
  5. Though not in that order...
  6. Starting with this class... Note 2 methods defined *at this level*.
  7. Synonymous (more or less). Array of accessible method names you can call. With false! Maybe show &amp;#x2018;true&amp;#x2019; result in irb here.
  8. Subtraction instead of &amp;#x2018;false&amp;#x2019;!
  9. Supposed to be synonymous, according to PickAxe... (Ruby 1.8.6, at least.) methods(false) acts like singleton_methods(false). Don&amp;#x2019;t delay too long - maybe for discussion at the bar later... irb demonstration? def motm.another_method puts &amp;#x2018;Another!&amp;#x2019; end
  10. Note: didn&amp;#x2019;t have to pass false to protected_methods since String has none.
  11. Note: now have to pass false.
  12. Don&amp;#x2019;t rush this slide (LOTS!). http://www.rubyist.net/~slagell/ruby/singletonmethods.html &amp;#x201C;A method given only to a single object is called a singleton method.&amp;#x201D;
  13. Don&amp;#x2019;t rush this slide (LOTS!). http://www.rubyist.net/~slagell/ruby/singletonmethods.html &amp;#x201C;A method given only to a single object is called a singleton method.&amp;#x201D;
  14. Don&amp;#x2019;t rush this slide (LOTS!). http://www.rubyist.net/~slagell/ruby/singletonmethods.html &amp;#x201C;A method given only to a single object is called a singleton method.&amp;#x201D;
  15. Don&amp;#x2019;t rush this slide (LOTS!). http://www.rubyist.net/~slagell/ruby/singletonmethods.html &amp;#x201C;A method given only to a single object is called a singleton method.&amp;#x201D;
  16. 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) ~ &amp;#x201C;singleton_and_inherited_methods&amp;#x201D; &quot;singleton_methods(true)&quot; means &quot;singleton methods on this object or, if the object is a Class, this object and its ancestral classes.&quot;
  17. No methods on this *instance*.
  18. Different than the other methods covered... (no array).
  19. ri Object#methods http://www.ruby-doc.org/