On Thu, Aug 28, 2008 at 11:30 AM, Sijo Kg [email protected] wrote:
And the result is 1 2 ..This looks some strange to me since
I have not notice in other languages And if arr1=[“1”,“2”] is absent
then it puts Blue Yellow …So dont we need return .I would like to know
what happens here actually
What happens is that Ruby returns the result of the last expression.
This is true for methods and also for blocks:
“this is the last expression”
a() # => “this is the last expression”
“it was true !”
“it was false !”
a(true) # => “it was true !”
a(false) # => “it was false !”
So, the return keyword is not needed. Some people say that (ab)using
can make the code more confusing, specially if the method or block is
with branches, etc, and that explicitly using “return” is better.
For simple methods is usually good enough, though.
And another inteesing thing is if I write
return arr=[“Blue”,“Yellow”] the result i get is Blue Yellow
If I give
return arr1=[“1”,“2”] then also I get "Blue Yellow
This is because “return” exits from the method, and so
this line “return arr1=[“1”,“2”]” is never executed.