public
Description: Tradução do livro Why's (Poignant) Guide to Ruby para português.
Homepage: http://poignantguide.net/ruby/
Clone URL: git://github.com/carlosbrando/poignant-br.git
arquivo 68b traduzido
anderson (author)
Sun Jul 20 20:44:58 -0700 2008
commit  05dd969eddb0c347dc186ae69358f833411147bb
tree    3b185c7a49ac6ea60d9d03770dc618b20246d46f
parent  baef3e186f20b5023d9ec9c41d7152ffb0b76d70
...
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
43
 
 
44
45
46
 
47
48
49
50
 
 
51
52
53
54
55
56
57
58
59
60
61
 
 
 
 
 
 
 
 
62
63
64
 
 
65
66
67
68
69
70
 
 
 
 
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 
 
 
 
 
 
 
 
 
 
 
85
86
87
88
89
90
 
91
92
93
94
95
 
 
96
97
98
 
99
100
 
101
102
103
 
104
105
106
107
108
109
110
 
 
111
112
113
114
115
 
 
 
116
117
118
 
119
120
121
122
123
 
 
 
124
125
126
127
 
 
128
129
130
 
131
132
133
134
135
 
 
 
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
 
 
 
 
 
 
 
 
 
 
 
 
151
152
153
154
 
 
 
155
156
157
158
159
160
161
162
 
 
 
 
 
 
 
163
164
165
166
167
 
 
 
 
168
169
170
171
 
 
172
173
174
...
 
 
 
 
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
43
44
45
 
46
47
48
 
 
49
50
51
52
53
 
 
 
 
 
 
 
 
54
55
56
57
58
59
60
61
62
 
 
63
64
65
66
 
 
 
 
67
68
69
70
71
72
73
 
 
 
 
 
 
 
 
 
 
 
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
90
91
92
93
 
 
94
95
96
97
 
98
99
 
100
101
102
 
103
104
105
106
107
108
 
 
109
110
111
112
 
 
 
113
114
115
116
117
 
118
119
120
 
 
 
121
122
123
124
125
 
 
126
127
128
129
 
130
131
132
 
 
 
133
134
135
136
137
138
 
 
 
 
 
 
 
 
 
 
 
 
139
140
141
142
143
144
145
146
147
148
149
150
151
 
 
 
152
153
154
155
 
 
 
 
 
 
 
156
157
158
159
160
161
162
163
 
 
 
 
164
165
166
167
168
169
 
 
170
171
172
173
174
0
@@ -1,174 +1,174 @@
0
- The program asks for a monster. If you type in @Dragon@, then the @monster_class@ variable
0
- will contain the string @"Dragon"@. Inside the @eval@, a few strings get added together
0
- to make the string @"monster = Dragon.new"@. And when the @eval@ executes this string,
0
- the @monster@ variable contains a @Dragon@ object. Ready for battle.
0
+ O programa pede por um monstro. Se você digitar @Dragao@, então a variável da @classe_monstro@
0
+ irá conter a string @"Dragao"@. Dentro do @eval@ algumas strings serão adicionadas juntas
0
+ para formar a string @"monstro = Dragao.new"@. E quando o @eval@ executa essa string, a
0
+ variável @monstro@ contém um objeto @Dragao@. Pronto pra batalha.
0
 
0
 
0
- This is great! Now we can leave it up to the player to pick a monster! Of course,
0
- we're trusting the player to supply a real monster class. If they type in @BotanicalWitch@
0
- and there is no @BotanicalWitch@ class, they'll get an exception tossed in their face.
0
+ Isso é maravilhoso! Agora podemos deixar que o jogador escolha um monstro! Claro, estamos
0
+ confiando no jogador para que ele forneça uma classe real de monstro. Se eles digitarem
0
+ @BruxaBotanica@ e não existe a classe @BruxaBotanica@, eles terão uma exceção jogada na sua cara.
0
 
0
 
0
- So, in short, @eval@ lets you make up code as you go. Which can be useful and which can
0
- be dangerous as well.
0
+ Então, em resumo, o @eval@ permite que você crie um código a medida que prossegue. O que
0
+ pode ser útil e pode ser perigoso ao mesmo tempo.
0
 
0
 
0
- The @instance_eval@ and @class_eval@ method used in the metaprogramming for the @Creature@
0
- class are slightly different from the normal @eval@. These two special methods run code just like
0
- @eval@ does, but they duck into classes and objects and run the code there.
0
+ Os métodos @instance_eval@ e @class_eval usado na metaprogramação para a classe @Criatura@
0
+ são levemente diferente do normal @eval@. Esses dois métodos especiais passam o código assim
0
+ como o @eval@ faz, mas eles mergulham em classes e objetos e passam o código lá.
0
 
0
 
0
         <pre>
0
- # The instance_eval method runs code as if it were run inside an
0
- # object's instance method.
0
- irb> drgn = Dragon.new
0
+ # O método instance_eval executa o código como se ele fosse executado dentro
0
+ # do objeto do método de instância.
0
+ irb> drgn = Dragao.new
0
          irb> drgn.instance_eval do
0
- irb> @name = "Tobias"
0
+ irb> @nome = "Tobias"
0
          irb> end
