This project is archived and is in readonly mode.

#1080 ✓duplicate

More options to make delegate more flexible

Reported by porras | September 20th, 2008 @ 12:56 PM | in 2.x

Adds two options to delegate to make it more flexible and support two frequent patterns in delegation.

You can specify a target if you don't want your method to have the same name:

class Foo < ActiveRecord::Base
  belongs_to :greeter
  delegate :hi, :to => :greeter, :target => :hello
end # returns

If the object in which you delegate can be nil, you may want to use the :allow_nil option. In that case, it returns nil instead of raising a NoMethodError exception:

class Foo
  def initialize(bar = nil)
    @bar = bar
  delegate :zoo, :to => :bar
end   # raises NoMethodError exception (you called nil.zoo)

class Foo
  def initialize(bar = nil)
    @bar = bar
  delegate :zoo, :to => :bar, :allow_nil => true
end   # returns nil

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket


Referenced by