Skip to content

Instantly share code, notes, and snippets.

@manveru
Created October 6, 2010 12:13
Show Gist options
  • Select an option

  • Save manveru/613255 to your computer and use it in GitHub Desktop.

Select an option

Save manveru/613255 to your computer and use it in GitHub Desktop.
class Etanni
SEPARATOR = "E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82"
CHOMP = "<<#{SEPARATOR}.chomp!"
START = "\n_out_ << #{CHOMP}\n"
STOP = "\n#{SEPARATOR}\n"
REPLACEMENT = "#{STOP}\\1#{START}"
def initialize(template, filename = '<Etanni>')
@template = template
@filename = filename
compile
end
def compile(filename = @filename)
temp = @template.strip
temp.gsub!(/<\?r\s+(.*?)\s+\?>/m, REPLACEMENT)
@compiled = eval("Proc.new{ _out_ = [#{CHOMP}]\n#{temp}#{STOP}_out_.join }",
nil, @filename)
end
def result(instance, filename = @filename)
instance.instance_eval(&@compiled)
end
end
etanni = Etanni.new('Hello, #{name}')
p etanni.result(Struct.new(:name).new('manveru'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment