Classes and objects

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

ruby first_class.rb

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

ruby initialize_demo.rb

Hello, Ada

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

ruby attr_demo.rb

Ada

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 ask it for its class.

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.class end end Counter.new.show_self

ruby self_demo.rb

Counter

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What runs when User.new("Ada") is called?

    Answer

    initialize, with "Ada" as its argument

    new builds the object and calls initialize with the arguments it was given.

  2. What does this program print? class User def hello puts "Hello" end end User.new.hello

    Answer

    Hello

    new works without an initialize method, and hello prints its line.

  3. What does this program print? class User def initialize(name) @name = name end def hello puts "Hello, " + @name end end User.new("Ada").hello

    Answer

    Hello, Ada

    initialize stores the argument in the instance variable, which hello reads.

  4. What does attr_reader :name add to a class?

    Answer

    A reader method called name that returns @name

    The reader exposes the instance variable without opening writing.

  5. What does this program print? class User attr_accessor :email end user = User.new user.email = "a@b" p user.email

    Answer

    "a@b"

    attr_accessor gives both the reader and the writer, and the writer stores the value.

  6. What does this program print? class User attr_reader :name def initialize(name) @name = name end end p User.new("Ada").name

    Answer

    "Ada"

    p shows the string with its quotes, unlike puts.

  7. What is self at the top level of a Ruby program?

    Answer

    An object called main

    That plain object is why a small script can call methods without a receiver.

  8. What does this program print? class Counter def me self end end p Counter.new.me.class

    Answer

    Counter

    self in an instance method is the object that received the call, and its class is Counter.

  9. What does this program print? class User attr_reader :name def initialize(name) name = name end end p User.new("Ada").name

    Answer

    nil

    The local variable name is assigned to itself; @name was never set, so the reader answers nil.

  10. What does attr_reader :@name raise?

    Answer

    NameError: invalid attribute name '@name'

    The declaration takes the bare attribute name, without the @.

Article author: Arthur Isaev