Ruby/Class/self

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

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:55, 13 сентября 2010

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