What does the self prefix mean in a Ruby method definition inside a class?
The <code>self</code> prefix means the method is a class method. It belongs to the class itself, not to instances of the class.
Click to reveal answer
beginner
How do you call a class method defined with <code>self.method_name</code> in Ruby?
You call it on the class directly, like <code>ClassName.method_name</code>, without creating an instance.
Click to reveal answer
intermediate
Why use class methods instead of instance methods?
Class methods are used for actions related to the class as a whole, like creating new instances or utility functions, while instance methods work on individual objects.
Click to reveal answer
beginner
Example: What does this code do?<br><pre>class Dog
def self.bark
puts 'Woof!'
end
end</pre>
Defines a class method <code>bark</code> on <code>Dog</code>. You can call <code>Dog.bark</code> to print 'Woof!'.
Click to reveal answer
intermediate
Can class methods access instance variables directly? Why or why not?
No, class methods cannot access instance variables directly because instance variables belong to individual objects, not the class itself.
Click to reveal answer
How do you define a class method in Ruby?
Adef self.method_name
Bdef method_name
Cdef @method_name
Ddef #method_name
✗ Incorrect
Class methods are defined with def self.method_name inside the class.
How do you call a class method named info in class Car?
Ainfo.Car
BCar.info
Ccar.info
DCar#info
✗ Incorrect
Class methods are called on the class itself, like Car.info.
Which of these is true about class methods?
AThey are only used inside instance methods.
BThey can access instance variables directly.
CThey are private by default.
DThey can be called without creating an object.
✗ Incorrect
Class methods belong to the class and can be called without creating an instance.
What will this code print?
class Cat
def self.sound
puts 'Meow'
end
end
Cat.sound
AError
Bsound
CMeow
DNothing
✗ Incorrect
Calling Cat.sound runs the class method and prints 'Meow'.
Can a class method access instance variables like @name?
ANo, because instance variables belong to objects.
BYes, always.
COnly if the class method is called on an instance.
DOnly if the instance variable is global.
✗ Incorrect
Instance variables belong to individual objects, so class methods cannot access them directly.
Explain what a class method is in Ruby and how it differs from an instance method.
Think about who owns the method: the class or the object.
You got /4 concepts.
Describe a situation where you would use a class method instead of an instance method.
When the action relates to the whole class, not a single object.
You got /4 concepts.
Practice
(1/5)
1. What does adding self. before a method name inside a Ruby class do?
easy
A. Defines a method that can only be called inside other methods
B. Makes the method private and inaccessible outside the class
C. Makes the method an instance method, callable on objects
D. Makes the method a class method, callable on the class itself
Solution
Step 1: Understand the role of self. in method definition
In Ruby, prefixing a method name with self. inside a class defines it as a class method.
Step 2: Identify how class methods are called
Class methods are called on the class itself, not on instances of the class.
Final Answer:
Makes the method a class method, callable on the class itself -> Option D
Quick Check:
self.method_name defines class method [OK]
Hint: Remember: self.method means class method, no instance needed [OK]
Common Mistakes:
Thinking self. makes method private
Confusing class methods with instance methods
Assuming self. methods are only for internal use
2. Which of the following is the correct syntax to define a class method named greet inside a Ruby class?
easy
A. def @greet; puts 'Hello'; end
B. def greet; puts 'Hello'; end
C. def self.greet; puts 'Hello'; end
D. def ::greet; puts 'Hello'; end
Solution
Step 1: Recall class method syntax
Class methods in Ruby are defined by prefixing the method name with self. inside the class.
Step 2: Check each option
def self.greet; puts 'Hello'; end uses def self.greet, which is the correct syntax for a class method.
Final Answer:
def self.greet; puts 'Hello'; end -> Option C
Quick Check:
Class method syntax = def self.method_name [OK]
Hint: Class methods always start with def self.method_name [OK]
Common Mistakes:
Omitting self. for class methods
Using instance variable syntax (@) for methods
Trying to use :: inside method definition
3. What will be the output of the following Ruby code?
class Calculator
def self.add(a, b)
a + b
end
end
puts Calculator.add(3, 4)
medium
A. 7
B. 34
C. Error: undefined method 'add'
D. nil
Solution
Step 1: Identify the method type and call
The method add is defined as a class method using self.add, so it can be called on the class Calculator.
Step 2: Evaluate the method call
Calling Calculator.add(3, 4) returns the sum of 3 and 4, which is 7.
Final Answer:
7 -> Option A
Quick Check:
Class method call returns sum 7 [OK]
Hint: Class methods called on class, so Calculator.add works [OK]
Common Mistakes:
Trying to call add on an instance
Confusing string concatenation with addition
Expecting error due to method definition
4. Find the error in this Ruby class method definition:
class Person
def self.say_hello
puts "Hello!"
end
def self.say_goodbye
puts "Goodbye!"
end
def say_hello
puts "Hi!"
end
end
Person.say_hello
Person.say_goodbye
Person.new.say_hello
Person.say_hello()
medium
A. Calling Person.say_hello() with parentheses causes error
B. No error, all calls are valid
C. Person.say_hello is both class and instance method causing conflict
D. Person.new.say_hello calls instance method correctly, no error
Solution
Step 1: Analyze method definitions
There are two class methods self.say_hello and self.say_goodbye, and one instance method say_hello.
Step 2: Check method calls
Calling Person.say_hello calls the class method. Person.new.say_hello calls the instance method. Parentheses in Person.say_hello() are allowed in Ruby.
Final Answer:
No error, all calls are valid -> Option B
Quick Check:
Class and instance methods coexist without conflict [OK]
Hint: Class and instance methods can share names without error [OK]
Common Mistakes:
Assuming parentheses cause errors in method calls
Thinking class and instance methods with same name conflict
Expecting error when calling class method with parentheses
5. You want to create a Ruby class Counter that keeps track of how many times its class method increment is called. Which code correctly implements this behavior?
hard
A. class Counter
def self.increment
@count ||= 0
@count += 1
end
def self.count
@count
end
end
B. class Counter
@count = 0
def increment
@count += 1
end
def count
@count
end
end
C. class Counter
def self.increment
@@count += 1
end
def self.count
@@count
end
end
D. class Counter
def increment
@@count ||= 0
@@count += 1
end
def count
@@count
end
end
Solution
Step 1: Understand class variable vs class instance variable
Using @@count is a class variable shared across subclasses, but @count inside class methods is a class instance variable, safer for this use.
Step 2: Check method definitions
class Counter
def self.increment
@count ||= 0
@count += 1
end
def self.count
@count
end
end uses @count ||= 0 inside the class method to initialize the class instance variable, then increments it. It also provides a class method count to read the value.
Final Answer:
class Counter
def self.increment
@count ||= 0
@count += 1
end
def self.count
@count
end
end -> Option A
Quick Check:
Use class instance variable @count inside self methods [OK]
Hint: Use @count with ||= inside self methods to track class state [OK]
Common Mistakes:
Using instance methods to change class state
Using @@count without initialization causing errors
Confusing instance variables with class variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.