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: [email]dblackwobblini.net[/email] # New email address
work: [email]blackdavshu.edu[/email]
Web: [url]http://pirate.shu.edu/~blackdav[/url]