power ruby<br />
method_missing<br />When you send a message to a Ruby object, Ruby looks for a method to invoke with the same name as the ...
Ruby’s Method Lookup Path<br />The current self object’s own instance methods. <br />In the list of instance methods that ...
class Thing<br />  def method_missing(m, *args, &block)  <br />    puts "There's no method called #{m} here -- please try ...
Variable Arguments<br />def test(*params)<br />    params.each_with_index do |arg, i|<br />        puts "#{i} #{arg}"<br /...
Variable Arguments<br />def test(a, b, *params)<br />    params.each_with_index do |arg, i|<br />        puts "#{i} #{arg}...
Upcoming SlideShare
Loading in...5
×

Power ruby

739

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
739
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Power ruby

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

    Clipping is a handy way to collect important slides you want to go back to later.

×