0
 
0
- irb> drgn.instance_variable_get( "@name" )
0
+ irb> drgn.instance_variable_get( "@nome" )
0
            => "Tobias"
0
 
0
- # The class_eval method runs code is if inside a class definition.
0
- irb> Dragon.class_eval do
0
- irb> def name; @name; end
0
+ # O método class_eval executa o código se estiver dentro da definição de classe.
0
+ irb> Dragao.class_eval do
0
+ irb> def nome; @nome; end
0
          irb> end
0
 
0
- irb> drgn.name
0
+ irb> drgn.nome
0
            => "Tobias"
0
         </pre>
0
 
0
 
0
- As you can see above, the @instance_eval@ and @class_eval@ methods also can take a code block
0
- instead of a string. Which is just how things are done in Dwemthy's Array.
0
+ Como você pode ver acima, os métodos @instance_eval@ e @class_eval@ também podem pegar um bloco
0
+ de código ao invés de uma string. O que é exatamente como as coisas foram feitas no Labirinto de Dwemthy.
0
 
0
 
0
- h3. Enough Belittling Instruction and Sly Juxtaposition -- Where Is Dwemthy's Array??
0
+ h3. Bastante instrução depreciante e Ardilosa Justaposição -- Onde está o Labirinto de Dwemthy??
0
 
0
 
0
- Tread carefully -- here is *the other half of DWEMTHY'S ARRAY!!*
0
- *Add these lines to @dwemthy.rb@.*
0
+ Vá com cuidado -- aqui está *a outra metade do LABIRINTO DE DWEMTHY!!*
0
+ *Adicione essas linhas a @dwemthy.rb@.*
0
 
0
 
0
         <pre>
0
- class Creature
0
-
0
- # This method applies a hit taken during a fight.
0
- def hit( damage )
0
- p_up = rand( charisma )
0
- if p_up % 9 == 7
0
- @life += p_up / 4
0
- puts "[#{ self.class } magick powers up #{ p_up }!]"
0
+ class Criatura
0
+
0
+ # Este método aplica um golpe recebido durante uma luta.
0
+ def golpear( dano )
0
+ aumento_poder = rand( carisma )
0
+ if aumento_poder % 9 == 7
0
+ @vida += aumento_poder / 4
0
+ puts "[Aumento de poderes mágicos de #{ self.class } #{ aumento_poder }!]"
0
              end
0
- @life -= damage
0
- puts "[#{ self.class } has died.]" if @life <= 0
0
+ @vida -= dano
0
+ puts "[#{ self.class } está morto.]" if @vida <= 0
0
            end
0
 
0
- # This method takes one turn in a fight.
0
- def fight( enemy, weapon )
0
- if life <= 0
0
- puts "[#{ self.class } is too dead to fight!]"
0
+ # Este método obtém uma rodada em uma luta.
0
+ def lutar( inimigo, arma )
0
+ if vida <= 0
0
+ puts "[#{ self.class } está muito morto para lutar!]"
0
                return
0
              end
0
 
0
- # Attack the opponent
0
- your_hit = rand( strength + weapon )
0
- puts "[You hit with #{ your_hit } points of damage!]"
0
- enemy.hit( your_hit )
0
-
0
- # Retaliation
0
- p enemy
0
- if enemy.life > 0
0
- enemy_hit = rand( enemy.strength + enemy.weapon )
0
- puts "[Your enemy hit with #{ enemy_hit } points of damage!]"
0
- self.hit( enemy_hit )
0
+ # Ataca o opononte
0
+ seu_golpe = rand( forca + arma )
0
+ puts "[Seu golpe com #{ seu_golpe } pontos de dano!]"
0
+ inimigo.golpear( seu_golpe )
0
+
0
+ # Retaliação
0
+ p inimigo
0
+ if inimigo.vida > 0
0
+ inimigo_golpe = rand( inimigo.forca + inimigo.arma )
0
+ puts "[Seu inimigo golpeia com #{ inimigo_golpe } pontos de dano!]"
0
+ self.golpear( inimigo_golpe )
0
              end
0
            end
0
 
0
          end
0
 
0
- class DwemthysArray < Array
0
+ class LabirintoDwemthy < Array
0
            alias _inspect inspect
0
            def inspect; "#<#{ self.class }#{ _inspect }>"; end
0
            def method_missing( meth, *args )
0
- answer = first.send( meth, *args )
0
- if first.life <= 0
0
+ resposta = first.send( meth, *args )
0
+ if first.vida <= 0
0
                shift
0
                if empty?
0
- puts "[Whoa. You decimated Dwemthy's Array!]"
0
+ puts "[Uauu. Você dizimou o Labirinto de Dwemthy!]"
0
                else
0
- puts "[Get ready. #{ first.class } has emerged.]"
0
+ puts "[Prepare-se. #{ first.class } surgiu.]"
0
                end
0
              end
0
- answer || 0
0
+ resposta || 0
0
            end
0
          end
0
         </pre>
0
 
0
 
