Clutch

Writing classes in Ruby

Writing classes in Ruby
Average rating: 0
(0 votes)

We know a lot about Ruby, and we are sure you know too. But practice makes perfect. In this article, we offer to check what you know about writing classes in Ruby, how to create them & why they’re so useful! 

A Ruby class helps you define a blueprint for objects. An object is an individual “thing”, with its own identity & its own data.

Notice that:

  • Class names start with an uppercase letter
  • We use the class keyword, then the end keyword
  • An empty class is not very useful, but you can still create objects from it

There are other ways to create a class (like Class.new), but these methods are only useful in special situations.

The main use of a class is to be a container of methods, instance variables & constants, forming a blueprint which you can create objects from. Classes become more useful when you start adding methods & instance variables to them.

A method is a thing your class can do. If you know the class you can find out what methods are available (use Google, ri, or pry), in other words, you can discover what the object can do for you!

Feel free to check more code examples here.

And explore 8 alternative Ruby frameworks! How many of them do you know?

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

*By submitting this form you agree with our Privacy Policy.

Mailing & Legal Address

Syndicode Inc. 340 S Lemon Ave #3299, Walnut CA, 91789, USA

Visiting & Headquarters address
Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Email info@syndicode.com
Phone (+1) 9035021111