SlideShare une entreprise Scribd logo
1  sur  6
power ruby
method_missing When you send a message to a Ruby object, Ruby looks for a method to invoke with the same name as the message you sent.  You can override method_missing anywhere along that method lookup path, and tell Ruby what to do when it can’t find a method. Developers often use this to create domain-specific languages (DSLs) in Ruby.
Ruby’s Method Lookup Path The current self object’s own instance methods.  In the list of instance methods that all objects of that class share  In each of the included modules of that class, in reverse order of inclusion The superclass  Then in the superclass’s included modules, all the way up until it reaches the class Object.  If it still can’t find a method, the very last place it looks is in the Kernel module, included in the class Object.  And there, if it comes up short, it calls method_missing http://www.thirdbit.net/articles/2007/08/01/10-things-you-should-know-about-method_missing/
class Thing   def method_missing(m, *args, &block)       puts "There's no method called #{m} here -- please try again."       puts "parameters = #{args.inspect}"   end   end   >> t = Thing.new >> t.anything("ddd",3) There's no method called anything here -- please try again. parameters = ["ddd", 3] => nil
Variable Arguments def test(*params)     params.each_with_index do |arg, i|         puts "#{i} #{arg}"      end end
Variable Arguments def test(a, b, *params)     params.each_with_index do |arg, i|         puts "#{i} #{arg}"      end end

Contenu connexe

Tendances (15)

Computer programming 2 Lesson 3
Computer programming 2  Lesson 3Computer programming 2  Lesson 3
Computer programming 2 Lesson 3
 
5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java
 
Introducing classes
Introducing classesIntroducing classes
Introducing classes
 
Code smells
Code smellsCode smells
Code smells
 
OOP Basic Concepts
OOP Basic ConceptsOOP Basic Concepts
OOP Basic Concepts
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Class
ClassClass
Class
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
On the path to become a jr. developer short version
On the path to become a jr. developer short versionOn the path to become a jr. developer short version
On the path to become a jr. developer short version
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
 
Oopsinphp
OopsinphpOopsinphp
Oopsinphp
 
Smali语法
Smali语法Smali语法
Smali语法
 
Intro Ruby Classes Part I
Intro Ruby Classes Part IIntro Ruby Classes Part I
Intro Ruby Classes Part I
 

En vedette

Sarah Allen Ignite 09
Sarah Allen Ignite 09Sarah Allen Ignite 09
Sarah Allen Ignite 09Sarah Allen
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentSarah Allen
 
Mobile Ruby, LA RubyConf
Mobile Ruby, LA RubyConfMobile Ruby, LA RubyConf
Mobile Ruby, LA RubyConfSarah Allen
 
RailsBridge Presentation
RailsBridge PresentationRailsBridge Presentation
RailsBridge PresentationSarah Allen
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersSarah Allen
 
Cross-Platform Mobile Apps with Ruby, MWRC
Cross-Platform Mobile Apps with Ruby, MWRCCross-Platform Mobile Apps with Ruby, MWRC
Cross-Platform Mobile Apps with Ruby, MWRCSarah Allen
 

En vedette (8)

Sarah Allen Ignite 09
Sarah Allen Ignite 09Sarah Allen Ignite 09
Sarah Allen Ignite 09
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Ruby Blocks
Ruby BlocksRuby Blocks
Ruby Blocks
 
Mobile Ruby, LA RubyConf
Mobile Ruby, LA RubyConfMobile Ruby, LA RubyConf
Mobile Ruby, LA RubyConf
 
RailsBridge Presentation
RailsBridge PresentationRailsBridge Presentation
RailsBridge Presentation
 
Ruby Blocks
Ruby BlocksRuby Blocks
Ruby Blocks
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
 
Cross-Platform Mobile Apps with Ruby, MWRC
Cross-Platform Mobile Apps with Ruby, MWRCCross-Platform Mobile Apps with Ruby, MWRC
Cross-Platform Mobile Apps with Ruby, MWRC
 

Similaire à Power ruby

Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview QuestionsSumanth krishna
 
6 Modules Inheritance Gems
6 Modules Inheritance Gems6 Modules Inheritance Gems
6 Modules Inheritance Gemsliahhansen
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaRafael Magana
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation javaJayasankarPR2
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview QestionsArun Vasanth
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyTushar Pal
 
