Skip to content

Commit b5fcf0b

Browse files
authored
Merge pull request #28 from csalmeida/return
Return
2 parents 8282e0c + 458a51f commit b5fcf0b

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Introduction to core features of the [Ruby](https://www.ruby-lang.org) programmi
4646
- [Define and Call Methods](#define-and-call-methods)
4747
- [Variable Scope](#variable-scope)
4848
- [Arguments](#arguments)
49+
- [Return](#return)
4950
</details>
5051

5152
# Getting Started
@@ -1640,4 +1641,67 @@ end
16401641

16411642
puts welcome("It's you,", welcome_options); # It's you, Geralt...
16421643
puts welcome("Hello"); # Hello friend!
1644+
```
1645+
1646+
## Return
1647+
1648+
Methods return values, and in Ruby the last operation's value in the code block is the one returned by default. For instance, in the example below, `y + z` is the value returned:
1649+
1650+
```ruby
1651+
def custom_method(x,y,z)
1652+
x + y
1653+
z + x
1654+
y + z
1655+
end
1656+
```
1657+
1658+
The last operation's value is the one returned. This can lead to pitfalls in cases where conditionals take place:
1659+
1660+
```ruby
1661+
# custom-methods/return.rb
1662+
def subtract(number_1, number_2)
1663+
result = number_1 - number_2
1664+
result = 0 if result < 5
1665+
end
1666+
1667+
puts subtract(8, 3) # nil
1668+
```
1669+
1670+
It is not required in Ruby for the `return` keyword to be used in the last line of the method. In some cases, a return value might be required to be declared explicitly. This can be done with the `return` keyword and it can be useful whe applying `if` statements and loops and there's the need to return early.
1671+
1672+
```ruby
1673+
# custom-methods/return.rb
1674+
def greet_again(cool = false)
1675+
if cool
1676+
return greeting = "Yo"
1677+
end
1678+
greeting = "Hello"
1679+
end
1680+
1681+
cool = true
1682+
puts greet_again(cool)
1683+
```
1684+
1685+
Additionally, `puts` and `print` should be avoided in most methods as it makes them more flexible. The return value can be assigned to a variable or interpolated into a string.
1686+
1687+
It is recommended to have separate methods to make calculations and another for output.
1688+
1689+
### Return Multiple Values
1690+
1691+
Methods are able to return only one object. If more than one value needs to be returned, they need to be stored in an object enumerable like a `hash` or an `array`.
1692+
1693+
```ruby
1694+
# custom-methods/return.rb
1695+
def add_and_subtract(number_1, number_2)
1696+
add = number_1 + number_2
1697+
subtract = number_1 - number_2
1698+
[add, subtract]
1699+
end
1700+
```
1701+
1702+
Furthermore the values can be assigned to variables using Ruby's multitple assignment feature. It takes array values and assign them to each variable:
1703+
1704+
```ruby
1705+
add_result, sub_result = add_and_subtract(15,2)
1706+
puts "Addition result was #{add_result} whilst subtraction was #{sub_result}."
16431707
```

custom-methods/return.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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(cool = false)
15+
if cool
16+
return greeting = "Yo"
17+
end
18+
greeting = "Hello"
19+
end
20+
21+
cool = true
22+
puts greet_again(cool)
23+
24+
# The last statement has a conditional
25+
# that when it is not met returns nil.
26+
# This can lead to unexpected behaviour.
27+
def subtract(number_1, number_2)
28+
result = number_1 - number_2
29+
result = 0 if result < 5
30+
end
31+
32+
# The return value might be nil
33+
puts subtract(8, 3).class
34+
35+
# To avoid unexpected behaviour if condition is not met,
36+
# The value might be specified again to make sure it returns correctly.
37+
def add(number_1, number_2)
38+
result = number_1 + number_2
39+
result = 0 if result < 5
40+
result
41+
end
42+
43+
puts add(8, 3)
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}."

0 commit comments

Comments
 (0)