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

Eric Hodel drbrain at segment7.net
Tue Apr 27 12:28:12 EDT 2004


??????? ???????? (slepnev_v at rambler.ru) wrote:

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

[snip Controller#delegate=]

> 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.

Note that when calling a controller, you pass through twice.  Once with
the controller you are delegating to, and again without it (since it is
stored in @delegate).  See the last line of code in Controller#call.

> 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

I'm going to leave these exactly as-is, since it caused me no end of
trouble.  Sometimes Ruby cannot tell when you are creating a local
variable or invoking a method.

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

These can go.

> We could make it a bit shorter, without much obfuscation =) For 
> example,
> 
> def active_controller
>   delegate.nil? ? self : delegate.active_controller
> end

OK

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

I'm not too fond of the multiple assignment.

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

Yes, these are Smalltalk transition leftovers.  I'm weeding them out
slowly.

-- 
Eric Hodel - drbrain at segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/borges-users/attachments/20040427/5845e955/attachment.bin


More information about the Borges-users mailing list