>From: Clemens Hintze <c.hintze / gmx.net> >Reply-To: ruby-talk / netlab.co.jp >To: ruby-talk / netlab.co.jp (ruby-talk ML) >Subject: [ruby-talk:02172] Ruby - Show Me >Date: Sun, 26 Mar 2000 15:46:32 +0200 (CEST) > >Hi, > >Dave has already shown a excellent solution. Only to show that there >is more than one way to go, I would like to add my solution here. Just >to be different I do not use Hashes: > > >alphabet = %w(Alpha Bravo Charlie Delta Echo Foxtrott Golf Hotel > India Juliet Kilo Lima Mike November Oscar Papa Quebec > Romeo Sierra Tango Uniform Victor Whisky X-Ray Yankee Zulu) > > >for word in ARGV > word.upcase.each_byte do |letter| > puts(alphabet[letter - ?A] || letter.chr) if letter > ?A > end >end > > >\cle > >-- >Clemens Hintze mailto: c.hintze / gmx.net This is a nice application of Array vs Hash in Ruby. There is a slight difference between the two versions though, given the string "abc defg" as input, Dave's version produces: Alpha Bravo Charlie Delta Echo Foxtrott Golf While your version produces: Bravo Charlie Delta Echo Foxtrott Golf The first spelling 'Alpha' is totally missing and there is no blank line between the two words "abc" & "defg". Your version ignores all 'a' characters in the input string no matter where they are. It's easy to spot it from the way you handle the index into the array, a typical 'C' way of doing thing, it makes the short script even less readable than Dave's version. My advice is when you program in a particular language, use the idioms of that very language. Don't do it the 'A', 'B' or 'C' way. Scripting language is at the foremost strongest by using regular expression whenever applicable in text processing. Bravo Dave, you are really the champion! By now you should realize that I pay attention to details and deserve a look at the draft of your book, unless you prefer an errata. Dat ______________________________________________________ Get Your Private, Free Email at http://www.hotmail.com