3 comments
clear explanation! thanks man
Thank for reading 🙂
Great article! Really dives deep and gives a ton of detail about a simple concept.
What is a Ruby method?
A method is one, or multiple, lines of Ruby code grouped together for a specific purpose.
This grouped code is given a name so you can use it whenever you want without having to write or copy & paste the code again.
The purpose of a method can be to:
Example 1:
The size
method on an Array
object gives you a count of elements (get information).
Example 2:
The pop
method removes the last element from the array (change objects).
When you understand the relationship between objects, classes & methods everything starts to make sense.
Let’s keep learning!
The Ruby programming language has many powerful built-in methods you can use, but you can also create your own.
How?
You can define your own instance method using the def
keyword.
Here’s the syntax:
def gimme_bacon puts "Bacon plz." end
What’s going on here?
def
is part of Ruby’s syntax, it says that we want to def
ine a methodgimme_bacon
is the name of the methodputs "Bacon plz."
is the body of the methodend
marks the end of the method definitionDefining a method only tells Ruby that you want to create it.
If you want to use it then you need to call the method.
In Ruby, when we use a method, we say that we’re calling it.
You’ll often hear “method call”, or if you’re working with someone who’s an Object-Oriented purist, you may hear that “you’re sending a message”.
Either way…
Let’s see an example of using a method.
Here you go:
gimme_bacon
This prints:
"Bacon plz."
You can call methods on objects.
For example:
n = [1,2,3] n.size # 3
This n.size
is calling the method size
on the object n
, which happens to be an Array
.
The result?
We get the array’s size.
That depends on the class of the object you’re calling the method on.
An array is going to have different methods than a hash.
You can check the Ruby documentation to find a list of methods for a given class.
One key concept in Ruby is that ALL methods return a value.
Let me explain!
As a result of calling a method, you get something back.
This “something” that you get comes from the last expression in your method definition.
Here’s what I mean:
def number_one 1 end number_one # 1
Another example:
def add(x,y) x + y end add(5, 6) # 11
We call this “implicit return”, just a fancy name for “automatically return the last thing”.
In addition:
You can tell Ruby to return
something with a keyword.
def two return 2 end # 2
Notice that your method stops running when you use return
.
You use this is for an early return in your code, or to exit a loop.
You may find some strange Ruby methods.
With names like:
empty?
sort!
title=
ALL of these are valid method names.
What does the question mark, the exclamation mark, or the equals sign mean?
They are conventions in the Ruby community.
Explanation:
true
or false
None of these conventions are enforced by the language.
But…
If you follow them, you’ll be able to write more Ruby-like code!
You’ve learned about the power of Ruby methods, how to define them, use them & how to follow proper conventions.
Now it’s your turn to put this into practice 🙂
Thanks for reading!
clear explanation! thanks man
Thank for reading 🙂
Great article! Really dives deep and gives a ton of detail about a simple concept.