Hi,
As always, I'm asking simple questions sorry.
Don't be sorry, questions need answers :).
Please help me to understand the "return" method because I dont see
where or how to use it.
Here I'm using the return method but the thing is that if remove it, it
also works, so I was wondering why would you use it if by defaul all
methods return a value?
def multiply(val1, val2 )
result = val1 * val2
return result
end
The right way to understand return is thst it's a control operation, it tells Ruby to exit from the procedure RIGHT NOW, with an optional return value. So you (almost) never need it as the last step in a function (the exception is given below), but you use it if you have the result you need, and don't want to execute the current procedure any more.
irb(main):001:0> def how_many_sum_to(n)
irb(main):002:1> sum = 0
irb(main):003:1> i = 1
irb(main):004:1> loop do
irb(main):005:2* sum += i
irb(main):006:2> return i if sum > n
irb(main):007:2> i += 1
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> how_many_sum_to(12)
=> 5
The (poorly-named) procedure keeps adding integers until it gets a value greater than n, and returns the value that put the sum over the top. (You can get the same answer without a loop if you do some algebra, but that's not relevant here.) I used a loop here rather than a while to demonstrate that in this example the return acts like a break, exiting the loop and the containing procedure.
irb(main):028:0> def print_square_root(x)
irb(main):029:1> if x > 0
irb(main):030:2> print("the square root of %f is %f\n"%[x, Math.sqrt(x)])
irb(main):031:2> return
irb(main):032:2> end
irb(main):033:1> print("sorry, I don't know about complex numbers yet\n")
irb(main):034:1> end
=> nil
irb(main):035:0> print_square_root(4)
the square root of 4.000000 is 2.000000
=> nil
irb(main):036:0> print_square_root(-4)
sorry, I don't know about complex numbers yet
=> nil
Here we used a return to exit from a procedure early, even without a loop.
Both of these procedures could be refactored to eliminate the need for return. In many (most?) cases, code reads more clearly without a return than with. The exception comes when you have perhaps several levels of nested loops, or some rescues, or the like. So I tend to see the presence of a return as an invitation to refactor, but there are certainly many cases where the version with return is the clearest.
There's one exception to the rule that a return as the last statement of a procedure is unnecessary. If you want to return several values, you need a return, because a comma-separated list of expressions isn't itself an expression.
irb(main):037:0> def return_needed(x)
irb(main):038:1> x+1, x-1
irb(main):039:1> end
SyntaxError: (irb):38: syntax error, unexpected ',', expecting keyword_end
x+1, x-1
^
from /Users/vmanis/local/bin/irb:12:in `<main>'
irb(main):040:0> def return_needed(x)
irb(main):041:1> return x+1, x-1
irb(main):042:1> end
=> nil
irb(main):043:0> return_needed(4)
=> [5, 3]
Of course, since returning multiple values returns an array, you can again eliminate the need for return by explicitly returning an array.
irb(main):044:0> def return_needed(x)
irb(main):045:1> [x+1, x-1]
irb(main):046:1> end
Hope this helps. -- vincent
···
On 2011-04-16, at 15:41, Fily Salas wrote: