Ruby's method_missing

1,565 views
1,458 views

Published on

Just two slides on method_missing

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

  • Be the first to like this

No Downloads
Views
Total views
1,565
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby's method_missing

  1. 1. 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 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 />
  2. 2. 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 />

×