DEV Community

pooyaalamdari
pooyaalamdari

Posted on

private method

class Dog
    attr_reader :age, :dog_years

    def dog_years=(years) # write
        @dog_years = years
    end

    def age=(years) # write
        @age = years
        self.dog_years = years * 7
    end

    private :dog_years=
end

rover = Dog.new 
rover.age = 10 

# calling private method 

# bcz dog_years method is private
# we can not call it directly 
# so we call self.dog_years
# that is in the age= method 
puts rover.dog_years

Enter fullscreen mode Exit fullscreen mode

we can not amounting private method directly
this is wrong way

puts rover.dog_years=(70)
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)