Ruby/Class/Public

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

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

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

A class member marked public is accessible by anyone from anywhere; it is the default.

# Default is public, so you don"t really need to use that keyword. 
class Animal
  def initialize(color)
    @color = color
  end
  public
  def get_color
    return @color
  end
end
animal = Animal.new("brown")
puts "The new animal is " + animal.get_color



public is default

class MyClass
  def public_method1
  end
  def public_method2
  end
  protected
  def protected_method1
  end
  private
  def private_method1
  end
  def private_method2
  end
end