On Jun 29, 2009, at 11:33 AM, Yanni Mac wrote:

>
> I am trying to figure out a (better) way to do the following:
>
> some_string = "Display a widget here - <widget>1234</widget>"
> new_string = replace_widgets_in_string(some_string)
> p new_string => #Display a widget here - Widget 1234 is blue"
>
> the method replace_widgets_in_string will need to pick out the xml tag
> and value (which will be the model id) and from there I will find the
> model and return a string that replaces the widget tags.
>
> Is there an existing library or easy way to do this that I might not  
> be
> thinking of?  I am assuming rexml wont work, since its not a well  
> formed
> doc.   I want to avoid coding a monolithic method using string splits.
> Any ideas?

Well, if you're string will only ever contain one instance of the  
widget this would work...

 >> some_string =~ /<(.*?)>(\d+)<\/\1>/
=> 24
 >> $1
=> "widget"
 >> $2
=> "1234"

Otherwise I'd look into hpricot/nokigirl and let it parse it.

-philip

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to