Skip to content

Commit 8282e0c

Browse files
authored
Merge pull request #27 from csalmeida/arguments
Arguments
2 parents f608b48 + 8b41b76 commit 8282e0c

File tree

2 files changed

+156
-1
lines changed

2 files changed

+156
-1
lines changed

README.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Introduction to core features of the [Ruby](https://www.ruby-lang.org) programmi
4545
- [Custom Methods](#custom-methods)
4646
- [Define and Call Methods](#define-and-call-methods)
4747
- [Variable Scope](#variable-scope)
48+
- [Arguments](#arguments)
4849
</details>
4950

5051
# Getting Started
@@ -1530,4 +1531,113 @@ output_value # Will print 5, not 10.
15301531
puts value # Will print 10, not 5.
15311532
```
15321533

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+
def volume(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+
def introduction(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+
def introduction(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+
def welcome()
1591+
puts "Hello!"
1592+
end
1593+
1594+
welcome()
1595+
1596+
# A method with no arguments is usually defined and called with no parentheses.
1597+
def goodbye
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+
def call name
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+
def greet(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:
1627+
1628+
```ruby
1629+
# custom-methods/arguments.rb
1630+
welcome_options = {
1631+
name: "Geralt",
1632+
punctuation: "..."
1633+
}
1634+
1635+
def welcome(greeting, options={})
1636+
name = options[:name] || 'friend'
1637+
punct = options[:punctuation] || '!'
1638+
greeting + ' ' + name + punct
1639+
end
1640+
1641+
puts welcome("It's you,", welcome_options); # It's you, Geralt...
1642+
puts welcome("Hello"); # Hello friend!
1643+
```

custom-methods/arguments.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!usr/bin/env ruby
2+
3+
# Defines a methods with multiple arguments.
4+
def volume(x, y, z)
5+
x * y * z
6+
end
7+
8+
puts volume(2,3,4)
9+
puts volume(5,7,8)
10+
puts volume(42,86,22)
11+
12+
# The order of arguments passed in matters.
13+
def introduction(greeting, name)
14+
puts "#{greeting}, #{name}."
15+
end
16+
17+
# Here the name is passed in first, resulting on a different output than expected.
18+
introduction("Yoda","I am") # "Yoda, I am."
19+
introduction("I am","Groot") # "I am, Groot."
20+
21+
# Arguments can be optional, any object can be passed as a default value.
22+
def greet(greeting="Hello", name="World")
23+
puts "#{greeting}, #{name}"
24+
end
25+
26+
# Calls greet with no arguments so default ones are used.
27+
# As per convention the parentheses are kept.
28+
greet()
29+
30+
welcome_options = {
31+
name: "Geralt",
32+
punctuation: "..."
33+
}
34+
35+
# A hash with optional arguments is passed, allowing more flexibility.
36+
# Each expected key is checked for a value inside the method.
37+
def welcome(greeting, options={})
38+
name = options[:name] || 'friend'
39+
punct = options[:punctuation] || '!'
40+
greeting + ' ' + name + punct
41+
end
42+
43+
# The greeting arg is always passed in but the options hash is optional.
44+
puts welcome("It's you,", welcome_options);
45+
puts welcome("Hello");

0 commit comments

Comments
 (0)