Anonymous user
The Name Game: Difference between revisions
→{{header|Ruby}}
(The Name Game en BASIC256) |
|||
Line 1,930:
=={{header|Ruby}}==
{{works with|Ruby|2.7.1}}
More idiomatic Ruby while not being object-oriented.
Also, like the Commodore Basic version, we handle consonants at the start of the name using a Regular Expression so we can handle appropriately names like Steve or Chris. Byron is an interesting edge-case because has a "y" and also starts with "b".
x = name.capitalize▼
<lang ruby>#!/usr/bin/env ruby
when 'A','E','I','O','U' then name.downcase▼
else x[1..-1]▼
end▼
case x[0]▼
when 'B' then b = y▼
when 'F' then f = y▼
when 'M' then m = y▼
end▼
def print_verse(name)
first_letter_and_consonants_re = /^.[^aeiyou]*/i
print "Banana-fana fo-", f, "\n"▼
print "Fee-fi-mo-", m, "\n"▼
suffixed = case full_name[0] # Y
name.downcase
else
full_name.sub(first_letter_and_consonants_re, '')
b_name = "b#{suffixed}"
f_name = "f#{suffixed}"
m_name = "m#{suffixed}"
b_name = suffixed
f_name = suffixed
m_name = suffixed
puts <<~END_VERSE
#{full_name}, #{full_name}, bo-#{b_name}
#{full_name}!
END_VERSE
%w[Gary Earl Billy Felix Mary Steve Chris Byron].each do |name|
print_verse name
end
▲end</lang>
{{out}}
<pre>Gary, Gary, bo-bary
Line 1,981 ⟶ 1,999:
Mary!
Steve, Steve, bo-
Banana-fana fo-
Fee-fi-mo-
Steve!
Chris, Chris, bo-bis
Banana-fana fo-fis
Fee-fi-mo-mis
Chris!
Byron, Byron, bo-yron
Banana-fana fo-fyron
Fee-fi-mo-myron
Byron!
</pre>
=={{header|Scala}}==
|