Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

More options to make delegate more flexible

#1080

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

Foo.new.hi # returns Foo.new.greeter.hello

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
  end
  delegate :zoo, :to => :bar
end

Foo.new.zoo   # raises NoMethodError exception (you called nil.zoo)

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

Foo.new.zoo   # returns nil

Reported by porras · September 20th, 2008 @ 12:56 PM

State: duplicate
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Pratik
    Pratik
    • Title changed from [PATCH] More options to make delegate more flexible to More options to make delegate more flexible

    September 24th, 2008 @ 11:21 PM

  2. porras
    porras
    • Title changed from More options to make delegate more flexible to [PATCH] More options to make delegate more flexible

    This is almost a duplicate of http://rails.lighthouseapp.com/p...

    I added the :allow_nil option to it.

    September 24th, 2008 @ 11:23 PM

  3. porras
    porras
    • Title changed from [PATCH] More options to make delegate more flexible to More options to make delegate more flexible

    Fixing unintentional change of title.

    September 24th, 2008 @ 11:24 PM

  4. Daniel Schierbeck
    Daniel Schierbeck
    • Tag changed from activesupport, enhancement, patch to activesupport, duplicate, enhancement

    October 3rd, 2008 @ 03:57 PM

  5. Frederick Cheung
    Frederick Cheung
    • State changed from new to duplicate

    December 30th, 2008 @ 04:28 PM