SlideShare una empresa de 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

Más contenido relacionado

Similar 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
 

Similar 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
 

Más de Kevin Munc

Más 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

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

The Methods Method and Its Friends (MOTM 2009.08)

Notas del 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/