Def ClassName.method_name (?)


In the virtual computer exercise why is the get_users method preceded by the its class name?

def Computer.get_users

All other methods are simply defined with

def method_name



All other methods belong to the instance, this method belongs to the class.

Here is a quick example:

class Example
    def initialize(instance_value, class_value)
        @instance_value = instance_value
        @@class_value = class_value
    def get_instance_value
        return @instance_value
    def Example.get_class_value
        return @@class_value

first =, 10)
second =, 20)
third =, 30)

print "Instance value of the second object: #{second.get_instance_value}\n"
print "Class value: #{Example.get_class_value}"

Result of this code is:

Instance value of the second object: 20
Class value: 30

Why instance_value is 20? Why not 30? Because variable instance_value and method get_instance_value belongs to the instance, to the single object. This method might return different result for every object. We can create many more instances of this class and still, the value of instance_value of the second object will be 20.

Class variables and methods act differently. When we change a value of the class variable we change it for the whole class, not only for the given instance. It's easy to remember that by looking at the way we call the class methods:

print "Class value: #{Example.get_class_value}"

Do you see? We don't have to specify the name of the object on which we want to execute method, we have to give the name of the class instead.