forked from eifion/asciicasts.com-translations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
it.html
executable file
·42 lines (41 loc) · 3.19 KB
/
it.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<p>La strana sintassi nella linea del find nel codice riportato qui sotto, con il simbolo preceduto dal carattere ampersand, è detta Symbol to_proc ed è un qualcosa che Rails aggiunge a Ruby.</p>
<pre class="ruby">
class Project < ActiveRecord::Base
has_many :tasks
def self.all_names
find(:all).collect(&:name)
end
end
</pre>
<p>Useremo la facility Rails <code>script/console</code> per spiegare come funziona. Per prima cosa, prendiamo un array di <code>Project</code>.</p>
<code class="terminal">
asalicetti$ script/console
Loading development environment (Rails 2.3.5)
>> projects = Project.find(:all)
=> [#<Project id: 1, name: "Scrivi un altro ASCIIcast", created_at: "2010-04-12 12:32:40", updated_at: "2010-04-12 12:32:40">, #<Project id: 2, name: "Vai fuori a passeggio", created_at: "2010-04-12 12:35:20", updated_at: "2010-04-12 12:35:20">]
</code>
<p>Ci sono due progetti e l’obiettivo è quello di ottenere un array contenente i nomi di tutti i progetti. Un modo per farlo sarebbe quello di usare il metodo <code>collect</code>, che accetta un blocco e restituisce il <code>name</code> di ogni <code>Project</code>. Il metodo <code>collect</code> iterata su di un array, esegue del codice (definito nel blocco) su ogni elemento dell’array, e restituisce un array contenente ogni risultato del codice del blocco eseguito su ogni singolo elemento dell’array originale.</p>
<code class="terminal">
>> projects.collect { |p| p.name }
=> ["Scrivi un altro ASCIIcast", "Vai fuori a passeggio"]
</code>
<p>Symbol to_proc è una scorciatoia utilizzabile ogni volta in cui, come in questo caso, si vuole chiamare un metodo sull’oggetto che è mandato al blocco. Per fare ciò, si passa un parametro che inizia con un ampersand, seguito dal nome del metodo da invocare (nella sua forma simbolica):</p>
<code class="terminal">
>> projects.collect {&:name}
=> ["Scrivi un altro ASCIIcast", "Vai fuori a passeggio"]
</code>
<p>Questa scorciatoia diventa particolarmente utile quando si vogliono combinare chiamate multiple:</p>
<code class="terminal">
>> projects.collect(&:name).collect(&:upcase)
=> ["SCRIVI UN ALTRO ASCIICAST", "VAI FUORI A PASSEGGIO"]
</code>
<p>Ovviamente questa tecnica non è limitata al solo metodo <code>collect</code>; più in generale, può essere applicata a qualsiasi metodo che accetta un blocco. Per esempio i metodi <code>any?</code> e <code>all?</code> possono usare questa scorciatoia per vedere se tutti (all) o almeno uno (any) dei progetti sono validi, o persino per salvare tutti i nostri progetti.</p>
<code class="terminal">
>> projects.all?(&:valid?)
=> true
>> projects.any?(&:valid?)
=> true
>> projects.each(&:save!)
=> [#<Project id: 1, name: "Scrivi un altro ASCIIcast", created_at: "2010-04-12 12:32:40", updated_at: "2010-04-12 12:32:40">, #<Project id: 2, name: "Vai fuori a passeggio", created_at: "2010-04-12 12:35:20", updated_at: "2010-04-12 12:35:20">]
</code>
<p>Nonostante la sua sintassi un tantino strana, Symbol to_proc è molto utile se usata quando si ha da concatenare metodi con blocchi.</p>