L
Lorenzo Brito Morales
I cant not understand why these code is well
class Class
def add_accessor(accessor_name)
self.class_eval %Q{
attr_accessor :#{accessor_name}
}
end
end
class Person
end
person = Person.new
Person.add_accessor :name
Person.add_accessor :gender
person.name = "Peter Cooper"
person.gender = "male"
puts "#{person.name} is #{person.gender}"
and these not work
class Person
def add_accessor(accessor_name)
self.class_eval %Q{
attr_accessor :#{accessor_name}
}
end
end
person = Person.new
Person.add_accessor :name
Person.add_accessor :gender
person.name = "Peter Cooper"
person.gender = "male"
puts "#{person.name} is #{person.gender}"
in the first group of code, the code is added to the class Class, so
any class has the method add_accesor, but i just want to the class
Person was the only class whom have it. Im just reading a book, and it
says that they put the code in the class Class just to all the class
has the method.
class Class
def add_accessor(accessor_name)
self.class_eval %Q{
attr_accessor :#{accessor_name}
}
end
end
class Person
end
person = Person.new
Person.add_accessor :name
Person.add_accessor :gender
person.name = "Peter Cooper"
person.gender = "male"
puts "#{person.name} is #{person.gender}"
and these not work
class Person
def add_accessor(accessor_name)
self.class_eval %Q{
attr_accessor :#{accessor_name}
}
end
end
person = Person.new
Person.add_accessor :name
Person.add_accessor :gender
person.name = "Peter Cooper"
person.gender = "male"
puts "#{person.name} is #{person.gender}"
in the first group of code, the code is added to the class Class, so
any class has the method add_accesor, but i just want to the class
Person was the only class whom have it. Im just reading a book, and it
says that they put the code in the class Class just to all the class
has the method.