File tree 1 file changed +17
-3
lines changed
1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -11,11 +11,15 @@ def greet
11
11
puts greet
12
12
13
13
# 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"
16
19
end
17
20
18
- puts greet_again
21
+ cool = true
22
+ puts greet_again ( cool )
19
23
20
24
# The last statement has a conditional
21
25
# that when it is not met returns nil.
@@ -38,3 +42,13 @@ def add(number_1, number_2)
38
42
39
43
puts add ( 8 , 3 )
40
44
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 } ."
You can’t perform that action at this time.
0 commit comments