Simple problem with Regular Expression

M

Max Norman

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
 
I

Iñaki Baz Castillo

El Martes, 21 de Julio de 2009, Max Norman escribi=C3=B3:
Here is the code:
fruit =3D gets.chomp.downcase

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

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


Ruby code must be beautiful:

=2D---------------------------------
fruit =3D gets.chomp.downcase

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

if %w{a e i o u}.include?(fruit[0])
puts "An #{fruit} is #{color}."
else
puts "A #{fruit} is #{color}."
end
=2D----------------------------------

:)


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


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
M

Max Norman

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

Iñaki Baz Castillo

El Martes, 21 de Julio de 2009, Max Norman escribi=C3=B3:
fruit =3D gets.chomp.downcase

case fruit
when "orange"
color =3D "orange"
when "apple"
color =3D "green"
when "banana"
color =3D "yellow"
else
color =3D "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:

irb> my_array =3D %w{a e i o u}
["a", "e", "i", "o", "u"]

irb> my_array
["a", "e", "i", "o", "u"]



=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
M

Max Norman

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

Caleb Clausen

Ruby code must be beautiful:

----------------------------------
fruit =3D gets.chomp.downcase

case fruit
when "orange"
color =3D "orange"
when "apple"
color =3D "green"
when "banana"
color =3D "yellow"
else
color =3D "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=3D{
'orange'=3D>'orange', 'apple'=3D>'red', 'banana'=3D>'yellow'
}
fruit =3D gets.chomp.downcase
color=3DFRUIT2COLOR[fruit] || 'unknown'
if /^[aeiou]/=3D=3D=3Dfruit
article=3D"An"
else
article=3D"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...
 
I

Iñaki Baz Castillo

El Martes, 21 de Julio de 2009, Max Norman escribi=C3=B3:
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=
=20
Ruby a character is also a string).

How would one code this in 1.8?

fruit[/^(.)/]


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top