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.

:allow_nil option for delegate

#1127

Adds :allow_nil option to delegate method:


  # 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
  #    attr_accessor :bar
  #    def initialize(bar = nil)
  #      @bar = bar
  #    end
  #    delegate :zoo, :to => :bar
  #  end
  #
  #  Foo.new.zoo   # raises NoMethodError exception (you called nil.zoo)
  #
  #  class Foo
  #    attr_accessor :bar
  #    def initialize(bar = nil)
  #      @bar = bar
  #    end
  #    delegate :zoo, :to => :bar, :allow_nil => true
  #  end
  #
  #  Foo.new.zoo   # returns nil
  #

Reported by porras · September 27th, 2008 @ 08:43 PM

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

Activity

  1. Daniel Schierbeck
    Daniel Schierbeck

    +1: This seems like a good addition to delegate, and I like the simplicity of the implementation.

    September 29th, 2008 @ 02:23 PM

  2. Emili Parreño
  3. David Röthlisberger
    David Röthlisberger
    1. Useful, and the patch is very simple.

    If anything, I'd change the rdoc to say "If the object to which you delegate...".

    October 14th, 2008 @ 02:06 PM

  4. David Röthlisberger
    David Röthlisberger

    (Um, that "1." in the comment above was supposed to be a "+1").

    October 14th, 2008 @ 02:07 PM

  5. Josh Susser
    Josh Susser

    -1

    I dislike silently hiding errors like that. Seems like just asking for trouble.

    October 14th, 2008 @ 03:30 PM

  6. David Röthlisberger
    David Röthlisberger

    How is it hiding the error any more than:

    
    def contact_name; contact && contact.name; end
    

    It's intended as a shortcut for the case where not having a contact isn't an error. Remember you have to specify :allow_nil, this isn't going to be default behaviour.

    October 14th, 2008 @ 04:46 PM

  7. porras
    porras

    I updated the patch, which no longer applied after the :prefix option was added.

    @Josh, as Dave explained, this option is intended only for the cases where this is not an error but a legitimate situation, just to avoid that verbose and ugly definition.

    December 8th, 2008 @ 12:09 AM

  8. Daniel Schierbeck
    Daniel Schierbeck

    I'd like some more attention brought on this ticket (i.e. BUMP), as I've recently found use cases for it abound.

    I currently have the following simplified setup:

    # A resident may or may not have a current residentship -- he
    # could be a former resident.
    class Resident < ActiveRecord::Base
      has_many :residentships
    
      delegate :room_number, :room, :to => :residentship
    
      def residentship
        residentships.only_current.first
      end
    end
    
    class Residentship < ActiveRecord::Base
      belongs_to :resident
      validates_presence_of :room_number, :moved_out_at
      named_scope :only_current, ...
      composed_of :room, :mapping => %w(room_number number)
    end
    
    class Room # value class, as there's a finite set of rooms
      def residentship
        Residentship.only_current.find_by_room_number(@number)
      end
    
      delegate :resident, :to => :residentships
    end
    
    

    I'd like Room#resident, Resident#room and Resident#room_number (as well as several more in my real app) to work properly for former residents, i.e. just return nil -- but with the current implementation of delegate, they raise an exception instead. Adding an :allow_nil option to delegate would greatly reduce the number of extra methods i need to have.

    December 16th, 2008 @ 07:02 PM

  9. Repository
    Repository
    • State changed from new to resolved

    (from [e8de7a67a5ef063164da022845a7cae1753da80e]) Add :allow_nil option to delegate [#1127 state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    December 22nd, 2008 @ 12:14 AM

  10. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:49 PM

  11. bingbing
  12. yoyogo
    yoyogo

    oh my!! i love you so much!!

    Thank you so much.
    Hermes Bags
    http://www.louisvuittonparty.com/
    Hermes Bags

    March 31st, 2011 @ 05:59 AM

  13. slina
    slina

    iPad 2 Video Converter is perfectly compatible with iPad 2

    iPad 2 Video Converter Mac is your best choice to convert videos to iPad 2 on Mac OS X.

    DVD to iPad 2 is specially designed for Apple users.

    DVD to iPad 2 Mac is a wonderful gift for Mac and iPad users

    Play 1080P Blu-ray movies with Blu-ray to iPad 2 Converter

    Blu-ray to iPad 2 Mac is definitely your best choice to convert Blu-ray DVD to iPad 2 for playback with with its large and high-resolution screen.

    Blu-ray to ipad 2

    Blu-ray to Nintendo 3DS Converter can easily rip/convert Blu-ray DVD, even protected DVDs and encrypted AACS/ BD+ Blu-ray movies to Nintendo 3DS easily and quickly.

    DVD to Nintendo 3DS, actally an all-in-one Blu-ray DVD Converter, enables users to convert Blu-ray (.m2ts) ) or DVD movies (.vob) or ISO image files or IFO to Nintendo 3DS/NDSL/ DS, iPhone 4, iPad 2, Motorola Xoom, Android Phones, iPod Touch, Xbox 360, PSP, PS3, Motorola Droid, Nexus One, HTC EVO 4G, etc for entertainment freely.

    March 31st, 2011 @ 06:36 AM