tiny and the simplest implementation of eRuby
ex.
eruby = TinyEruby.new(File.read('example.rhtml'))
print eruby.src # print ruby code
print eruby.result(binding()) # eval ruby code with Binding object
print eruby.evalute(context) # eval ruby code with context object
# File lib/erubis/tiny.rb, line 26
26: def convert(input)
27: src = "_buf = '';" # preamble
28: pos = 0
29: input.scan(EMBEDDED_PATTERN) do |indicator, code|
30: m = Regexp.last_match
31: text = input[pos...m.begin(0)]
32: pos = m.end(0)
33: #src << " _buf << '" << escape_text(text) << "';"
34: text.gsub!(/['\\]/, '\\\&')
35: src << " _buf << '" << text << "';" unless text.empty?
36: if !indicator # <% %>
37: src << code << ";"
38: elsif indicator == '#' # <%# %>
39: src << ("\n" * code.count("\n"))
40: else # <%= %>
41: src << " _buf << (" << code << ").to_s;"
42: end
43: end
44: #rest = $' || input # ruby1.8
45: rest = pos == 0 ? input : input[pos..1] # ruby1.9
46: #src << " _buf << '" << escape_text(rest) << "';"
47: rest.gsub!(/['\\]/, '\\\&')
48: src << " _buf << '" << rest << "';" unless rest.empty?
49: src << "\n_buf.to_s\n" # postamble
50: return src
51: end
# File lib/erubis/tiny.rb, line 61
61: def evaluate(_context=Object.new)
62: if _context.is_a?(Hash)
63: _obj = Object.new
64: _context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end
65: _context = _obj
66: end
67: _context.instance_eval @src
68: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.