0
- This code adds two methods to @Creature@. The @hit@ method which reacts to a hit from another
0
- @Creature@. And the @fight@ method which lets you place your own blows against that @Creature@.
0
+ Esse código adiciona dois métodos a @Criatura@. O método @golpear@ que reage ao golpe de outra
0
+ @Criatura@. E o método @lutar@ que permite que você coloque os seus próprios golpes contra aquela @Criatura@.
0
 
0
 
0
- When your @Creature@ takes a hit, a bit of defense kicks in and your @charisma@ value is used to generate a power-up. Don't
0
- ask me to explain the secrets behind this phenomenon. A random number is picked, some simple math
0
- is done, and, if you're lucky, you get a couple life points. @@life += p_up / 4@.
0
+ Quando a sua @Criatura@ leva um golpe, um pouco da defesa contribui e o seu valor @carisma@ é usado para gerar um aumento de
0
+ poder. Não me peça para explicar os segredos por trás deste fenômeno. Um número aleatório é escolhido, uma matemática
0
+ simples é feita e se você tiver sorte, você consegue alguns pontos vitais. @@vida += aumento_poder / 4@.
0
 
0
 
0
- Then, the enemy's blow is landed. @@life -= damage@. That's how the @Creature#hit@ method works.
0
+ Então o golpe do inimigo está dado. @@vida -= dano@. É assim que o método da @Criatura#golpear@ trabalha.
0
 
0
 
0
- The @fight@ method checks to see if your @Creature@ is alive. Next, a random
0
- hit is placed on your opponent. If your opponent lives through the hit, it gets
0
- a chance to strike back. Those are the workings of the @Creature#fight@ method.
0
+ O método @lutar@ checa se a sua @Criatura@ está viva. Em seguida, um golpe aleatório
0
+ é dado no seu oponente. Se o seu oponente sobreviver a este golpe, ele ganha uma
0
+ chance de atacar de volta. Estas são as atividades do método @Criatura#lutar@.
0
 
0
 
0
- I'll explain @DwemthysArray@ in a second. I really will. I'm having fun doing it.
0
- Let's stick with hitting and fighting for now.
0
+ Irei explicar o @LabirintoDwemthy@ em um segundo. Realmente explicarei. Estou me divertindo fazendo isso.
0
+ Por hora, vamos nos concentrar em golpear e lutar.
0
 
0
 
0
- h3. Introducing: You.
0
+ h3. Apresentando: Você.
0
 
0
 
0
- You may certainly tinker with derivations on this rabbit. But official
0
- Dwemthy Paradigms explicitly denote the code -- and the altogether character -- inscribed
0
- below. *Save this as @rabbit.rb@.*
0
+ Você talvez tenha trabalhado com derivações neste coelho. Mas o oficial
0
+ Paradigma de Dwemthy explicitamente denota o código -– e os caracteres todos juntos -–
0
+ inscritos abaixo. *Salve isto como @rabbit.rb@.*
0
 
0
 
0
         <pre>
0
- class Rabbit < Creature
0
- traits :bombs
0
-
0
- life 10
0
- strength 2
0
- charisma 44
0
- weapon 4
0
- bombs 3
0
-
0
- # little boomerang
0
- def ^( enemy )
0
- fight( enemy, 13 )
0
+ class Coelho < Criatura
0
+ caracteristicas :bombas
0
+
0
+ vida 10
0
+ forca 2
0
+ carisma 44
0
+ arma 4
0
+ bombas 3
0
+
0
+ # bumeranguinho
0
+ def ^( inimigo )
0
+ lutar( inimigo, 13 )
0
            end
0
- # the hero's sword is unlimited!!
0
- def /( enemy )
0
- fight( enemy, rand( 4 + ( ( enemy.life % 10 ) ** 2 ) ) )
0
+ # a espada do herói é ilimitada!!
0
+ def /( inimigo )
0
+ lutar( inimigo, rand( 4 + ( ( inimigo.vida % 10 ) ** 2 ) ) )
0
            end
0
- # lettuce will build your strength and extra ruffage
0
- # will fly in the face of your opponent!!
0
- def %( enemy )
0
- lettuce = rand( charisma )
0
- puts "[Healthy lettuce gives you #{ lettuce } life points!!]"
0
- @life += lettuce
0
- fight( enemy, 0 )
0
+ # alface irá formar a sua força e extras substâncias indigestas
0
+ # voarão na cara de seu oponente!!
0
+ def %( inimigo )
0
+ alface = rand( carisma )
0
+ puts "[Saudável alface lhe dá #{ alface } pontos de vida!!]"
0
+ @vida += alface
0
+ lutar( inimigo, 0 )
0
            end
0
- # bombs, but you only have three!!
0
- def *( enemy )
0
- if @bombs.zero?
0
- puts "[UHN!! You're out of bombs!!]"
0
+ # bombas, mas você possui somente três!!
0
+ def *( inimigo )
0
+ if @bombas.zero?
0
+ puts "[HUMM!! Você está sem bombas!!]"
0
                return
0
              end
0
- @bombs -= 1
0
- fight( enemy, 86 )
0
+ @bombas -= 1
0
+ lutar( inimigo, 86 )
0
            end
0
          end
0
         </pre>

Comments

    No one has commented yet.