Hi,
In message "[ruby-talk:10028] dynamic class creation question"
on 01/01/29, David Alan Black <dblack / candle.superlink.net> writes:
|The way I'm doing it right now feels a bit awkward. I'm wondering if
|there's a nicer way to get a dynamically-created class to be able to
|see and use variables from the instance that's creating it (as opposed
|to using class variables in a quasi-global-variable way, which is what
|I'm currently doing).
|
|Here's the little example:
class FirstStage
def initialize(s)
@words = s.split
end
def second_class
words = @words
c = Class.new
c.class_eval <<-EOE
@@words = words
attr_accessor :stuff
def initialize
@stuff = @@words
end
def showstuff
self.stuff.each do |t|
puts t.upcase
end
end
EOE
return c
end
end
# Test run
f = FirstStage.new("These are some words")
s = f.second_class
p = s.new
p.showstuff # THESE\nARE\nSOME\nWORDS\n
--
matz.