|
How to pass a variable number of arguments
def showme(firstw, *therest)
puts firstw + " " + therest.join(" ")
end
showme "Thank", "you", "very", "much!"
Put an * before the last argument when you define the method.It will turn it into an array that will store any other arguments you will use when you call the method.
Using the join method is not necessary, but if you want to separate them when displaying the final message with spaces, you should use it.
Using methods for returning values
def multi(first,second)
first * second
end
multi(2,536)
A very simple method that multiplies 2 numbers.
How to return multiple values with a method
def showme()
return “Thank”, “you”
end
array = showme
puts array.join(“ “)
showme
Like in one of the previous examples, if you want the text to be displayed with spaces between words, assign an array to the method and use the join method on the array.
|