All Ruby methods always return a value.
When no return value is specified, Ruby methods return the result of the last expression evaluated.
Consider this method:
def method1 a = 1 b = 2 c = a + b # returns 3 end
The last expression evaluated is a + b, which happens to return 3.
3 is the value returned by this method.
You can specify the return value using the return keyword:
def method2 a = 1 b = 2 c = a + b return b # returns 2 end
A method don not need to use any assignments to return a value.
If a simple piece of data happens to be the last thing evaluated in a method, that will be the value the method returns.
When nothing is evaluated, nil is returned:
def method3 "hello" # returns "hello" end def method4 a = 1 + 2 "goodbye" # returns "goodbye" end def method5 end # returns nil