Posted By

narkisr on 03/21/09


Tagged

method ruby missing proxie


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Blank slate objects in Ruby


 / Published in: Ruby
 

Creating a object that doesn't have any methods in Ruby (regular objects inherit methods from Object class), useful for Proxies that use method missing (http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc).

  1. lass BlankSlate
  2. instance_methods.each { |m| undef_method m unless m =~ /^__/ }
  3. end
  4.  
  5. # All methods will be passed to method_missing
  6. class Proxy < BlankSlate
  7. def initialize(obj)
  8. @obj = obj
  9. end
  10.  
  11. def method_missing(sym, *args, &block)
  12. puts "Sending #{sym}(#{args.join(',')}) to obj"
  13. @obj.__send__(sym, *args, &block)
  14. end
  15. end

Report this snippet  

You need to login to post a comment.