SlideShare ist ein Scribd-Unternehmen logo
1 von 8
POWER RUBY




   Sarah Allen
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 ïŹnd a method.

Developers often use this to create domain-speciïŹc
languages (DSLs) in Ruby.
RUBY’S METHOD LOOKUP
              PATH
1.   The current self object’s own instance methods.
2.   In the list of instance methods that all objects of that class share
3.   In each of the included modules of that class, in reverse order of
     inclusion
4.   The superclass
5.   Then in the superclass’s included modules, all the way up until it
     reaches the class Object.
6.   If it still can’t ïŹnd a method, the very last place it looks is in the
     Kernel module, included in the class Object.
7.   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
HASH ARGUMENTS

def test(options)
   a = options[:a]
   b = options[:b]
end
test(:a => 1, :b =>2)
test({:a => 1, :b =>2})
test :a => 1, :b =>2
test {:a => 1, :b =>2}
HASH ARGUMENTS

def test(name, options)
   a = options[:a]
   b = options[:b]
end
test("foo", :a => 1, :b =>2)
test("foo", {:a => 1, :b =>2})
test "foo", :a => 1, :b =>2
test "foo", {:a => 1, :b =>2}

Weitere Àhnliche Inhalte

Was ist angesagt?

Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
Matlab 4
Matlab 4Matlab 4
Matlab 4
ak2011031
 
learn Ruby in AMC Square learning
learn Ruby in AMC Square learninglearn Ruby in AMC Square learning
learn Ruby in AMC Square learning
ASIT Education
 

Was ist angesagt? (19)

Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
The Awesome Python Class Part-5
The Awesome Python Class Part-5The Awesome Python Class Part-5
The Awesome Python Class Part-5
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
Object Oriented Principles
Object Oriented PrinciplesObject Oriented Principles
Object Oriented Principles
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 
#6- Arrays and Collections Framework
#6- Arrays and Collections Framework#6- Arrays and Collections Framework
#6- Arrays and Collections Framework
 
The Awesome Python Class Part-4
The Awesome Python Class Part-4The Awesome Python Class Part-4
The Awesome Python Class Part-4
 
Exam 1 Review CS 1803
Exam 1 Review CS 1803Exam 1 Review CS 1803
Exam 1 Review CS 1803
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3
 
.Net F# Generic class
.Net F# Generic class.Net F# Generic class
.Net F# Generic class
 
Lect10
Lect10Lect10
Lect10
 
Les08
Les08Les08
Les08
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Javascript: The good parts for humans (part 2)
Javascript: The good parts for humans (part 2)Javascript: The good parts for humans (part 2)
Javascript: The good parts for humans (part 2)
 
Matlab 4
Matlab 4Matlab 4
Matlab 4
 
learn Ruby in AMC Square learning
learn Ruby in AMC Square learninglearn Ruby in AMC Square learning
learn Ruby in AMC Square learning
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 

Andere mochten auch

The Breakthroughs
The BreakthroughsThe Breakthroughs
The Breakthroughs
clarissebai
 

Andere mochten auch (6)

20320140503006
2032014050300620320140503006
20320140503006
 
HostingCon 2011- How Not Just to Survive but Thrive in the Evolving Hosting M...
HostingCon 2011- How Not Just to Survive but Thrive in the Evolving Hosting M...HostingCon 2011- How Not Just to Survive but Thrive in the Evolving Hosting M...
HostingCon 2011- How Not Just to Survive but Thrive in the Evolving Hosting M...
 
The Breakthroughs
The BreakthroughsThe Breakthroughs
The Breakthroughs
 
College Parents Humor: From Acceptance through Move-In
College Parents Humor: From Acceptance through Move-InCollege Parents Humor: From Acceptance through Move-In
College Parents Humor: From Acceptance through Move-In
 
Intro to J Ruby
Intro to J RubyIntro to J Ruby
Intro to J Ruby
 
45 lessons in life
45 lessons in life45 lessons in life
45 lessons in life
 

Ähnlich wie Power Ruby

Power ruby
Power rubyPower ruby
Power ruby
Sarah Allen
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
Chamnap Chhorn
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
tutorialsruby
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
tutorialsruby
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
Metaprogramming 101
Metaprogramming 101Metaprogramming 101
Metaprogramming 101
Nando Vieira
 
Object-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and MooseObject-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and Moose
Dave Cross
 

Ähnlich wie Power Ruby (20)

Power ruby
Power rubyPower ruby
Power ruby
 
11 ruby methods
11 ruby methods11 ruby methods
11 ruby methods
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
 
