Over a million developers have joined DZone.

Delegation Without Method_missing

·
With ActiveSupport we get a clean and easy way to implement the delegation pattern:

class QueueManager

  attr_accessor :queue

  # Delegates some methods to the internal queue
  delegate :size, :clear, :to => :queue

  def intialize
    self.queue = []
  end

end

m = QueueManager.new
m.size
# => 0
m.clear
# => []

More info: 
- http://www.simonecarletti.com/blog/2009/12/inside-ruby-on-rails-delegate/
- http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/module/delegation.rb
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}