From: dblack@... Date: 2003-11-21T01:36:12+09:00 Subject: Re: learning the "Ruby way" Hi -- On Thu, 20 Nov 2003, Mark Wirdnam wrote: > **Hobby-programmer alarm** > > Hello! > I am trying to learn Ruby, but with the goal of understanding new > elegant solutions, not to repeat the same messy solutions I usually > come up with. > Here is an example I would like to use to improve my understanding. > Any pointers would be great. > > Problem: > I have a sorted list ('alfred', 'boris', 'bruce', 'claire', 'dean', 'donald') > and would like to generate or return ('-a-', 'alfred', '-b-', 'boris', > 'bruce', '-c-', 'claire', '-d-', 'dean', 'donald'). This may be a bit ill-advised, because it changes the array as it iterates through it (though in such a way that I think it can actually be shown to be safe -- ? ). Anyway, just for fun: a =['alfred', 'boris', 'bruce', 'claire', 'dean', 'donald'] a.map {|s| s[0]}.uniq.each {|c| a.insert(a.index(a.detect {|t| t[0] == c}), "-#{c.chr}-") } a # => ["-a-", "alfred", "-b-", "boris", "bruce", "-c-", "claire", "-d-", "dean", "donald"] David -- David Alan Black home: dblack@wobblini.net # New email address work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav