When to use Ruby DelegateClass instead of SimpleDelegator? (DelegateClass method vs. SimpleDelegator class)
Use subclass SimpleDelegator when you want an object that both has its own behavior and delegates to different objects during its lifetime.
Essentially saying use DelegateClass when the class you are creating is not going to get a different object.TempFile in Ruby is only going to decorate a File objectSimpleDelegator can be reused on different objects.
require 'delegate'class TicketSeller def sellTicket() 'Here is a ticket' endendclass NoTicketSeller def sellTicket() 'Sorry-come back tomorrow' endendclass TicketOffice < SimpleDelegator def initialize @seller = TicketSeller.new @noseller = NoTicketSeller.new super(@seller) end def allowSales(allow = true) __setobj__(allow ? @seller : @noseller) allow endendto = TicketOffice.newto.sellTicket » "Here is a ticket"to.allowSales(false) » falseto.sellTicket » "Sorry-come back tomorrow"to.allowSales(true) » trueto.sellTicket » "Here is a ticket"
Here is another good explanation a-delegate-matter