DEV Community

Randy Steele
Randy Steele

Posted on

Ruby Class vs Object

I once was asked in an interview what the difference between a class and an object is and of course, I froze! So, I wanted to talk about it a little bit.

A class describes the contents of the objects that belong to it. I can define a class like this

class Blog
end
Enter fullscreen mode Exit fullscreen mode

Once a class is created you can create a new object that will belong to the Blog class.

Objects can be strings, integers, symbols etc. Every object has a class and classes define objects. I can create an object like this

myblog = Blog.new
Enter fullscreen mode Exit fullscreen mode

So basically the class is like the blueprint. I can have a blog class and it will have properties or attributes such as content and title, and if I created an object from the Blog class called myblog then that object would have a content title and attributes.

I hope this helps!

Discussion (0)