class_eval

  • Thread starter Lorenzo Brito Morales
  • Start date
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.
 
B

botp

..., 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

person=Person.new
#=> #<Person:0x871188c>

Person.add_accessor :name
#=> nil

Person.add_accessor :gender
#=> nil

person.name="Peter Cooper"
#=> "Peter Cooper"

person.gender="male"
#=> "male"

person.name
#=> "Peter Cooper"

person.gender
#=> "male"

best regards -botp
 
L

Lorenzo Brito Morales

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 =3D 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 ?
 
B

botp

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
 
L

Lorenzo Brito Morales

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top