Classes and objects

Contents
Define a class
Initialize objects
attr_reader and accessor
What self is

Define a class

A class is a blueprint for objects. It groups data and behavior in one named place, so related methods live next to the values they use.

Shared behavior can also come from outside the class, as shown in Modules and mixins.

class User def hello puts "Hello" end end user = User.new user.hello

Initialize objects

When you call new, Ruby builds a fresh object and runs a special method named initialize. It receives the arguments you pass to new and stores them in instance variables that start with @.

Each object keeps its own copy of these variables, so two users never share a name.

class User def initialize(name) @name = name end def hello puts "Hello, " + @name end end user = User.new("Ada") user.hello

attr_reader and accessor

Instance variables hide inside the object. To read them from outside, declare attr_reader with the variable name without @.

Use attr_accessor when outside code must also change the value. Prefer the reader by default and open writing only where needed.

class User attr_reader :name attr_accessor :email def initialize(name) @name = name end end user = User.new("Ada") puts user.name

What self is

Inside a method, self is the current object, the one that received the call. You can call other methods on it or print it for inspection.

At the top level of a program self is a plain object called main, which is why small scripts can call methods without a receiver.

class Counter def show_self puts self end end Counter.new.show_self

Next: Inheritance

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: