Ruby/Class/self

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

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



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

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