Thanks Christoph,
Yes, I found the way to make singleton methods. It is
by using object.instance_eval(string)
As for avoiding namespace pollution, yes, I really
do want to avoid that. This is why the actual created
methods live in the class of JotsOutput, while the
template processing class itself is different (Jots).
The only real kicker I have left is to figure out
a way to ignore errors where strings contain
variables that have not been defined.
i.e.:
puts "hello #{not_yet_defined_var}"
should print out:
"hello \n"
instead of just raising an exception.
In perl for instance you can:
no strict;
print "hello $not_yet_defined_var\n";
and it will print:
"hello \n"
thanks,
-joe
> -----Original Message-----
> From: Christoph Rippel [mailto:crippel / primenet.com]
> Sent: Thursday, December 14, 2000 10:13 PM
> To: ruby-talk ML
> Subject: [ruby-talk:7286] RE: more on the template system
>
>
>
>
> > -----Original Message-----
> > From: Joseph McDonald [mailto:joe / vpop.net]
> > Sent: Thursday, December 14, 2000 03:40 PM
> > To: ruby-talk ML
> > Subject: [ruby-talk:7263] more on the template system
> >
> >
> >
> > Here is a rough outline of what I am thinking of:
> >
> > snippet1 = <<'EOF'
> > def output
> > puts "I am snippet1 var is: #{var}"
> > puts var2.inspect
> > puts "joe"
> > end
> > EOF
> >
> > snippet2 = <<'EOF'
> > def output
> > puts "I am snippet2 var is: #{var}"
> > puts var2.inspect
> > end
> > EOF
> I think you need to replace this with
> #####
> snippet1 = <<'EOF'
> class Jots
> def output
> puts "I am snippet1 var is: #{var}"
> puts var2.inspect
> puts "joe"
> end
> end
> EOF
>
> snippet2 = <<'EOF'
> class Jots
> def output
> puts "I am snippet2 var is: #{var}"
> puts var2.inspect
> end
> end
> EOF
> ####
> I am also not quite sure if not better served
> with a singlteon construction
>
> t1 = Jots.new
> def t1.output
> puts "I am snippet1 var is: #{var}"
> puts var2.inspect
> puts "joe"
> end
>
> You can automated this if you really want too along the line ..
> (you can also automate adding new ``Snippets'')
>
>
> class Jots ## avoid name pollotion
> module Snippet1
> def output
> puts "I am snippet1 var is: #{var}"
> puts var2.inspect
> puts "joe"
> end
> end
> module Snippet2
> def output
> puts "I am snippet2 var is: #{var}"
> puts var2.inspect
> end
> end
> def Jots.with_output(s)
> t = Jots.new
> case s
> when "Snippet1"
> t.extend(Jots::Snippet1)
> when "Snippet2"
> t.extend(Jots::Snippet2)
> else
> # do nothing
> end
> end
> end
>