Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Defining Methods0:00 with Jay McGavren
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
- 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
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
- Can call a method as many times as we want
You need to sign up for Treehouse in order to download course files.Sign up