Class Basics
In this page:
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
class Dog
def bark
puts "Woof!"
end
end
rex = Dog.new
rex.bark
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: