Defining a class
Here we define a class and
Here the class Bar inherits
from class Foo.
Define a Constructor
In Ruby one defines a
constructor through the
Here we've defined initialize
and have it take an
argument called 'name'.
Then we set an instance
variable to value of 'name'.
In Ruby everything is object.
Objects are instances of a class.
Lets Look at Some Code
This is how we discover an object's class.
This is how we discover a class' parent class.
Classes in Ruby
In Ruby everything is an object. This
includes classes. As such a class in Ruby
is an instance of class Class.
But wait, if everything is an object
then that means Class is an object
too. And sure enough it is.
So if Class is an object, what is it an
It's an instance of class Class. In
other words, it's an instance of itself.
an instance of
Class is an object.
Remember in Ruby everything is an object.
Class is an instance of class Class.
Remember all objects are instances of a class.
So the thing is, there really is nothing overly significant about
this. This relationships is just a result of the way the Ruby
object model was designed. It won't be on the final but it's stuff
that can be useful to know.
The class Class
Let's take a look at what we can do with Class.
We can define classes.
We can do inheritance.
How I commonly use class Class
Many times when people need to
do a simple subclass they jam
their code on a single line as
shown in line :2.
It's valid ruby code but a little
unsightly. It resorts to using the
semicolon which separates the
My personal preference is to
subclass using class Class.
There's quite a bit about classes which we have
This will be an ongoing series so keep an eye
out for more to come.
Thank you for attending.