From: Rob Biedenharn Date: 2009-11-25T07:55:19+09:00 Subject: Re: Help with each_with_index method On Nov 24, 2009, at 5:40 PM, Chris Pelow wrote: >> Now, given your requirement for how you'd like the names to be >> printed, I think I'd also add a full_name() method to the Person >> class, something like: >> >> class Person >> def full_name >> unless middlename.empty? >> "#{firstname} #{middlename} #{surname}" >> else >> "#{firstname} #{surname}" >> end >> end >> end >> >> Finally, there are several issues with the bit at the end where >> you're >> trying to iterate over the array of people. You have two loops (an >> #each loop and then an #each_with_index loop) nested inside each >> other. For the inner loop, the "index" and "person" arguments that >> each_with_index() passes into the block aren't instance variables, so >> you just refer to them as "index" and "person", not "@index" and >> "@person". I'd change that last bit to look like this: >> >> people_sorted = people.sort >> people_sorted.each_with_index do |person, index| >> puts "Hi there #{index} #{person.full_name}!" >> end >> >> Hope this helps, >> >> Lyle > > Hi Lyle, > > Thank you very much for your time in helping me here and for your > suggestions. You have also helped me understand it alot more so thank > you for that. I have made the suggested changes exactly how you > suggested however I am getting syntax errors for that full_name method > you gave me. Have I done it right? > > Thanks alot, > Chris. > > Attachments: > http://www.ruby-forum.com/attachment/4290/Person2.rb > > -- > Posted via http://www.ruby-forum.com/. Look very closely at the line having the error and match up all the paired delimiters. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com