When you make a method private, it's not accessible outside the object it's defined in : private « Class « Ruby






When you make a method private, it's not accessible outside the object it's defined in


class Animal
  def initialize(color)
    @color = color
  end

  private
  def get_color
    return @color
  end
end

class Dog < Animal
  def initialize(color)
    @animal = Animal.new(color)
  end

  def get_info
    return @animal.get_color
  end
end

dog = Dog.new("brown")
puts "The new animal is " + dog.get_info

animal2 = Animal.new("red")
puts "The new animal is " + animal2.get_color

 








Related examples in the same category

1.Controlling Access by Making Methods Private