0
0
Rubyprogramming~10 mins

Class methods with self prefix in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Class methods with self prefix
Define class
Define method with self.
Call method on class
Execute method body
Return result
Output result
This flow shows how a class method defined with self. is called on the class itself and executes its code.
Execution Sample
Ruby
class Greeting
  def self.say_hello
    "Hello from class method!"
  end
end

puts Greeting.say_hello
Defines a class method say_hello and calls it on the Greeting class to print a message.
Execution Table
StepActionEvaluationResult
1Define class GreetingClass Greeting createdClass Greeting ready
2Define method self.say_helloMethod say_hello added to Greeting classMethod ready
3Call Greeting.say_helloInvoke class methodMethod body runs
4Execute method bodyReturn string "Hello from class method!""Hello from class method!"
5puts outputPrint string to consoleHello from class method!
💡 Method returns string and program ends after printing output
Variable Tracker
VariableStartAfter callFinal
GreetingClass definedClass existsClass exists
say_hello (method)Not definedDefined on GreetingDefined on Greeting
Return valueNoneNone"Hello from class method!"
Key Moments - 2 Insights
Why do we use self. before method name?
Using self. defines the method as a class method, so it belongs to the class itself, not instances. See execution_table step 2 where method is added to class.
Can we call say_hello without self. prefix?
No, without self. it becomes an instance method. Here, we call Greeting.say_hello on the class, so method must be defined with self. (see step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result returned by the class method at step 4?
Anil
BAn error
C"Hello from class method!"
D"say_hello"
💡 Hint
Check the 'Result' column at step 4 in execution_table
At which step is the method say_hello defined as a class method?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Action' column for method definition in execution_table
If we remove self. from method definition, what will happen when calling Greeting.say_hello?
AIt will print the message as before
BIt will raise a NoMethodError
CIt will call an instance method automatically
DIt will return nil
💡 Hint
Recall key_moments about method type and how class methods are called
Concept Snapshot
Class methods use self. prefix in method definition.
Called on the class itself, not instances.
Syntax: def self.method_name
Use for behavior related to the class as a whole.
Called like ClassName.method_name.
Without self., methods are instance methods.
Full Transcript
This example shows how to define and call a class method in Ruby using the self. prefix. The class Greeting is defined, then a method say_hello is defined with self. to make it a class method. When we call Greeting.say_hello, Ruby runs the method body and returns the string "Hello from class method!" which is printed. The execution table traces each step: defining the class, defining the method, calling the method, returning the string, and printing it. The variable tracker shows the class and method states. Key moments clarify why self. is needed and what happens if it is removed. The quiz tests understanding of the method return value, definition step, and behavior without self. The snapshot summarizes the key points about class methods with self. prefix.