Delegation Pattern in Ruby

Blog ยป Delegation Pattern in Ruby

Posted on 04 Aug 2009 22:50

Sometimes I need to pass an object to an API. But my object does not have the required methods…. And I don't want go and change the 3rd party class definition of my object. In Ruby the solution is easy. Another scenario is I have classes that do parts of the job required. And you need to combine them. There are many ways of implementing the delegation pattern in Ruby:

   class Whatever  
     delegate :first_name, :last_name, :to => :@user  
 
     def initialize(user)  
       @user = user  
     end  
   end

If you like this page, please spread the word: diggdel.icio.usFacebook

You can contact me if you have questions or corrections.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License