← Back to Ruby Course | Chapter 9: OOP | Lesson 1 of 7

Class Basics

In this page:

  1. Class Basics

Class Basics

A Ruby class is defined with class ClassName ... end, bundling data and behavior into a single blueprint that objects are created from with .new. Class names conventionally use CamelCase, unlike methods and variables which use snake_case. Every value in Ruby, including numbers and strings, is an object of some class.

Note: Ruby class names must start with a capital letter -- this is enforced by the language, not just a style convention.

Example: Class Basics

markup
class Dog
  def bark
    puts "Woof!"
  end
end

rex = Dog.new
rex.bark
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.