← Home · Ruby skill tree

An interactive Ruby lesson

Classes

A class defines methods for the objects you create from it. Each object made from a class is an instance of that class. Build a counter that stores a number and changes it through method calls. Start with Methods.

p displays an inspected value. Predict the output, press Run, then open the expected output. Change one input and run again. Reset restores that example; Stop ends a running example.

The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so include the whole class definition and its calls in the editor you run. The code and answers remain readable without JavaScript.

1. Define a class

class Counter begins a class definition. A class name starts with an uppercase letter. The inner def value and its end define a method for objects made from this class. The outer end closes the class.

Counter.new creates an instance of Counter and counter = stores that object in a variable. In a call with a dot, the object before the dot receives the call. counter.value calls this object's value method; counter.class reports its class. Predict both lines.

Show expected output
Counter
0

Try it: Change the 0 in value to 5. Predict which output line changes.

2. Set an initial value

Counter.new(5) creates an instance and automatically passes 5 to its initialize method. The parameter starting is local to that method. @value is an instance variable: its name begins with @, and it belongs to that object. The assignment @value = starting stores the starting number. The same object's value method can read it. Predict the displayed number.

Show expected output
5

Try it: Change the starting argument from 5 to 0. Predict the output, then try a negative starting number.

3. Change a stored value

increment reads the object's current @value, adds 1, and assigns the result back. Each call on counter starts from the value left by the previous call in this run. Predict the value after two calls.

Show expected output
7

Try it: Add a third counter.increment call before p. Predict the output. Then change the starting value.

4. Pass an update amount

add(amount) takes an argument. In counter.add(3), amount receives 3 for that call. The method adds it to this object's stored value. Predict the output.

Show expected output
8

Try it: Change 3 to 0, then to a negative amount. Predict each result. Then try two add calls before displaying the value.

5. Keep two instances separate

The two Counter.new calls create separate objects. first.add(3) updates the @value belonging to first. second keeps its own starting value. Predict both lines.

Show expected output
5
10

Try it: Change first.add(3) to second.add(3). Predict both lines and explain which object receives the call.

6. Calculate from stored state

doubled_value returns its last expression, @value * 2. This calculation has no assignment to @value, so the object keeps its stored value. Predict the computed result and the stored value.

Show expected output
8
4

Try it: Change * 2 to * 3. Predict both lines. Then try a starting value of 0 or a negative number.

Practice on your own

Replace the code in the last editor with a program for these tasks. Predict the output, then run it.

  1. Define Counter with initialize(starting), add(amount), and value. Store and update the number in @value.
  2. Create first = Counter.new(10) and second = Counter.new(1).
  3. Call first.add(5) and second.add(2).
  4. Display first.value, then second.value. Explain which object's @value each call reads or changes.
Show one solution and its output
class Counter
  def initialize(starting)
    @value = starting
  end

  def value
    @value
  end

  def add(amount)
    @value = @value + amount
  end
end

first = Counter.new(10)
second = Counter.new(1)
first.add(5)
second.add(2)
p(first.value)
p(second.value)

Output:

15
3

Change the amount in first.add(5) to 0, then to -5. Predict both lines each time. Explain why the value of second stays the same.

Ruby documentation

See the Ruby 4.0 documentation for defining a class and creating an instance with new. The syntax guides explain instance variables and method return values.

Return to the Ruby skill tree to choose another topic.