Frederick C. wrote:
On 9 Oct 2008, at 10:15, David T. wrote:
I don’t see other possibilities.
accumulate the replacements you need and do them at the end (last one
first)
your locals are things like bar=foo (and i imagine the values are
blank)
Unfortunately that is not true. When I comment out the “content[from,
to] = render…” line and put “puts debug arguments” on the line
before
so it looks like this:
given how :locals is handled it seems highly likely
foo: bars
bar: foos
It would be a lot simpler to just stick a breakpoint in there (or
output arguments.inspect)
I’m quite close to the solution now, my method now looks like this:
def dropify(content)
s = StringScanner.new(content)
drops = {}
i = 0
while s.scan_until /{/
drop = {}
drop[:from] = s.pointer - 1
drop[:arguments] = {}
drop[:partial] = s.scan /\w+/
s.skip /\s+/
while argument = s.scan( /\w+:\w+/)
name, value = argument.split(/:/)
drop[:arguments][name.to_sym] = value
s.skip /\s+/
end
drop[:to] = s.pointer+1
s.skip_until /}/
i = i+1
drops[i] = drop
end
puts drops.inspect
drops.each do |drop|
#content[drop[:from], drop[:to]] = “…Partial content…”
end
content
end
The difference is, that I now have a hash called “drops” with properties
for all my partials to render in there. For each partial I create a
temporary hash called “drop” that I apply to “drops” in the end.
Then I loop over each drop and replace the {some_partial foo:bar} stuff
with a partial (like you wanted: after the StringScanner has finished).
As you can see, I’m doing a drops.inspect, and it outputs this:
{1=>{:to=>42, :partial=>“contact_form”, :arguments=>{:hello=>“you”,
:bar=>“foos”, :foo=>“bars”}, :from=>0}, 2=>{:to=>88,
:partial=>“contact_form”, :arguments=>{:hi=>“you”, :its=>“a_good_day”},
:from=>43}}
It looks right but when I uncomment one of the last lines
(content[drop[:from], drop[:to]] = "…) I get this error:
Symbol as array index
But I’m not using an array!.. Am I missing something?