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