Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, we'll dive deeper in to Classes and how they work. We'll learn about the differences between classes and instances, as well as the process of creating new objects.
Code Samples
Initialize a new string:
String.new
Initialize a new string with an argument:
String.new("Jason")
Initialize a new array:
Array.new
Creating your own initialize method:
class MyClass
def initialize
puts "This is the initialize method."
end
end
MyClass.new
This will print out the following:
This is the initialize method.
-
0:00
As we're thinking of a class in terms of an abstract idea like a blueprint,
-
0:05
we'll eventually want to get specific.
-
0:08
Let's use a string as an example.
-
0:11
Using the variable name set to the string Jason, we have a string class.
-
0:18
The name is the specific version of the abstract idea of a string.
-
0:24
We call that an instance.
-
0:26
The act of creating an instance of a class is called instantiation.
-
0:32
Once an instance of a class is created we call that instance an object.
-
0:39
Classes are referred to and
-
0:41
created in Ruby by using a capital letter, and the name of the class.
-
0:46
For example, to refer to the string class, we use a capital S, and
-
0:51
write out the rest of the word.
-
0:53
Objects are instantiated by using the class name, then a dot, then the word new.
-
1:01
We've already seen that before, but let's practice that now using Workspaces.
-
1:06
So, I've launched a Ruby workspace and you'll see on the left side,
-
1:10
we have myclass.rb.
-
1:12
We'll get to that in just a minute.
-
1:15
Let's go ahead and look at instantiating classes.
-
1:19
So down here I'm gonna just bring the console up.
-
1:24
And let's start by typing in irb to launch interactive ruby.
-
1:30
Now first, I'm going to instantiate a new class of a string.
-
1:37
Now, we can do this by assigning a variable.
-
1:41
We're just gonna call that string, and then a space, an equal sign, and
-
1:46
another space.
-
1:48
And we'll say String, which is the name of the class.
-
1:54
And you can tell that it's a class name because the S is capitalized.
-
1:58
Classes are written with capital letters first.
-
2:02
They we'll type a dot and the word new.
-
2:06
That will create a new empty string.
-
2:10
When we call .new, we are initializing a new instance of the string class.
-
2:18
And we can see what kind of class our variable is
-
2:22
by calling the class method on the object.
-
2:28
And in this case it returns String.
-
2:31
Now, we've seen this before,
-
2:33
if we wanted to we could also initialize a string with an argument.
-
2:39
Just like methods have arguments,
-
2:41
instantiation is also a method which you can pass arguments to.
-
2:50
So we could call String.new and then pass it a string argument.
-
2:57
And the same thing works with arrays.
-
2:59
We've seen arrays before.
-
3:02
And hashes, these are some built in Ruby types that we've worked with previously.
-
3:08
We're not gonna get too far into it right now but
-
3:10
let's go ahead and exit irb and open up myclass.rb.
-
3:21
Here's a class that I've written.
-
3:22
And this is just a very, very simple class.
-
3:25
We'll get a little bit more into writing our own classes in later videos.
-
3:29
This is just to show an example of the initialize method.
-
3:34
When you call MyClass.new the initialize method automatically gets called.
-
3:41
If we were to run this file, we would see, this is the initialize method.
You need to sign up for Treehouse in order to download course files.
Sign up