Hello
I have this code:
country = Struct.new(“france”.intern, :person)
this doesn’t seem right, should be
Struct.new(:name, :person)
but then again, the association “a country has a name and a person”
is quite strange, I’d look for a better name instead.
person_class = Struct.new(“person”.intern)
meth = <<-METHOD
def method
@person = @person_class.new
end
METHOD
Why are you using the evil arts? 
You could just use a constant for each class and write the method right
away with
class SomeClass
def method
#…
end
end
If you don’t want to pollute the namespace in Object wrap everything
in a module.
Plus you should be passing an argument to the constructor, otherwise the
corresponding iv. gets initialized to nil.
country.class_eval(meth)
c = country.new()
c.method()
this is my error message:
…/temp.rb
(eval):2: warning: instance variable @person_class not initialized
(eval):2:in method': undefined method
new’ for nil (NoMethodError)
from ./temp.rb:17
the one that bothers me is the first one with “@person_class not
initialized”
You didn’t create the instance variable at all: person_class is a local
belonging to the outer scope.
You can use $person_class (ugly) or better simply a constant: Person.
Country = Struct.new(:name, :person)
Person = Struct.new(:person)
You can then work your way with class_eval, but in the example you gave
it’s useless.
is it possible to do this ?? I am not sure if this could work at all.
I also tried
“@person_class.name” + “.new”
There’s no way this will work, for
- @person_class is not initialized
- its value is nil
- it doesn’t understand message :name
- even if it did, “some string” + “some other” does nothing but
return the concatenation of both strings, not eval it
- and were it to work in some parallel universe, you’ll normally want
to pass and argument to the constructor.
···
On Mon, Jun 16, 2003 at 06:10:49AM +0900, Markus Jais wrote:
but @person_class.name does return an empty string.
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
And Bruce is effectively building BruceIX
– Alan Cox