From: Caleb Clausen Date: 2009-07-22T06:36:56+09:00 Subject: Re: Simple problem with Regular Expression On 7/21/09, Iņaki Baz Castillo wrote: > Ruby code must be beautiful: > > ---------------------------------- > fruit = gets.chomp.downcase > > case fruit > when "orange" > color = "orange" > when "apple" > color = "green" > when "banana" > color = "yellow" > else > color = "unknown" > end > > if %w{a e i o u}.include?(fruit[0]) > puts "An #{fruit} is #{color}." > else > puts "A #{fruit} is #{color}." > end > ----------------------------------- > > :) > > > PS: Just valid for 1.9 due to the usage of "fruit[0]". But, you can do better: FRUIT2COLOR={ 'orange'=>'orange', 'apple'=>'red', 'banana'=>'yellow' } fruit = gets.chomp.downcase color=FRUIT2COLOR[fruit] || 'unknown' if /^[aeiou]/===fruit article="An" else article="A" end puts "#{article} #{fruit} is #{color}." Works perfectly in 1.8. PS: since this program seems to assume english input, you can also assume ascii encoding, and therefore your version works fine in 1.8 too. PPS: Hmm, I seem to have subconsciously repainted the apples red. I was going to point out that apples are stereotypically red, and that the bananas you can actually buy in the store are actually green...