From: Jeremy Woertink Date: 2009-11-25T03:59:27+09:00 Subject: Re: Help with each_with_index method Chris Pelow wrote: > Hi there, > I'm looking for help with my work here.I'm just learning Ruby. > Basically I have 5 names entered into an array. They have a firstname > and surname. A middlename is then optional. > What I want to do is to print out the names like this.. > John Seymour Smith > Peter Jones > Mary Walsh > > So it yields the middlename and prints it out if it has one. > > Some of my code is as follows... > > [code] > people << Person.new("John", "Paul", "Smith" ) > people << Person.new("Mary","Barry", "Jones" ) > people << Person.new("Chris","Townsend", "Barry" ) > people << Person.new("Peter","", "Murphy" ) > people << Person.new("Scott","", "Evans" ) > > people_sorted = people.sort > people_sorted.each do |person| > people_sorted.each_with_index do|person, index| > puts "Hi there #@index #@person!" > end > [/code] I was a bit confused by your post, but if I understand correctly, you only want people with a middle name, and then print their names out? ok, well to start, I would recommend including the curly braces when you're using string interpolation. This way you can include your local variables and not just instance variables. people = [] people << Person.new({:firstname => "Billy", :middlename => "Jones", :surname => "Smith"}) people << Person.new({:firstname => "Sally", :middlename => "", :surname => "Johnson"}) people.each do |person| puts "Hello #{person.firstname}" unless person.middlename.empty? end Hope that helps. ~Jeremy Woertink -- Posted via http://www.ruby-forum.com/.