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.
Ok, thanks, in that way what actually we are doing is a class method
not? a static called in java or csharp
but the thing i really feel very weird is these
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
but why the method added to Class works on Person as class method ? if
it is added in Class as instance method ?
···
On Sun, Jan 23, 2011 at 11:29 PM, botp <botpena@gmail.com> wrote:
On Mon, Jan 24, 2011 at 12:58 PM, Lorenzo Brito Morales > <lorenzo.brito@gmail.com> wrote:
..., but i just want to the class Person was the only class whom have it.
then just put it Person
eg,
class Person
def self.add_accessor accessor_name
self.class_eval %Q{
attr_accessor :#{accessor_name}
}
end
end
#=> nil
Ok, thanks, in that way what actually we are doing is a class method
yes. a class method
not? a static called in java or csharp
similar
but the thing i really feel very weird is these
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
but why the method added to Class works on Person as class method ? if
it is added in Class as instance method ?
that means, Person is an instance of class Class
ie when you do,
class Person
end
ruby treats it as
Person=Class.new
best regards -botp
···
On Mon, Jan 24, 2011 at 1:46 PM, Lorenzo Brito Morales <lorenzo.brito@gmail.com> wrote:
WOW thanks a lot, i really got crazy about that. i will continue to
reading the book but
as i see it, ruby really difers from other languate, his feautures are
powerfull if we can understand it.
···
On Mon, Jan 24, 2011 at 12:20 AM, botp <botpena@gmail.com> wrote:
On Mon, Jan 24, 2011 at 1:46 PM, Lorenzo Brito Morales > <lorenzo.brito@gmail.com> wrote:
Ok, thanks, in that way what actually we are doing is a class method
yes. a class method
not? a static called in java or csharp
similar
but the thing i really feel very weird is these
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
but why the method added to Class works on Person as class method ? if
it is added in Class as instance method ?