July 2011
Intermediate to advanced
400 pages
8h 46m
English
In many programming languages, a distinction is made between functions or methods that return a value to the calling code and those that do not. In Pascal, for example, a function returns a value, but a procedure does not. No such distinction is made in Ruby. All methods always return a value, though of course you are not obliged to use it.
When no return value is specified, Ruby methods return the result of the last expression evaluated. Consider this method:
return_vals.rb
def method1
a = 1
b = 2
c = a + b # returns 3
endThe last expression evaluated is a + b, which happens to return 3, so that is the value returned by this method. There may often be times when you don’t want to return the last expression evaluated. In such cases, ...
Read now
Unlock full access