Question on Struct and class_eval

Hello

I have this code:

country = Struct.new(“france”.intern, :person)
person_class = Struct.new(“person”.intern)

meth = <<-METHOD
def method
@person = @person_class.new
end
METHOD

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 methodnew’ for nil (NoMethodError)
from ./temp.rb:17

the one that bothers me is the first one with “@person_class not
initialized”

is it possible to do this ?? I am not sure if this could work at all.
I also tried
"@person_class.name" + “.new”

but @person_class.name does return an empty string.

any ideas

Thanks in advance

Markus

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? :slight_smile:
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

I thought Mauricio was going to take your head off :wink:
(SIG_ORANGE_ALERT:)

Country = Struct.new(:name, :person)

Person_class = Struct.new(:person)

Country.class_eval(<<-METHOD)
def method
p [name, person]
p Person_class.new(‘someone’)
end
METHOD

c = Country.new(‘France’, ‘Guy’)
c.method()

class Country
def method_2
p Person_class.new(‘someone else’)
end
end

c.method_2

daz

···

“Markus Jais” info@mjais.de wrote:

Hello

I have this code:

country = Struct.new(“france”.intern, :person)
person_class = Struct.new(“person”.intern)

meth = <<-METHOD
def method
@person = @person_class.new
end
METHOD

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”

is it possible to do this ?? I am not sure if this could work at all.
I also tried
@person_class.name” + “.new”

but @person_class.name does return an empty string.

any ideas

Thanks in advance

Markus

I thought Mauricio was going to take your head off :wink:
(SIG_ORANGE_ALERT:)

Didn’t mean to be harsh. Sorry if I was, I guess I was too tired to
think of style :slight_smile:

Country = Struct.new(:name, :person)

Person_class = Struct.new(:person)

c = Country.new(‘France’, ‘Guy’)

class Country
def method_2
p Person_class.new(‘someone else’)
end
end

c.method_2

This method is as simple as it gets, I don’t see why you’d want to use
any other :slight_smile:

···

On Mon, Jun 16, 2003 at 01:31:28PM +0900, daz wrote:

“Markus Jais” info@mjais.de wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

I’ve run DOOM more in the last few days than I have the last few
months. I just love debugging :wink:
– Linus Torvalds

Fear not. I was engaging in playful exaggeration for
lack of anything to add. :))

daz

···

“Mauricio Fernández” batsman.geo@yahoo.com wrote:

On Mon, Jun 16, 2003 at 01:31:28PM +0900, daz wrote:

“Markus Jais” info@mjais.de wrote:

I thought Mauricio was going to take your head off :wink:
(SIG_ORANGE_ALERT:)

Didn’t mean to be harsh. Sorry if I was, I guess I was too tired to
think of style :slight_smile:

thanks for your help!

Markus

daz wrote:

···

“Mauricio Fernández” batsman.geo@yahoo.com wrote:

On Mon, Jun 16, 2003 at 01:31:28PM +0900, daz wrote:

“Markus Jais” info@mjais.de wrote:

I thought Mauricio was going to take your head off :wink:
(SIG_ORANGE_ALERT:)

Didn’t mean to be harsh. Sorry if I was, I guess I was too tired to
think of style :slight_smile:

Fear not. I was engaging in playful exaggeration for
lack of anything to add. :))

daz