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