self represents the current class, : self « Class « Ruby






self represents the current class,


# def self.test_method defines the method as being specific to the class. 
# with no prefix, methods are automatically instance methods.



class Square
  def self.test_method
    puts "Hello from the Square class!"
  end

  def test_method
    puts "Hello from an instance of class Square!"
  end
end

Square.test_method
Square.new.test_method

 








Related examples in the same category

1.The style ClassName.method_name versus self.method_name to define class method