Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Your First Ruby Program 3:14
- Course Overview 1:56
- Our Feature List 0:57
- Methods 6:38
- Methods 5 questions
- Methods 1 objective
- Comments 1:36
- Defining Methods 4:00
- Defining Methods 1 objective
- Variables 5:15
- Variables 1 objective
- Method Arguments 7:02
- Method Arguments 1 objective
- Method Return Values 11:42
- Method Return Values 1 objective
- Review 4 questions
Start a free Courses trial
to watch this video
![](https://videos.teamtreehouse.com/stills/TH-Ruby-Basics-Refresh-S1V6-stills-0.jpg?token=6991e4e4_5b8f3d87124e02cc4b16a286cb6a297a631a9f55)
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
We've shown you how to call methods that Ruby defines for you. Now let's learn how to define your own methods.
- Defining a method
-
def
keyword - method name
- method body: one or more lines of code that will be run when method is called
- lines of method body are usually indented to make it clear they're a part of the method, although this isn't required
-
end
keyword
-
def wait
puts "Waiting..."
sleep 3
puts "Done"
end
def count_to_three
puts 1
puts 2
puts 3
end
- Valid method names
- All lower case
- Numbers are legal but rarely used
- Separate words with underscores. This is called snake case because it makes the name look like it's crawling on the ground.
- Call a method by typing its name in your code
count_to_three
wait
- Can call a method as many times as we want
count_to_three
count_to_three
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Tapiwa Phiri
143 Points2 Answers
-
Ben Santen
1,998 Points3 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up