Skip to content

Commit 458a51f

Browse files
committed
Return multiple values. 🐙
1 parent a9ffdf8 commit 458a51f

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

custom-methods/return.rb

+17-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ def greet
1111
puts greet
1212

1313
# The return value can be explicitly declared as needed.
14-
def greet_again
15-
return greeting = "Yo"
14+
def greet_again(cool = false)
15+
if cool
16+
return greeting = "Yo"
17+
end
18+
greeting = "Hello"
1619
end
1720

18-
puts greet_again
21+
cool = true
22+
puts greet_again(cool)
1923

2024
# The last statement has a conditional
2125
# that when it is not met returns nil.
@@ -38,3 +42,13 @@ def add(number_1, number_2)
3842

3943
puts add(8, 3)
4044

45+
# Multiple values can be returned in an array or hash.
46+
def add_and_subtract(number_1, number_2)
47+
add = number_1 + number_2
48+
subtract = number_1 - number_2
49+
[add, subtract]
50+
end
51+
52+
# Multiple assignment can be used to take values out of an array and assign them to variables.
53+
add_result, sub_result = add_and_subtract(15,2)
54+
puts "Addition result was #{add_result} whilst subtraction was #{sub_result}."

0 commit comments

Comments
 (0)