Return statement where it is usefull?

i’ve the habit (from java ;-)) to put a return statement in a method
like that :

def get_server_index(server)
if self.servers.include?(server)
return self.servers.index(server)
end
return -1
end

in that case, is it usefull ? (i think not)

where could be it usefull ?
never ???

Hi,

i’ve the habit (from java ;-)) to put a return statement in a method
like that :

def get_server_index(server)
if self.servers.include?(server)
return self.servers.index(server)
end
return -1
end

in that case, is it usefull ? (i think not)

I think that both cases are usefull: return indicates that you intention
to return something and you don’t do it by accident. It’s more for the
human who reads the code.

(my 0.02 â?¬)

Patrick

return stops execution of a method.

def m
return [nil]
p “here”
end
m

See, “here” never gets printed, if you didn’t have the return keyword:
def m
[nil]
p “here”
end
m
“here” does get printed.

j`ey
http://www.eachmapinject.com

Le 20 juil. 06 à 10:58, Patrick G. a écrit :

(my 0.02 â?¬)

or a glass of your prefered liquid when u stop by Paris ? :wink:

Le 20 juil. 06 à 11:52, Joey a écrit :

See, “here” never gets printed, if you didn’t have the return keyword:
def m
[nil]
p “here”
end
m
“here” does get printed.

and what’s the returned value in that case ? nil, i guess…

Yvon T. wrote:

“here” does get printed.

and what’s the returned value in that case ? nil, i guess…
It’s the return value of the p method, which happens to be nil in this
case. In general, it’s the value of the last expression in the method.

On 20-jul-2006, at 10:40, Une bévue wrote:

in that case, is it usefull ? (i think not)
Ruby implicitly returns from the last expression in the method, but
if it’s inside a condition
it might not return. For instance:

def foo
case bar
when “x”
1
when “y”
2
else
3
end
end

In this case it will return 1, 2 or 3 implicitly.

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

On 20 Jul 2006, at 09:40, Une bévue wrote:

in that case, is it usefull ? (i think not)

where could be it usefull ?
never ???

As a documentation aid.
From the middle of a condition without falling through to the bottom
of the method
To return more than one value

Dave.

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 :wink:

Le 20 juil. 06 à 13:25, F. Senault a écrit :

Fred
Stating the obvious ?

not at all :wink:

Le 20 juillet à 10:35, Une bévue a écrit :

in that case, is it usefull ? (i think not)
Emmm, in this precise case, it is - if you don’t put your -1 in a else
statement, without the return, you’ll always get -1…

Now, I’d probably write it like this :

def get_server_index(server)
if self.servers.include?(server)
self.servers.index(server)
else
-1
end
end

Fred
Stating the obvious ?

end
This starts to be off topic, but

def get_server_index(server)
servers.fetch(server, -1)
end

should do the same. To say something more
on topic, return is a method call (or sort
of) and not using it (if you don’t need)
is faster - or at least it was last time
I checked.

cheers

Simon

Hi –

On Thu, 20 Jul 2006, Julian ‘Julik’ Tarkhanov wrote:

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

Definitely (and even “self.” can be eliminated) – also, I would guess
that the return value is being tested for negativeness, in which case
maybe the test could be changed to test for nil, and then the method
could be:

def get_server_index(server)
servers.index(server)
end

at which point it would probably cease to be necessary.

David

On Jul 20, 2006, at 12:30, Yvon T. wrote:

thanks very much, much more rubyish :wink:

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.

Le 20 juil. 06 à 14:01, [email protected] a écrit :

at which point it would probably cease to be necessary.

the solution i like the best :wink:

Le 20 juil. 06 à 14:42, Matthew S. a écrit :

Obviously, this may not be an issue in your particular code, but it
is something to watch out for.

fine, thanks, i’ve learned something this hot afternoon :wink:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs