′, ‘multi-Tier’, ‘30,000 foot’, ‘B-to-B’, ‘win-win’, ‘front-end’, ‘web-based’, ‘pervasive’, ’smart’, ’six-sigma’, ‘critical-path’, ‘dynamic’]
word_list_two = [’empowered’, ’sticky’, ‘value-added’, ‘oriented’, ‘centric’, ‘distributed’, ‘clustered’, ‘branded’, ‘outside-the-box’, ‘positioned’, ‘networked’, ‘focused’, ‘leveraged’, ‘aligned’, ‘targeted’, ’shared’, ‘cooperative’, ‘accelerated’]
word_list_three = [’process’, ‘tipping-point’, ’solution’, ‘architecture’, ‘core competency’, ’strategy’, ‘mindshare’, ‘portal’, ’space’, ‘vision’, ‘paradigm’, ‘mission’]
one_len = word_list_one.length
two_len = word_list_two.length
three_len = word_list_three.length
rand1 = rand(one_len)
rand2 = rand(two_len)
rand3 = rand(three_len)
phrase = word_list_one[rand1] + ” ” + word_list_two[rand2] + ” ” + word_list_three[rand3]
puts phrase
Questo programma produce tre liste di parole, e poi successivamente prende una parola da ognuna delle tre liste e mostra il risultato. Quello che segue è un test tratto dal libro di Chris Pine: “Scrivere un programma Deaf Gradma” (nonna sorda). Qualunque cosa si dice alla nonna (qualunque cosa si scriva), lei risponderà con Eh?! PARLA, SONNY!, a meno che non lo si gridi ed ecco spiegato il motivo delle lettere maiuscole. Se si grida, la nonna può sentire (o almeno sente qualcosa) e grida NO, NON DAL 1938! Per rendere questo programma il più credibile possibile e far gridare alla nonna un anno diverso ogni volta, si potrebbe scegliere un numero a caso tra 1930 e 1950. Per interrompere il dialogo con la nonna, basterà gridare BYE.
# p026zdeafgm1.rb
a = ( 1930…1951).to_a
puts ‘Inserisci la tua risposta: ‘
STDOUT.flush
until (response = gets.chomp).eql?(’BYE’)
if (response.eql?(response.upcase ))
puts ‘NO, NON DAL ‘ + a[rand(a.size)].to_s + ‘ !’
else
puts ‘EH?! PARLA, SONNY!’
end
puts ‘Enter your response: ‘
STDOUT.flush
end
A questo punto proviamo ad estendere il programma Deaf Grandma: e se la nonna non vuole andarsene? Quando si grida BYE, lei potrebbe non sentirvi. Quindi bisognerà cambiare il precedente programma, così da gridare per tre volte BYE nella stessa riga. Si può testare il programma, e se si scrive tre volte BYE, ma non nella stessa riga, si continuerà a discorrere con la nonna.
# p026zdeafgm2.rb
a = ( 1930…1951).to_a
puts ‘Inserisci la tua risposta: ‘
STDOUT.flush
until (response = gets.chomp).eql?(’BYE BYE BYE’)
if response.eql?(’BYE’)
# do nothing
elsif response.eql?(response.upcase)
puts ‘NO, NON DAL ‘ + a[rand(a.size)].to_s + ‘ !’
else
puts ‘Eh?! PARLA, SONNY!’
end
puts ‘Enter your response: ‘
STDOUT.flush
end
LEZIONI PRECEDENTI:
29 Agosto 2007: Introduzione
30 Agosto 2007: I numeri
31 Agosto 2007: Le stringhe
3 Settembre 2007: Le variabili
5 Settembre 2007: Le conversioni
12 Settembre 2007: I metodi gets e chomp
17 Settembre 2007: Lo scopo delle variabili
19 Settembre 2007: I nomi
20 Settembre 2007: Il metodo self
21 Settembre 2007: Scrivere un proprio metodo (1/2)
25 Settembre 2007: Scrivere un proprio metodo (2/2)
26 Settembre 2007: Ancora sulle stringhe
28 Settembre 2007: method_missing
2 Ottobre 2007: I costrutti
3 Ottobre 2007: Il range
4 Ottobre 2007: Gli array
8 Ottobre 2007: SMTP class
9 Ottobre 2007: Confronto fra Java e Ruby
10 Ottobre 2007: Interactive Ruby Shell
di Satish Talim - Programmazione.it