Damir Svrtan
class Person
def age
rand(1..100)
end
end
Person.new.age
=> 83
class Person
protected
def age
@age
end
end
Person.new.age
=> protected method `age' called for #<Person:0x007fd901098fd0>
class Person
def initialize(age=25)
@age = age
end
def whats_my_age_again?
self.age
end
private
def age
@age
end
end
puts Person.new.whats_my_age_again?
private method `age' called for #<Person:0x007fc06d0d4b18 @age=25>
class Person
def initialize(age=25)
@age = age
end
def whats_my_age_again?
age
end
private
def age
@age
end
end
puts Person.new.whats_my_age_again?
25
class City
attr_accessor :name, :county, :mayor
def initialize(name, county, mayor)
@name = name
@county = county
@mayor = mayor
end
def ==(other)
other.class == City && self.full_location == other.full_location
end
protected
def full_location
"#{name.titleize} #{county.titleize}"
end
end
moj_voljeni_grad = City.new('Zagreb', 'Grad Zagreb', 'Milan Bandic')
moj_omrazeni_grad = City.new('Zagreb', 'Grad zagreb', 'scumbag')
class Person
protected
def has_sti?
@has_sti
end
private
def age
@age
end
end
class Person
protected def has_sti?
@has_sti
end
private def age
@age
end
end
class Person
private
def age=(value)
@age = value
end
def age
@age
end
end
person = Person.new
person.age
=> private method `age' called for
person.age = 25