J
James Brister
I'm trying to do something similar to the Camping 'R' class generator.
Can anyone explain to me how to get the following to print out from the
program below:
param is p and arg is this is a P
param is q and arg is this is a Q
(basically I want to capture the argument to the maker functions inside
the class they generate).
---- cut here --- cut here --- cut here ---
## Using this function causes a blank after "arg is"
def maker1(thearg)
Class.new do
@arg = thearg.dup
def self.argval
@arg
end
def doit(param)
puts "param is #{param} and arg is #{self.class.argval}"
end
end
end
## Using this function causes the same value to be printed out for all
classes
def maker2(thearg)
Class.new do
@@arg = thearg.dup
def doit(param)
puts "param is #{param} and arg is #{@@arg}"
end
end
end
## using this function causes a undefined 'local variable or method'
def maker3(thearg)
Class.new do
def doit(param)
puts "param is #{param} and arg is #{thearg}"
end
end
end
# change maker1 to maker2 or maker3
class P < maker1 'this is a P'
def something
doit('p')
end
end
# change maker1 to maker2 or maker3
class Q < maker1 'this is a Q'
def something
doit('q')
end
end
P.new.something
Q.new.something
Can anyone explain to me how to get the following to print out from the
program below:
param is p and arg is this is a P
param is q and arg is this is a Q
(basically I want to capture the argument to the maker functions inside
the class they generate).
---- cut here --- cut here --- cut here ---
## Using this function causes a blank after "arg is"
def maker1(thearg)
Class.new do
@arg = thearg.dup
def self.argval
@arg
end
def doit(param)
puts "param is #{param} and arg is #{self.class.argval}"
end
end
end
## Using this function causes the same value to be printed out for all
classes
def maker2(thearg)
Class.new do
@@arg = thearg.dup
def doit(param)
puts "param is #{param} and arg is #{@@arg}"
end
end
end
## using this function causes a undefined 'local variable or method'
def maker3(thearg)
Class.new do
def doit(param)
puts "param is #{param} and arg is #{thearg}"
end
end
end
# change maker1 to maker2 or maker3
class P < maker1 'this is a P'
def something
doit('p')
end
end
# change maker1 to maker2 or maker3
class Q < maker1 'this is a Q'
def something
doit('q')
end
end
P.new.something
Q.new.something