Skip to content

Commit 5d78eea

Browse files
authored
Merge pull request #25 from csalmeida/define-call-methods
Defining and calling methods
2 parents dbc0dab + 3c2f8ac commit 5d78eea

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Introduction to core features of the [Ruby](https://www.ruby-lang.org) programmi
4242
- [Inject Methods](#inject-methods)
4343
- [Sort Methods](#sort-methods)
4444
- [Merge Methods](#merge-methods)
45+
- [Custom Methods](#custom-methods)
46+
- [Define and Call Methods](#define-and-call-methods)
4547
</details>
4648

4749
# Getting Started
@@ -1452,4 +1454,54 @@ Since `:b` also presents a `key` conflict even if they have the same values, the
14521454

14531455
The merge method also has a `merge!` version which replaces the contents of a hash.
14541456

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+
def method_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+
def greet(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.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!usr/bin/env ruby
2+
3+
# Defines a method that can take a name as a parameter.
4+
def greet(name)
5+
puts "Hello, #{name}"
6+
end
7+
8+
# Call the method passing its required parameter (name).
9+
greet('Skoglund')
10+
11+
12+
# Defines a method that takes an array of strings and returns the longest one.
13+
def longest(string)
14+
if string.class == String
15+
words = string.split(' ').sort_by! {|word| word.length}
16+
words.last
17+
else
18+
nil
19+
end
20+
end
21+
22+
23+
# Looks for the longest word in the following sentence:
24+
sentence = "The quick brown fox jumps over the lazy dog"
25+
puts longest(sentence)

0 commit comments

Comments
 (0)