This is a total newbie question, but I’d like to know how “return” is
specifically used in ruby. From my understanding, it can be avoided in
a
lot of cases since ruby returns the last value by default.
However, some of the code in the Rails stuff I’m looking at has “return”
and
I was wondering if this “return” is necessary or just the individual
programmer’s style (maybe carried over from other languages).
Sorry, I’m vague but I would like to know how “return” is effectively
used
in ruby if at all.
Programmer’s individual style. You could replace that line with:
security_token
“self” as receiver would be implied, and since it’s the last
expression evaluated, it would be the return value of the method.
end
or this:
def authenticate(login, pass)
u = find(:first, :conditions => ["login = ? AND verified = 1 AND
deleted = 0", login])
return nil if u.nil?
A return in mid-method needs “return”. You could avoid it by
rewriting the end of the method like this:
if u.nil?
nil
else
other code
end
But the “return nil if u.nil?” thing both terminates the method and
provides a visual cue that u being nil is a terminal condition. So
it’s partly style, but once you decide to do a mid-method return, you
have to use “return”.
hi,
‘return’ is used to return a value, and end the method processing.
at the end of a method, ‘return’ is indeed not needed, and simply that
value
will suffice, though i think a return looks nicer
a good example where a return is needed, is this:
def method(arg)
if arg.class==String
return “invalid argument”
end #rest of processing
end
this looks a lot nicer than placing your entire method in if/else code.
greetings, Dirk.
I was wondering if this “return” is necessary or just the individual
write_attribute(‘security_token’, AuthenticatedUser.hashed(
“self” as receiver would be implied, and since it’s the last
return nil if u.nil?
But the “return nil if u.nil?” thing both terminates the method and
provides a visual cue that u being nil is a terminal condition. So
it’s partly style, but once you decide to do a mid-method return, you
have to use “return”.
David
“return” is required if you want to return multiple values like this:
def foo
return “a”, “b”, “c”
end
Though you can also do this
def foo
[“a”, “b”, “c”]
end
Personally, I always use “return” if the name of the variable/method is
short.
But the “return nil if u.nil?” thing both terminates the method and
provides a visual cue that u being nil is a terminal condition. So
it’s partly style, but once you decide to do a mid-method return, you
have to use “return”.
In this case a completely different solution is possible:
def authenticate(login, pass)
u = find(:first, :conditions => [“login = ? AND verified = 1 AND
deleted
= 0”, login]) and
find(:first, :conditions => [“login = ? AND salted_password = ? AND
verified = 1”,
login, AuthenticatedUser.salted_password(u.salt,
AuthenticatedUser.hashed(pass))])
end
Most weird. Let’s hope someone notices and the compiler is modified to
plain drop tail-returns. That is, if “return” is a reserved word and
thus not viable to being overridden by someone who codes with a dead
chicken in his left hand (Gotta love it when flexibility bites you
back. daydreams about clean blocks)