7 Methods and Functional Programming
7  Methods and Functional Programming7  Methods and Functional Programming
7 Methods and Functional Programming
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
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
 
The Ring programming language version 1.5.1 book - Part 70 of 180
The Ring programming language version 1.5.1 book - Part 70 of 180The Ring programming language version 1.5.1 book - Part 70 of 180
The Ring programming language version 1.5.1 book - Part 70 of 180
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196
 
Metaprogramming 101
Metaprogramming 101Metaprogramming 101
Metaprogramming 101
 
Ruby_Basic
Ruby_BasicRuby_Basic
Ruby_Basic
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)Prototype Utility Methods(1)
Prototype Utility Methods(1)
 
The Ring programming language version 1.3 book - Part 56 of 88
The Ring programming language version 1.3 book - Part 56 of 88The Ring programming language version 1.3 book - Part 56 of 88
The Ring programming language version 1.3 book - Part 56 of 88
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
Object-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and MooseObject-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and Moose
 
Designing Ruby APIs
Designing Ruby APIsDesigning Ruby APIs
Designing Ruby APIs
 
Ruby Programming
Ruby ProgrammingRuby Programming
Ruby Programming
 

Mehr von Blazing Cloud

RSpec Quick Reference
RSpec Quick ReferenceRSpec Quick Reference
RSpec Quick Reference
Blazing Cloud
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
Blazing Cloud
 
Routes Controllers
Routes ControllersRoutes Controllers
Routes Controllers
Blazing Cloud
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Blazing Cloud
 

Mehr von Blazing Cloud (20)

Rails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_manyRails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_many
 
Active Record Introduction - 3
Active Record Introduction - 3Active Record Introduction - 3
Active Record Introduction - 3
 
Rails Class Intro - 1
Rails Class Intro - 1 Rails Class Intro - 1
Rails Class Intro - 1
 
Your first rails app - 2
 Your first rails app - 2 Your first rails app - 2
Your first rails app - 2
 
RSpec Quick Reference
RSpec Quick ReferenceRSpec Quick Reference
RSpec Quick Reference
 
Extending rails
Extending railsExtending rails
Extending rails
 
2day Ruby Class Intro
2day Ruby Class Intro2day Ruby Class Intro
2day Ruby Class Intro
 
Mobile Lean UX
Mobile Lean UXMobile Lean UX
Mobile Lean UX
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
 
Interactive Graphics w/ Javascript, HTML5 and CSS3
Interactive Graphics w/ Javascript, HTML5 and CSS3Interactive Graphics w/ Javascript, HTML5 and CSS3
Interactive Graphics w/ Javascript, HTML5 and CSS3
 
Form helpers
Form helpersForm helpers
Form helpers
 
Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)
 
What you don't know (yet)
What you don't know (yet)What you don't know (yet)
What you don't know (yet)
 
Introduction to Rails
Introduction to RailsIntroduction to Rails
Introduction to Rails
 
ActiveRecord
ActiveRecordActiveRecord
ActiveRecord
 
Ruby on Rails Class intro
Ruby on Rails Class introRuby on Rails Class intro
Ruby on Rails Class intro
 
Ruby on rails toolbox
Ruby on rails toolboxRuby on rails toolbox
Ruby on rails toolbox
 
Routes Controllers
Routes ControllersRoutes Controllers
Routes Controllers
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Active Record
Active RecordActive Record
Active Record
 

KĂŒrzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

KĂŒrzlich hochgeladen (20)

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
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Power Ruby

  • 1. POWER RUBY Sarah Allen
  • 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 ïŹnd a method. Developers often use this to create domain-speciïŹc languages (DSLs) in Ruby.
  • 3. RUBY’S METHOD LOOKUP PATH 1. The current self object’s own instance methods. 2. In the list of instance methods that all objects of that class share 3. In each of the included modules of that class, in reverse order of inclusion 4. The superclass 5. Then in the superclass’s included modules, all the way up until it reaches the class Object. 6. If it still can’t ïŹnd a method, the very last place it looks is in the Kernel module, included in the class Object. 7. 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
  • 7. HASH ARGUMENTS def test(options) a = options[:a] b = options[:b] end test(:a => 1, :b =>2) test({:a => 1, :b =>2}) test :a => 1, :b =>2 test {:a => 1, :b =>2}
  • 8. HASH ARGUMENTS def test(name, options) a = options[:a] b = options[:b] end test("foo", :a => 1, :b =>2) test("foo", {:a => 1, :b =>2}) test "foo", :a => 1, :b =>2 test "foo", {:a => 1, :b =>2}

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n