4

I have two bits of data I'm working with here.

A "question": This is an [example] string for [testing] and [what not].

And then I have an array of objects (called "answers") from my database:

[#<Answer id: 137, question_id: 207, text: "example">, #<Answer id: 138, question_id: 207, text: "testing">, #<Answer id: 139, question_id: 207, text: "what not"]

What I need to do is replace the bracketed text in the question ([example]) with a link containing data from the corresponding object.

So [example] might become <a href="#" data-answer="137" data-question="207">example</a>.

How can I do that?

3
  • Do you know beforehand what answers to use or do you first have to extract the bracketed text and then search the database? Commented Mar 30, 2011 at 1:51
  • I have the answers beforehand. Commented Mar 30, 2011 at 2:33
  • Since Answer is your custom class, you have tell how to access the id and the question id in an Answer instance. Otherwise, it's impossible to answer. Commented Mar 30, 2011 at 2:44

3 Answers 3

6

Assuming you have the answers already:

str = "This is an [example] string for [testing] and [what not]."
answers.each{|o| str.gsub!("[#{o.name}]", "<a href=\"#\" data-answer=\"#{o.id}\" data-question=\"#{o.question_id}\">#{o.name}</a>")}

If you don't have the answers:

str = "This is an [example] string for [testing] and [what not]."
m =  str.scan(/\[([^\]]*)\]/).map{|s|s[0]}
Answer.where(:text => m).each{|o| str.gsub!("[#{o.name}]", "<a href=\"#\" data-answer=\"#{o.id}\" data-question=\"#{o.question_id}\">#{o.name}</a>")}

This works by using regex to search for all the text inbetween the [] brackets, which then returns an array to m. This array consists of ["example", "testing", "what not"].

You then pass that array into the where() call, which internally has SQL use an IN expression, such as WHERE text IN ('example','testing', 'what not')

After running this, str is now changed to your desired result.

Sign up to request clarification or add additional context in comments.

2 Comments

?? I've been busy writing that for the last 5 or so minutes. (I actually created an Answers model ^_^)
Very true. Honestly I'll be in the create answer page for a couple minutes, and when I submit the answer.. I see someone already had a simliar answer. Sorry for the mixup, Kelly.
2

If you don't want to have to get the whole table from the database, you can search for those bracketed strings first, then perform a search on the database:

str = "This is an [example] string for [testing] and [what not]."
matches =  str.scan(/\[([^\]]*)\]/).collect { |s|s[0]}
Answer.where( :text => matches).each{|o| str.gsub!("[#{o.name}]", "<a href=\"#\" data-answer=\"#{o.id}\" data-question=\"#{o.question_id}\">#{o.name}</a>")}

1 Comment

I would just suggest using %Q{} so you don't have to escape all of the double quotes.
0
s = "This is an [example] string for [testing] and [what not]"
 => "This is an [example] string for [testing] and [what not]" 
h = {id: 137, question_id: 207, text: "example"}
 => {:id=>137, :question_id=>207, :text=>"example"} 

s.sub(/\[example\]/, %Q{<a href="#" data-answer="#{h[:id]}" data-question="#{h[:question_id]}">#{h[:text]}</a>})
 => "This is an <a href=\"#\" data-answer=\"137\" data-question=\"207\">example</a> string for [testing] and [what not]" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.