Ruby - Returning Multiple Values

Introduction

Ruby doesn't make a distinction between "by reference" and "by value,".

Ruby can return multiple values all in one statement:

def ret_things
    greeting = "Hello world"
    a = 1
    b = 2.0
    return a, b, 3, "four", greeting, 6 * 10
end

Multiple return values are placed into an array.

You can explicitly return a different collection type such as a Hash:

def ret_hash
    return {'a'=>'hello', 'b'=>'goodbye', 'c'=>'fare thee well'}
end

Related Topic