Simple problem with Regular Expression

This simple application is an extension of an exercise from Peter
Cooper's 'Beginning Ruby, from Novice to Professional.' Its purpose is
simple: to collect the name of a fruit and print a sentence describing
the fruits name and its color. I've just learned of regular expressions,
so I've introduced an 'if' statement that modifies the sentence to suit
a name that begins with a vowel, replacing 'a' with 'an.' Unfortunately,
this feature doesn't seem to work. The program prints the sentence, but
doesn't change it if the word begins with a vowel.

Here is the code:
fruit = gets.chomp.downcase

if fruit == "orange"
  color = "orange"
elsif fruit == "apple"
  color = "green"
elsif fruit == "banana"
  color = "yellow"
else
  color = "unknown"
end

fruit.scan(/^./) do |first|
  if first == 'a' || first == 'e' || first = 'i' || first = 'o' || first
= 'u'
      puts "An #{fruit} is #{color}."
  else
      puts "A #{fruit} is #{color}."
  end
end

···

--
Posted via http://www.ruby-forum.com/.

If you are using Ruby 1.9 jthen do:

  fruit[0] => returns the first char of fruit.

···

El Martes, 21 de Julio de 2009, Max Norman escribió:

fruit.scan(/^./) do |first|

--
Iñaki Baz Castillo <ibc@aliax.net>

Ruby code must be beautiful:

···

El Martes, 21 de Julio de 2009, Max Norman escribió:

Here is the code:
fruit = gets.chomp.downcase

if fruit == "orange"
  color = "orange"
elsif fruit == "apple"
  color = "green"
elsif fruit == "banana"
  color = "yellow"
else
  color = "unknown"
end

fruit.scan(/^./) do |first|
  if first == 'a' || first == 'e' || first = 'i' || first = 'o' || first
= 'u'
      puts "An #{fruit} is #{color}."
  else
      puts "A #{fruit} is #{color}."
  end
end

----------------------------------
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
-----------------------------------

:slight_smile:

PS: Just valid for 1.9 due to the usage of "fruit[0]".

--
Iñaki Baz Castillo <ibc@aliax.net>

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

Thanks for this. I've just come across a few of these concepts, like
literal arrays, denoted by %w{}. Could you explain your usage of the
literal array?

···

--
Posted via http://www.ruby-forum.com/\.

PS: Just valid for 1.9 due to the usage of "fruit[0]".

Unfortunately I'm not using 1.9, but I would appreciate any explanation
of this syntax as well. Are you referring to the word 'fruit' as an
array, then specifying the first element of that array?

How would one code this in 1.8?

···

--
Posted via http://www.ruby-forum.com/\.

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...

···

On 7/21/09, Iñaki Baz Castillo <ibc@aliax.net> 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
-----------------------------------

:slight_smile:

PS: Just valid for 1.9 due to the usage of "fruit[0]".

Hi --

Here is the code:
fruit = gets.chomp.downcase

if fruit == "orange"
  color = "orange"
elsif fruit == "apple"
  color = "green"
elsif fruit == "banana"
  color = "yellow"
else
  color = "unknown"
end

fruit.scan(/^./) do |first|
  if first == 'a' || first == 'e' || first = 'i' || first = 'o' || first
= 'u'
      puts "An #{fruit} is #{color}."
  else
      puts "A #{fruit} is #{color}."
  end
end

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]".

You can change it to fruit[0,1] for backward compatibility. (Getting
1.8 to produce a number is harder, as the recent thread on that topic
showed.)

Also, consider tightening up the assignment:

   color = case fruit
           when "orange" then "orange"
           when "apple" then "green"
           when "banana" then "yellow"
           else "unknown"
           end

And this is just for fun:

   puts "A#{"n" if "aeiou".include?(fruit[0,1])} #{fruit} is #{color}"

:slight_smile:

David

···

On Wed, 22 Jul 2009, Iñaki Baz Castillo wrote:

El Martes, 21 de Julio de 2009, Max Norman escribió:

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2\)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN\)

> 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

Thanks for this. I've just come across a few of these concepts, like
literal arrays, denoted by %w{}. Could you explain your usage of the
literal array?

Easy:

my_array = %w{a e i o u}

["a", "e", "i", "o", "u"]

my_array

["a", "e", "i", "o", "u"]

···

El Martes, 21 de Julio de 2009, Max Norman escribió:

--
Iñaki Baz Castillo <ibc@aliax.net>

> PS: Just valid for 1.9 due to the usage of "fruit[0]".

Unfortunately I'm not using 1.9, but I would appreciate any explanation
of this syntax as well. Are you referring to the word 'fruit' as an
array, then specifying the first element of that array?

'fruit' is not an array, but a string.
However, 'fruit'[0] returns the first character of the string (note that in
Ruby a character is also a string).

How would one code this in 1.8?

fruit[/^(.)/]

···

El Martes, 21 de Julio de 2009, Max Norman escribió:

--
Iñaki Baz Castillo <ibc@aliax.net>

How would one code this in 1.8?

fruit[/^(.)/]

This works perfectly.

···

--
Posted via http://www.ruby-forum.com/\.