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

El Martes, 21 de Julio de 2009, Max N. escribió:

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

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 N. escribió:

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

:slight_smile:

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

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?

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?

On 7/21/09, Iñaki Baz C. [email protected] wrote:

when “banana”

:slight_smile:

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…

El Martes, 21 de Julio de 2009, Max N. escribió:

color = “unknown”
literal array?
Easy:

irb> my_array = %w{a e i o u}
[“a”, “e”, “i”, “o”, “u”]

irb> my_array
[“a”, “e”, “i”, “o”, “u”]

How would one code this in 1.8?

fruit[/^(.)/]

This works perfectly.

El Martes, 21 de Julio de 2009, Max N. escribió:

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[/^(.)/]

Hi –

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

color = “yellow”
end
color = “orange”
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs