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
+53-1Lines changed: 53 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,8 @@ Introduction to core features of the [Ruby](https://www.ruby-lang.org) programmi
42
42
-[Inject Methods](#inject-methods)
43
43
-[Sort Methods](#sort-methods)
44
44
-[Merge Methods](#merge-methods)
45
+
-[Custom Methods](#custom-methods)
46
+
-[Define and Call Methods](#define-and-call-methods)
45
47
</details>
46
48
47
49
# Getting Started
@@ -1452,4 +1454,54 @@ Since `:b` also presents a `key` conflict even if they have the same values, the
1452
1454
1453
1455
The merge method also has a `merge!` version which replaces the contents of a hash.
1454
1456
1455
-
> There's a challenge available for this chapter: [Ruby Blanks](challenges/ruby-blanks.rb)
1457
+
> There's a challenge available for this chapter: [Ruby Blanks](challenges/ruby-blanks.rb)
1458
+
1459
+
# Custom Methods
1460
+
1461
+
This chapter is focused on introducing concepts to create and work with Ruby's custom methods.
1462
+
1463
+
## Define and Call Methods
1464
+
1465
+
Methods were already mentioned in previous chapters and how they can be applied to other objects. For instance, the use of `reverse`, `each` and `sort`, methods that are predefined on Ruby objects.
1466
+
1467
+
This section will go over how to define can call custom methods. Methods, called functions in other programming languages, are going to provide instructions to perform a specific task which have been packaged up as a unit, and can be called later in a script, multiple times.
1468
+
1469
+
This allows the _"Do not Repeat Yourself"_ (DRY) paradigm to be applied as a methods is defined once and called as needed, instead of being rewritten multiple times.
1470
+
1471
+
In Ruby methods have to be defined before they can be called and can be redefined without error. Usually, on other programming languages a method/function cannot be redeclared at any point, this is not the case with Ruby.
1472
+
1473
+
Generally, methods will be named in lowercase, with words separated by underscores (`my_ruby_method`) and the _first_ character has always to be either lowercase or an underscore, where the latter is uncommon.
1474
+
1475
+
Any other character can be a letter, digit or underscore, with the exception of the last character which can also be a `?`, `!` or `=`.
1476
+
1477
+
Additionally, using the same names for variables and methods should be avoided as it can be confusing.
1478
+
1479
+
### Method Definition
1480
+
1481
+
A method can be defined with the keywords `def` and `end` as follows:
1482
+
1483
+
```ruby
1484
+
defmethod_name
1485
+
# code here...
1486
+
end
1487
+
```
1488
+
1489
+
After being defined, a method can be called as follows:
1490
+
1491
+
```ruby
1492
+
# Executes the method.
1493
+
method_name
1494
+
```
1495
+
1496
+
A more complete method shows how arguments can be passed in:
1497
+
1498
+
```ruby
1499
+
# custom-methods/define-and-call-methods.rb
1500
+
defgreet(name)
1501
+
puts"Hello, #{name}"
1502
+
end
1503
+
1504
+
greet('Skoglund')
1505
+
```
1506
+
1507
+
A method doesn't need to have arguments, but if it does, different data can be passed in each time it's called.
0 commit comments