Ruby OOP: Objects over Classes
Ruby OOP: Objects over ClassesRuby OOP: Objects over Classes
Ruby OOP: Objects over ClassesAman King
 
Ruby :: Training 1
Ruby :: Training 1Ruby :: Training 1
Ruby :: Training 1Pavel Tyk
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 eehomeworkping9
 
Ruby Metaprogramming - OSCON 2008
Ruby Metaprogramming - OSCON 2008Ruby Metaprogramming - OSCON 2008
Ruby Metaprogramming - OSCON 2008Brian Sam-Bodden
 
Object Oriented Programming - Polymorphism and Interfaces
Object Oriented Programming - Polymorphism and InterfacesObject Oriented Programming - Polymorphism and Interfaces
Object Oriented Programming - Polymorphism and InterfacesHabtamu Wolde
 
Lightning talk
Lightning talkLightning talk
Lightning talknpalaniuk
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APINiranjan Sarade
 
Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfGammingWorld2
 

Similaire à Power ruby (20)

Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
 
6 Modules Inheritance Gems
6 Modules Inheritance Gems6 Modules Inheritance Gems
6 Modules Inheritance Gems
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation java
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
 
Ruby OOP: Objects over Classes
Ruby OOP: Objects over ClassesRuby OOP: Objects over Classes
Ruby OOP: Objects over Classes
 
Ruby :: Training 1
Ruby :: Training 1Ruby :: Training 1
Ruby :: Training 1
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Ruby Metaprogramming - OSCON 2008
Ruby Metaprogramming - OSCON 2008Ruby Metaprogramming - OSCON 2008
Ruby Metaprogramming - OSCON 2008
 
Object Oriented Programming - Polymorphism and Interfaces
Object Oriented Programming - Polymorphism and InterfacesObject Oriented Programming - Polymorphism and Interfaces
Object Oriented Programming - Polymorphism and Interfaces
 
Iq rails
Iq railsIq rails
Iq rails
 
13 ruby modules
13 ruby modules13 ruby modules
13 ruby modules
 
Lightning talk
Lightning talkLightning talk
Lightning talk
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
Oop basic concepts
Oop basic conceptsOop basic concepts
Oop basic concepts
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
 
Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdf
 

Plus de Sarah Allen

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesSarah Allen
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)Sarah Allen
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical SkillSarah Allen
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government ServicesSarah Allen
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency WinsSarah Allen
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of ComputersSarah Allen
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software FunSarah Allen
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of TransparencySarah Allen
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for FunSarah Allen
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceSarah Allen
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead PeopleSarah Allen
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby Sarah Allen
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Sarah Allen
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?Sarah Allen
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentSarah Allen
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeSarah Allen
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future ThoughtsSarah Allen
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web VideoSarah Allen
 
Elementary Computer History
Elementary Computer HistoryElementary Computer History
Elementary Computer HistorySarah Allen
 

Plus de Sarah Allen (20)

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency Wins
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
 
Let's pretend
Let's pretendLet's pretend
Let's pretend
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product Development
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
 
Elementary Computer History
Elementary Computer HistoryElementary Computer History
Elementary Computer History
 

Power ruby

  • 2. method_missing When you send a message to a Ruby object, Ruby looks for a method to invoke with the same name as the message you sent. You can override method_missing anywhere along that method lookup path, and tell Ruby what to do when it can’t find a method. Developers often use this to create domain-specific languages (DSLs) in Ruby.
  • 3. Ruby’s Method Lookup Path The current self object’s own instance methods. In the list of instance methods that all objects of that class share In each of the included modules of that class, in reverse order of inclusion The superclass Then in the superclass’s included modules, all the way up until it reaches the class Object. If it still can’t find a method, the very last place it looks is in the Kernel module, included in the class Object. And there, if it comes up short, it calls method_missing http://www.thirdbit.net/articles/2007/08/01/10-things-you-should-know-about-method_missing/
  • 4. class Thing def method_missing(m, *args, &block) puts "There's no method called #{m} here -- please try again." puts "parameters = #{args.inspect}" end end >> t = Thing.new >> t.anything("ddd",3) There's no method called anything here -- please try again. parameters = ["ddd", 3] => nil
  • 5. Variable Arguments def test(*params) params.each_with_index do |arg, i| puts "#{i} #{arg}" end end
  • 6. Variable Arguments def test(a, b, *params) params.each_with_index do |arg, i| puts "#{i} #{arg}" end end