class_eval is ideal for adding methods to a class dynamically: : class_eval « Reflection « Ruby






class_eval is ideal for adding methods to a class dynamically:


class Person
end

def add_accessor_to_person(accessor_name)
  Person.class_eval %Q{
    attr_accessor :#{accessor_name}
  }
end

person = Person.new
add_accessor_to_person :name
add_accessor_to_person :gender
person.name = "Peter Cooper"
person.gender = "male"
puts "#{person.name} is #{person.gender}"

 








Related examples in the same category

1.Creating Your Own Version of attr_accessor