From: Matthew Smillie Date: 2006-07-20T21:42:12+09:00 Subject: Re: return statement where it is usefull ? On Jul 20, 2006, at 12:30, Yvon Thoraval wrote: > > Le 20 juil. 06 � 13:16, Julian 'Julik' Tarkhanov a �crit : > >> As for your method, why not use a conditional assignment? >> Array#index returns nil when the element is not in the >> array, so you can get a nil and suppress it with -1. >> >> def get_server_index(server) >> self.servers.index(server) || -1 >> end > > thanks very much, much more rubyish ;-) Well, as long as we're talking about Rubyish, then -1 is probably not the best choice as an indicator of failure, nil would be preferred. One of the reasons for this is that -1 is a valid array index in Ruby, while in the programmer's mind, -1 means failure. This sort of disagreement can lead to some very annoying errors: servers[some_index].do_something_important If some_index is nil, you get an immediate error. If it's -1, then you would actually perform the operation, but on an unexpected object, which is potentially catastrophic. Tracking down answers to "how did this data get corrupted?" is a vastly unrewarding task. Obviously, this may not be an issue in your particular code, but it is something to watch out for. matthew smillie.