Hello, I''m learning rails and starting to try to refactor code that repeats itself.. I have several lines of code like the following and wonder if there is a more efficient way to do this. Any suggestions would be greatly appreciated as one example would help me figure out how to refactor alot of my code. if @formatted_question[:anno_a].length < 2 @formatted_question[:anno_a] = @question[:correct_anno] end if @formatted_question[:anno_b].length < 2 @formatted_question[:anno_b] = @question[:correct_anno] end if @formatted_question[:anno_c].length < 2 @formatted_question[:anno_c] = @question[:correct_anno] end if @formatted_question[:anno_d].length < 2 @formatted_question[:anno_d] = @question[:correct_anno] end if @formatted_question[:anno_e].length < 2 @formatted_question[:anno_e] = @question[:correct_anno] end -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c7c1664c60f0c30b690385a7d9ee014d%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
@formatted_question.each_pair do |key, value| @formatted_question[key] = @question[:correct_anno] if value.length < 2 end -- Dheeraj Kumar On Wednesday 3 July 2013 at 8:19 AM, Dave Castellano wrote:> Hello, > > I''m learning rails and starting to try to refactor code that repeats > itself.. > > I have several lines of code like the following and wonder if there is a > more efficient way to do this. Any suggestions would be greatly > appreciated as one example would help me figure out how to refactor alot > of my code. > > if @formatted_question[:anno_a].length < 2 > @formatted_question[:anno_a] = @question[:correct_anno] > end > if @formatted_question[:anno_b].length < 2 > @formatted_question[:anno_b] = @question[:correct_anno] > end > if @formatted_question[:anno_c].length < 2 > @formatted_question[:anno_c] = @question[:correct_anno] > end > if @formatted_question[:anno_d].length < 2 > @formatted_question[:anno_d] = @question[:correct_anno] > end > if @formatted_question[:anno_e].length < 2 > @formatted_question[:anno_e] = @question[:correct_anno] > end > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c7c1664c60f0c30b690385a7d9ee014d%40ruby-forum.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/06D4719696FD4CE78EAB13A873952EA5%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Dheeraj Kumar wrote in post #1114259:> @formatted_question.each_pair do |key, value| > @formatted_question[key] = @question[:correct_anno] if value.length < > 2 > end > > -- > Dheeraj KumarThanks!! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/aaa709f35904eda2652b1fa290e4bc93%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Dave Castellano wrote in post #1114260:> Dheeraj Kumar wrote in post #1114259: >> @formatted_question.each_pair do |key, value| >> @formatted_question[key] = @question[:correct_anno] if value.length < >> 2 >> end >> >> -- >> Dheeraj Kumar > > Thanks!!Oops, that will not work as the hash contains other attributes.. formatted = { #id: self.id, #complete_question: "\r#{question} \r\r\r A. #{answer_list[0][0]}\r\r B. #{answer_list[1][0]}\r\r C. #{answer_list[2][0]}\r\r D. #{answer_list[3][0]}\r\r E. #{answer_list[4][0]}\r", correct_answer: self.correct_ans_1, answer_a: answer_list[0][0], answer_b: answer_list[1][0], answer_c: answer_list[2][0], answer_d: answer_list[3][0], answer_e: answer_list[4][0], anno_a: answer_list[0][1], anno_b: answer_list[1][1], anno_c: answer_list[2][1], anno_d: answer_list[3][1], anno_e: answer_list[4][1], anno_pict_1: answer_list[0][2], anno_pict_2: answer_list[1][2], anno_pict_3: answer_list[2][2], anno_pict_4: answer_list[3][2], anno_pict_5: answer_list[4][2], correct_ans_pict: self.correct_ans_pict, #author: self.author, question_pict: self.question_pict, question: self.question, correct_answer_position: random_insert + 1, correct_answer_letter: correct_answer_shuffled } I need to target just anno_a thru anno_d -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3d918ff75bfc1a261eed3615e2cde1dd%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
[:anno_a, :anno_b, :anno_c, :anno_d].each do |key| @formatted_question[key] = @question[:correct_anno] if @formatted_question[key].length < 2 end -- Dheeraj Kumar On Wednesday 3 July 2013 at 9:08 AM, Dave Castellano wrote:> Dave Castellano wrote in post #1114260: > > Dheeraj Kumar wrote in post #1114259: > > > @formatted_question.each_pair do |key, value| > > > @formatted_question[key] = @question[:correct_anno] if value.length < > > > 2 > > > end > > > > > > -- > > > Dheeraj Kumar > > > > > > > > > Thanks!! > > Oops, that will not work as the hash contains other attributes.. > formatted = { > #id: self.id, > #complete_question: "\r#{question} \r\r\r A. > #{answer_list[0][0]}\r\r B. #{answer_list[1][0]}\r\r C. > #{answer_list[2][0]}\r\r D. #{answer_list[3][0]}\r\r E. > #{answer_list[4][0]}\r", > correct_answer: self.correct_ans_1, > answer_a: answer_list[0][0], > answer_b: answer_list[1][0], > answer_c: answer_list[2][0], > answer_d: answer_list[3][0], > answer_e: answer_list[4][0], > anno_a: answer_list[0][1], > anno_b: answer_list[1][1], > anno_c: answer_list[2][1], > anno_d: answer_list[3][1], > anno_e: answer_list[4][1], > anno_pict_1: answer_list[0][2], > anno_pict_2: answer_list[1][2], > anno_pict_3: answer_list[2][2], > anno_pict_4: answer_list[3][2], > anno_pict_5: answer_list[4][2], > correct_ans_pict: self.correct_ans_pict, > #author: self.author, > question_pict: self.question_pict, > question: self.question, > correct_answer_position: random_insert + 1, > correct_answer_letter: correct_answer_shuffled > } > > I need to target just anno_a thru anno_d > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3d918ff75bfc1a261eed3615e2cde1dd%40ruby-forum.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8D3D4991B4284EED9EAE26CEDB642BB1%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.