You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+111-1Lines changed: 111 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,7 @@ Introduction to core features of the [Ruby](https://www.ruby-lang.org) programmi
45
45
-[Custom Methods](#custom-methods)
46
46
-[Define and Call Methods](#define-and-call-methods)
47
47
-[Variable Scope](#variable-scope)
48
+
-[Arguments](#arguments)
48
49
</details>
49
50
50
51
# Getting Started
@@ -1530,4 +1531,113 @@ output_value # Will print 5, not 10.
1530
1531
puts value # Will print 10, not 5.
1531
1532
```
1532
1533
1533
-
The example above shows how two variables called `value` were defined and are different as they belong to different local scopes. `value = 10` is scoped to the wider structure of the document whilst `value = 5` is scoped to the `output_value` method.
1534
+
The example above shows how two variables called `value` were defined and are different as they belong to different local scopes. `value = 10` is scoped to the wider structure of the document whilst `value = 5` is scoped to the `output_value` method.
1535
+
1536
+
## Arguments
1537
+
1538
+
Arguments, also referred to as `args`, allow a method to receive values at runtime (when it is called). Multiple arguments can be defined in a method, and these are separated by commas.
1539
+
1540
+
The order and number of arguments passed in must match the method definition. In the example below, the `volume` method expects three arguments:
1541
+
1542
+
```ruby
1543
+
# custom-methods/arguments.rb
1544
+
defvolume(x, y, z)
1545
+
x * y * z
1546
+
end
1547
+
1548
+
volume(2,3,4) # 24
1549
+
```
1550
+
1551
+
Arguments can be used to add dynamism to scripts, as methods can be called with different values each time:
1552
+
1553
+
```ruby
1554
+
# custom-methods/arguments.rb
1555
+
volume(5,7,8) # 280
1556
+
volume(42,86,22) # 79464
1557
+
```
1558
+
1559
+
The method requires all arguments to be passed in, otherwise it will throw an error:
1560
+
1561
+
```ruby
1562
+
volume(42,86)
1563
+
# custom-methods/arguments.rb:4:in `volume': wrong number of arguments (given 2, expected 3) (ArgumentError)
1564
+
```
1565
+
1566
+
It will also expect arguments to be passed in the correct order, otherwise unexpected behavior might occur:
1567
+
1568
+
```ruby
1569
+
# custom-methods/arguments.rb
1570
+
defintroduction(greeting, name)
1571
+
puts"#{greeting}, #{name}."
1572
+
end
1573
+
1574
+
introduction("Yoda","I am") # "Yoda, I am."
1575
+
introduction("I am","Groot") # "I am, Groot."
1576
+
```
1577
+
1578
+
The convention in Ruby is that methods should only have parentheses if they take arguments, whether they're being defined or called:
1579
+
1580
+
```ruby
1581
+
# Parentheses being used to wrap arguments:
1582
+
defintroduction(greeting, name)
1583
+
puts"#{greeting}, #{name}."
1584
+
end
1585
+
1586
+
introduction("Yoda","I am")
1587
+
1588
+
# The same being done with a method with no arguments.
1589
+
# Uncommon, but valid Ruby.
1590
+
defwelcome()
1591
+
puts"Hello!"
1592
+
end
1593
+
1594
+
welcome()
1595
+
1596
+
# A method with no arguments is usually defined and called with no parentheses.
1597
+
defgoodbye
1598
+
puts"Hello!"
1599
+
end
1600
+
1601
+
goodbye
1602
+
1603
+
# However, methods with arguments can also refrain from using parentheses.
1604
+
# This is discouraged and considered bad practice by some Rubyists, but some might define and call methods this way.
1605
+
defcallname
1606
+
puts"#{name}!"
1607
+
end
1608
+
1609
+
call 'puppy'
1610
+
```
1611
+
1612
+
### Argument Default Values
1613
+
1614
+
Arguments can be made optional, in case it is not assigned a default value will be put in place instead. It can take any Ruby object as an optional value:
1615
+
1616
+
```ruby
1617
+
defgreet(greeting="Hello", name="World")
1618
+
puts"#{greeting}, #{name}"
1619
+
end
1620
+
1621
+
greet() # "Hello, World"
1622
+
```
1623
+
1624
+
Required arguments should be listed first as those need to be passed in, whilst optional ones are listed last.
1625
+
1626
+
However, if a default value needs be skipped over, usually the optional arguments are passed in as a `hash` in order to provider further flexibility:
0 commit comments