Blank slate objects in Ruby


/ Published in: Ruby
Save to your folder(s)

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).


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.