[Borges-users] controller.rb - some code suggestions

Слепнев Владимир slepnev_v at rambler.ru
Tue Apr 27 05:28:02 EDT 2004


I think that @delegate is actually never nil in this method:

   def delegate=(controller)
     if @delegate.nil?
       @delegate = Borges::StateHolder.new(controller)
     else
       @delegate.contents = controller
     end

     return self.delegate
   end

So, we could make it:

def delegate=(controller)
   @delegate.contents = controller
end

Returning the value of the last expression evaluated; also, the 
"will_call" optimisation is irrelevant here, because this function is 
called only when call() is invoked.


Also, I we don't need so much "self" in those methods:

   def delegate_to(controller, &block)
     saved = self.delegate
     self.delegate = controller
     value = block.call
     self.delegate = saved
     return value
   end

   def active_controller
     if self.delegate.nil? then
       return self
     else
       return self.delegate.active_controller
     end
   end


We could make it a bit shorter, without much obfuscation =) For 
example,

def active_controller
   delegate.nil? ? self : delegate.active_controller
end


And

def delegate_to(controller)
   saved, self.delegate = delegate, controller
   value = yield
   self.delegate = saved
   return value
end


Also, I think the "return" is superfluous in many one-line methods, 
but maybe it's just my coding style... =)

Vladimir Slepnev


More information about the Borges-users mailing list