File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!usr/bin/env ruby
2
+
3
+ # The last operation's statement is returned.
4
+ # In this case, "Hey".
5
+ def greet
6
+ greeting = "Hi"
7
+ greeting = "Yo"
8
+ greeting = "Hey"
9
+ end
10
+
11
+ puts greet
12
+
13
+ # The return value can be explicitly declared as needed.
14
+ def greet_again
15
+ return greeting = "Yo"
16
+ end
17
+
18
+ puts greet_again
19
+
20
+ # The last statement has a conditional
21
+ # that when it is not met returns nil.
22
+ # This can lead to unexpected behaviour.
23
+ def subtract ( number_1 , number_2 )
24
+ result = number_1 - number_2
25
+ result = 0 if result < 5
26
+ end
27
+
28
+ # The return value might be nil
29
+ puts subtract ( 8 , 3 ) . class
30
+
31
+ # To avoid unexpected behaviour if condition is not met,
32
+ # The value might be specified again to make sure it returns correctly.
33
+ def add ( number_1 , number_2 )
34
+ result = number_1 + number_2
35
+ result = 0 if result < 5
36
+ result
37
+ end
38
+
39
+ puts add ( 8 , 3 )
40
+
You can’t perform that action at this time.
0 commit comments