I am trying to work our for myself access modifiers in Ruby. I have:
class Person
def initialize (first_name, last_name, age)
@first_name=first_name
@last_name=last_name
@age=age
end
def show()
puts @first_name
puts @last_name
puts @age
end
protected
def compare(other)
self.instance_variable_get(:@age)<=>other.instance_variable_get(:@age)
end
end
p1=Person.new("Some", "Body", "99")
p1.show
puts "\n"
p2=Person.new("Who", "Ever", "21")
p2.show
puts "\n"
p1.compare(p2)
I am getting the error "protected method `compare' called for # (NoMethodError)" I have tried calling from within the class and without. I pasted the without version here. I thought that protected methods could be called on other objects of the same class. What does this error mean and how would I properly use a protected method here? Thank you for your help.