On Oct 9, 9:39 am, David Trasbo <[EMAIL PROTECTED]>
wrote:
> Frederick Cheung wrote:
> >> How can I replace a {some_partial foo:bar} match with a partial
> >> rendering?
>
> > the string scanner will tell you its position. Keep track of the
> > relevant positions and then use String's usual methods to replace
> > content between 2 positions.
>
> Got it. Okay, I have made this method:
>
> def dropify(content)
> # create a StringScanner
> s = StringScanner.new(content)
> while s.scan_until /\{/
> # where are we now?
> from = s.pointer - 1
> # locals to pass to the partial
> arguments = {}
> partial = s.scan /\w+/
> s.skip /\s+/
> while argument = s.scan( /\w+:\w+/)
> # splitting the argument up into a name and a value
> argument.split!(":")
> name = argument.first.to_sym
> value = argument.last
> # putting it into the arguments hash
> arguments[name] = value
> s.skip /\s+/
> end
> # where are we now?
> to = s.pointer+1
> s.skip_until /\}/
> # put the partial on top of the {some_partial foo:bar} match
> content[from, to] = render :partial => "drops/#{partial}", :locals
> => arguments
> end
> # return
> content
> end
>
> Let me start out by saying that both the argument, name, value,
> arguments, to and from has the values they should have when I debug
> them, but for some reason I'm getting an error that looks like this:
A general point - it may not be advisable to modify the string while
stringscanner is iterating over it
At least on my version of ruby String#split! doesn't exist, and the
errors suggest it didn't actually do what you think it did (and I
struggle to see how it could exist)
name, value = arguments.split(/:/) would probably work better.
The error you get later is a reflection of the fact that the keys in
your locals are things like bar=foo (and i imagine the values are
blank)
Fred
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---