In the previous lessons, we talked about how objects are things that receive commands, and methods are the commands that tell the object what to do.
We saw many Array and String methods - but it turns out we can actually create methods ourselves as well.
Let's navigate to the ruby_fundamentals
folder.
cd ruby_fundamentals
Next, let's create a new file called first_method.rb
.
touch first_method.rb
Open the new file that we just created.
Inside this file, we are going to define our first method. Inside the file, let's add the following lines of code:
def hello_world
end
As we can see here, we can define a new method with def
. def
stands for define.
Defining a new method
def method_name # write code here end
As you can see, a Ruby method always starts with def
and ends with end
.
Let's add the following line of code inside the method:
puts "Hello world!"
Your method should now look like this:
def hello_world
puts "Hello world!"
end
Awesome! You've written your first Ruby method. Even though we have written our method, our method won't do anything until we call it. We can call the method by typing the method name:
def hello_world
puts "Hello world!"
end
hello_world
Calling the method is also called invoking the method.
Save the file. Let's run the ruby file. Type in the following command in your terminal and press enter:
ruby first_method.rb
Your output should look like this:
Let's write a more slightly more interesting program. Let's write a program that will greet the person depending on the name that is passed into the program.
Let's create a new file called greet.rb
. In your terminal type in the following and press enter:
touch greet.rb
This will create a new file called greet.rb
. Let's open this file up.
Inside this file, let's define a method called greet
:
def greet
end
Next, let's print out a greeting:
def greet
puts "Greetings!"
end
The next step is to take in a parameter. As we talked about before, parameters are information passed into the method so that we can use that information inside that method.
We can specify a parameter like this:
def greet(name)
puts "Greetings!"
end
Notice how after the method name greet
, we added (name)
. In this program, name
is a parameter.
You might hear the word argument and parameter being used in the same context. They both mean the same thing and are synonyms.
We can use the parameter that is passed in inside of the program:
def greet(name)
puts "Greetings #{name}!"
end
As you can see here, name
acts like a variable within the program. However, it is invalid outside of the program:
def greet(name)
puts "Greetings #{name}!"
end
# outside of the method, name cannot be used
puts name
NameError: undefined local variable or method `name' for main:Object
Let's call our new method. Again, to call the method, we simply write the method name:
def greet(name)
puts "Greetings #{name}!"
end
greet
But this time, we need to pass in a parameter. In otherwords, we need to pass in a name to the greet
method:
def greet(name)
puts "Greetings #{name}!"
end
greet("Jack")
Save the file and run the ruby file:
ruby greet.rb
Let's go over what we're doing here.
- We defined a method called
greet
- We added a parameter called
name
- We are printing out
"Greeting #{name}!"
.#{name}
is the information passed in as a parameter- We end the method with
end
One thing to note is that in Ruby, using parenthesis to provide parameters are optional:
# The two statements below are the same
greet "Jack"
greet("Jack")
Let's create a new ruby file called happy_birthday.rb
:
touch happy_birthday.rb
We are going to create a method that congratulates a person's birthday!
First, let's define a method called happy_birthday
:
def happy_birthday
end
Next, let's set up two parameters this time, name
and age
:
def happy_birthday(name, age)
end
As you can see, you can add multiple parameters by seperating them with ,
s.
Next, let's print out a sentence that congratulates them:
def happy_birthday(name, age)
puts "Happy birthday #{name}, you're #{age}!"
end
Let's call this method a few times:
def happy_birthday(name, age)
puts "Happy birthday #{name}, you're #{age}!"
end
happy_birthday("Adam", 30)
happy_birthday("Tom", 10)
happy_birthday("Peter", 23)
Save the file and run the file:
ruby happy_birthday.rb
Output:
Happy birthday Adam, you're 30!
Happy birthday Tom, you're 10!
Happy birthday Peter, you're 23!
def
end
,
s