Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Instantiation3:49 with Jason Seifer
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.
Initialize a new string:
Initialize a new string with an argument:
Initialize a new array:
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,
we'll eventually want to get specific.
Let's use a string as an example.
Using the variable name set to the string Jason, we have a string class.
The name is the specific version of the abstract idea of a string.
We call that an instance.
The act of creating an instance of a class is called instantiation.
Once an instance of a class is created we call that instance an object.
Classes are referred to and
created in Ruby by using a capital letter, and the name of the class.
For example, to refer to the string class, we use a capital S, and
write out the rest of the word.
Objects are instantiated by using the class name, then a dot, then the word new.
We've already seen that before, but let's practice that now using Workspaces.
So, I've launched a Ruby workspace and you'll see on the left side,
we have myclass.rb.
We'll get to that in just a minute.
Let's go ahead and look at instantiating classes.
So down here I'm gonna just bring the console up.
And let's start by typing in irb to launch interactive ruby.
Now first, I'm going to instantiate a new class of a string.
Now, we can do this by assigning a variable.
We're just gonna call that string, and then a space, an equal sign, and
And we'll say String, which is the name of the class.
And you can tell that it's a class name because the S is capitalized.
Classes are written with capital letters first.
They we'll type a dot and the word new.
That will create a new empty string.
When we call .new, we are initializing a new instance of the string class.
And we can see what kind of class our variable is
by calling the class method on the object.
And in this case it returns String.
Now, we've seen this before,
if we wanted to we could also initialize a string with an argument.
Just like methods have arguments,
instantiation is also a method which you can pass arguments to.
So we could call String.new and then pass it a string argument.
And the same thing works with arrays.
We've seen arrays before.
And hashes, these are some built in Ruby types that we've worked with previously.
We're not gonna get too far into it right now but
let's go ahead and exit irb and open up myclass.rb.
Here's a class that I've written.
And this is just a very, very simple class.
We'll get a little bit more into writing our own classes in later videos.
This is just to show an example of the initialize method.
When you call MyClass.new the initialize method automatically gets called.
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