-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatom.xml
More file actions
371 lines (269 loc) · 36.7 KB
/
Copy pathatom.xml
File metadata and controls
371 lines (269 loc) · 36.7 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Code Gazer]]></title>
<link href="http://icelle.github.io/atom.xml" rel="self"/>
<link href="http://icelle.github.io/"/>
<updated>2013-12-04T13:00:00-05:00</updated>
<id>http://icelle.github.io/</id>
<author>
<name><![CDATA[Icelle Ku]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Breakable Toy: Sprout]]></title>
<link href="http://icelle.github.io/blog/2013/12/01/breakable-toy/"/>
<updated>2013-12-01T12:00:00-05:00</updated>
<id>http://icelle.github.io/blog/2013/12/01/breakable-toy</id>
<content type="html"><![CDATA[<p>My first “big” coding project is called Sprout. It’s an app that tracks children’s growth rate (weight and height) over time. Here’s a quick summary of my user stories and acceptance criteria—</p>
<div style="background-color: #f0f0f0; border: 1px solid black; padding: 10px;">
<ul style="list-style-type: none; padding: 0; margin: 0">
<li>As a user,</li>
<li>I want to add a child/children</li>
<li>So I can start tracking weight and height.</li>
<ul style="padding: 0 0 0 2em; margin: 0">
<li>User must add a child.</li>
<li>User can add multiple children.</li>
<li>User must enter each child’s birth date, gender and starting height and weight.</li>
</ul>
</div>
<div style="background-color: #f0f0f0; border: 1px solid black; padding: 10px; margin: 1em 0 0 0">
<ul style="list-style-type: none; padding: 0; margin: 0">
<li>As a user,</li>
<li>I can update child’s height and weight or edit existing information about child</li>
<li>So my child’s information is accurate.</li>
<ul style="padding: 0 0 0 2em; margin: 0">
<li>User can click update button to enter new height and weight for a specific child.</li>
<li>User can click edit button to change/add information.</li>
</ul>
</div>
<!-- more -->
<div style="background-color: #f0f0f0; border: 1px solid black; padding: 10px; margin: 1em 0 0 0">
<ul style="list-style-type: none; padding: 0; margin: 0">
<li>As the program,</li>
<li>I want to produce a graph that shows me timeline of growth rate for each/all children</li>
<li>So user can compare weight and height.</li>
<ul style="padding: 0 0 0 2em; margin: 0">
<li>Track date/time of when user enters the information (automatically or have user specify date/time).</li>
<li>Must have a graph legend for each child.</li>
<li>User can switch from one child to another by clicking on the legend buttons.</li>
<li>User can click “all” legend button to see graph of all children as a group.</li>
</ul>
</div>
<div style="background-color: #f0f0f0; border: 1px solid black; padding: 10px; margin: 1em 0 0 0">
<ul style="list-style-type: none; padding: 0; margin: 0">
<li>As a user </li>
<li>I want to see my child’s average, mean and median height and weight as an individual</li>
<li>So I can accurately track my child’s progress.</li>
<ul style="padding: 0 0 0 2em; margin: 0">
<li>Calculate individual child’s lowest weight, include time stamp.</li>
<li>Calculate individual child’s highest weight, include time stamp.</li>
<li>Calculate individual child’s average weight, include time stamp.</li>
</ul>
</div>
<div style="background-color: #f0f0f0; border: 1px solid black; padding: 10px; margin: 1em 0 0 0">
<ul style="list-style-type: none; padding: 0; margin: 0">
<li>As the program</li>
<li>I want to allow users to see national average in comparison with child’s stats</li>
<li>So user knows if child’s development is on the right track compared to the national average.</li>
<ul style="padding: 0 0 0 2em; margin: 0">
<li>Compare child’s weight and height to national average based on gender and age.</li>
<li>Show national average on individual child’s graph.</li>
</ul>
</div>
<p style= "padding: 15px 0 0 0"> If I have extra time, I may work on the following nice-to-have functionalities:</p>
<div style="background-color: #f0f0f0; border: 1px solid black; padding: 10px; margin: 1em 0 0 0">
<ul style="list-style-type: none; padding: 0; margin: 0">
<li>As a user</li>
<li>I want to get a badge every time my child meets a certain milestones</li>
<li>So I can celebrate!</li>
<ul style="padding: 0 0 0 2em; margin: 0">
<li>Notify and give badge to user when child reaches certain age, weight or height. For example, “Congratulations! Your child is now x months old!” or “Congratulations! Your child is now 20 lbs!”</li>
</ul>
</div>
<div style="background-color: #f0f0f0; border: 1px solid black; padding: 10px; margin: 1em 0 0 0">
<ul style="list-style-type: none; padding: 0; margin: 0">
<li>As a user</li>
<li>I want to upload my child’s picture and use that as the legend</li>
<li>So that I can easily differentiate each child in the graph if I have multiple children.</li>
<ul style="padding: 0 0 0 2em; margin: 0">
<li>Allow user to upload picture and use that picture as legend for the graph.</li>
</ul>
</div>
<p style= "padding: 15px 0 0 0">I hope to have a working app (and working knowledge of Ruby on Rails) in the next eight weeks. I have a lot to learn. I’m crossing my fingers!</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Software Licensing and Virtual Worlds]]></title>
<link href="http://icelle.github.io/blog/2013/11/28/software-licensing-and-the-virtual-worlds/"/>
<updated>2013-11-28T12:00:00-05:00</updated>
<id>http://icelle.github.io/blog/2013/11/28/software-licensing-and-the-virtual-worlds</id>
<content type="html"><![CDATA[<p>Before dabbling in the coding arts, I was a Code Head (not the technology kind, the <a href="http://law.duke.edu/lib/researchguides/pdf/ucc.pdf">Uniform Commercial Code</a> kind). It’s really interesting to me how law applies to new technology. In this article, I am going to discuss the effectiveness of using software license agreements in resolving virtual property right disputes that arise in massively multiplayer online role-playing games (MMORPG).</p>
<p><strong>Enforceability of Shrinkwrap and Clickwrap License Agreements:</strong>
Shrinkwrap licenses are terms and conditions found inside software boxes. Consumers can only read and accept the terms after opening the product. Clickwrap, on the other had, is usually found on the internet where consumers click “agree” before they can download a particular software. If the user refuses to click “agree,” he can no longer use or purchase the product or service.</p>
<p>Courts considered shrinkwrap and clickwrap licenses accompanying the sale of products as ordinary contracts subject to Uniform Commercial Code. <em>ProCD, Inc. v. Zeidenberg</em>, 86 F. 3d 1447. Given their unilateral nature, courts have found these licenses to be unenforceable contracts of adhesion. <em>Step-Saver Data System v. Wyse Technology</em>, 939 F. 2d 91. In <em>Step-Saver Data Systems v. Wyse Technology</em>, the court relied on UCC § 2-207 and § 2-209 in refusing to uphold the disclaimer of all express and implied warranties contained in the box-top license. The court reasoned that the box-top license was not the final and complete expression of the parties’ agreement because Step-Saver ordered the Advanced Multilink computer program over the phone. The telephone order acted as the final and complete expression of the parties’ agreement; therefore, conditions added subsequent the telephone order in the box-top license are material alterations of the contract.<em>Step-Saver Data System v. Wyse Technology</em>, 939 F. 2d 91. Because UCC § 2-209 requires that both parties must intend to adopt the additional terms, the shrinkwrap license was unenforceable.</p>
<!-- more -->
<p>Modern interpretations hold that shrinkwrap licenses satisfy the assent requirements of contract law. <em>ProCD, Inc. v. Zeidenberg</em> reasoned that <em>Step-Saver</em> does not apply to <em>ProCD</em> because <em>Step-Saver</em> is a battle of the forms case, which involves parties exchanging incompatible forms. <em>ProCD, Inc. v. Zeidenberg</em>, 86 F. 3d 1447. The court in <em>ProCD</em> relied on UCC § 2-204 to establish that shrinkwrap licenses are enforceable because “a vendor may invite acceptance by conduct and may propose limitations on the kind of conduct that constitutes acceptance.” Buyers can accept the terms of the agreement by using and keeping the software program and reject the terms by returning the package to the seller.</p>
<p>Courts have applied similar reasoning in the context of clickwrap license agreements. According to <em>i.Lan Systems, Inc. v. Netscout Service Level Corp</em>, if shrinkwrap licenses, which manifests only implied assent are enforceable, then clickwrap licenses, which require explicit assent before being able to download or use a software program, must also be enforceable. However, the court in <em>Specht v. Netscape Communications Corporation</em> applied the exception to the general contract rule (that users cannot avoid contractual obligation by failing to read before signing) to clickwrap licenses. This exception applies when the terms of the contract is unconscionable and the terms of the agreement are not called to the attention of the recipient. If a reasonably prudent internet user would not have known or learned of the existence of the license terms before responding to the invitation to download a software, no real assent was given; therefore, rendering the agreement invalid.</p>
<p><strong>Use of License Agreements in Virtual Worlds:</strong>
Shrinkwrap and Clickwrap licenses are commonly used in MMORPGs. MMORPG is a genre of computer game where thousands of players socially interact with each other in a virtual online environment through their avatars. Some MMORPGs such as World of Warcraft (WoW) are based on fantasy themes that have a character progression system in which the players focus on developing their characters’ skills by gaining experience points through completing individual or group tasks. Conversely, other MMORPGs like Second Life allow players to participate without completing particular tasks. They play solely to create virtual communities and virtual goods, socialize with others by participating in “real-life” activities and “real” cash economy.<em>15 TX WLR 109</em>. In Second Life, players can convert real life currency to game currency in order buy and sell virtual goods (including clothing, furniture, cars or land) and vice versa. In fact, many spend most of their time in the online universe that they’ve made selling virtual goods and land their main source of living. For example, <a href="http://www.anshechung.com/include/press/press_release251106.html">Anshe Chung</a> “achieved a net worth exceeding one million dollars from profits entirely earned inside the virtual world.”</p>
<p>In order to join the virtual world, MMORPG players must accept end user license agreements (EULA) and/or terms of service (TOS) by clicking the “I accept” button on the screen when they boot the software for the first time. Providers use EULA and TOS to protect themselves from copyright claims that can impede their discretion to manage the game and govern interactions between players. <em>82 INLJ 261.</em> The players’ access to the virtual world is contingent upon their obedience to the terms of the license. For example, players cannot infringe on intellectual property of other players in Second Life and WoW prohibits the sale or transfer of in-game assets (ability and power obtained through completing certain tasks in the virtual world) and characters. Since virtual law does not exist, these terms alone protect virtual world rights.</p>
<p><strong>Are Software License Agreements Sufficient to Solve Virtual World Disputes?</strong>
Courts usually examine and decide different virtual property right disputes by looking at the enforceability of the license agreements. See <em>Davidson & Associates v. Internet Gateway</em>, 334 F. Supp. 2d 1164 (circumvention; the court held that federal copyright law does not preempt Blizzard EULA because contractual restrictions on the use of software constituted an extra element that made the claim qualitatively different from copyright infringement); <em>Bragg v. Linden Research Inc.</em>, 487 F.Supp.2d 593 (regarding virtual land; arbitration provision in the terms of service is unconscionable; therefore, unenforceable). This approach is ineffective because software license agreements serve as a declaration of vendor’s intellectual property rights. As seen in <em>StepSaver</em>, <em>ProCD</em>, <em>i.Lan Systems</em> and <em>Specht</em>, license agreements are not designed to deal with disputes between players or protect personal property and real estate in the virtual world because it is essentially a contract between the player and the game provider.</p>
<p>The <a href="http://lindenlab.com/tos">Second Life’s Terms Of Service</a> states, “Virtual Land License is transferable by holder to any other user… [the holder] acknowledge that Virtual Land is a limited license right and is… not redeemable for any sum of money from Linden Lab.” Although the TOS also states that transactions between players are valid only if both “maintain their accounts in good standing, and are not delinquent on any account payment requirements,” there is no indication that Linden Lab has the authority to award injured players for loss of property or defamation in the virtual world. Many players conduct real business in the virtual world. Loss of property or defamation against their avatars in the virtual world would cause actual damage to real businesses and reputations. Because of this, I suggest using applicable law rather than looking at the license agreement in deciding these cases.</p>
<p>Many would disagree with using applicable law in deciding the merits of virtual disputes because the purpose of MMORPG is role-playing. Ethical conducts in the real world do not apply in virtual worlds because violence or theft is usually part of the game. Virtual goods are not tangible so they do not fall within property law. Because of this, players either should bear the risk of wrongdoing in the virtual world or virtual world disputes should be arbitrated in the game, not in real-life courts.</p>
<p>I agree that the impact of wrongdoing is less in the virtual world. Obviously, someone stealing your car in real life has more negative impact than someone stealing your avatar’s car in the virtual world. However, many people spend most of their day “living” in the virtual world and participating in virtual economy that loss of property or defamation against their avatars impacts real world life. In the Netherlands, a teenager was <a href="http://news.bbc.co.uk/2/hi/7094764.stm">arrested for stealing virtual furniture worth 4,000 euros from “rooms” in Habbo Hotel</a>, a 3D social networking website. The people who spent real-world money would surely want real-world remedy for their loss, especially if it’s a big sum of money.</p>
<p>A court has acknowledged virtual real estate rights. In <em>Bragg v. Linden Research, Inc.</em>, Bragg sued Second Life for freezing his virtual bank account because he allegedly fraudulently exploited a feature in the software that allowed him to bid on parcels of land that were not actually for sale, allowing him to buy land in the virtual world for a low price. <em>Bragg v. Linden Research Inc.</em>, 487 F.Supp.2d 593. At the time his account was frozen, he had $2,000 of real-world cash in his account and he owned 100 parcels of virtual land. Linden claimed that buying virtual land gave Bragg license to access Linden’s servers and storage; it did not give him title to property because it’s not real land. Instead of discussing the issue in the context of intellectual property rights, the court acknowledged that real property rights are affected by the virtual world because there was real-life harm to the deprivation of Bragg’s virtual world possessions. However, the court did not reach any concrete decisions regarding the property claims because the case was based on the unconscionability of the arbitration provision in the TOS. <em>15 TX WLR 109</em></p>
<p>Proponents of using EULA and TOS to resolve virtual world disputes reason that license agreements are sufficient to safeguard both the rights of the players and the game provider. The Second Life TOS allows players to retain intellectual property rights in their inventions in the virtual worlds. In 2010, it stated,</p>
<blockquote><p>Intellectual property infringement on the service is a violation of this Terms of Service, and [players] agree not to engage in such infringement. It is our policy to respond to notices of alleged copyright infringement that comply with the Digital Millennium Copyright Act and to terminate the accounts of repeat infringers in appropriate circumstances.</p></blockquote>
<p>Virtual goods are not real property because the aural elements players perceive when they log into the game are codes stored in the developer’s server.<em>82 INLJ 261.</em> The ability to retain intellectual property right in the TOS is sufficient to protect the codes written by players against others who copy, distribute or adapt that work.</p>
<p>The problem in this is that the DMCA Safe Harbor Provision immunizes ISPs from liability for copyright infringement if they have no knowledge of the unauthorized use of copyrighted work. This allows game providers to impose lax standards in monitoring content of their servers. In addition to this, <em>Davidson & Associates v. Internet Gateway</em> held that contractual restrictions on the use of software constitutes an extra element that makes a claim qualitatively different from copyright infringement. <em>Davidson & Associates v. Internet Gateway</em>, 334 F. Supp. 2d 1164. This means that TOSs and EULAs cannot be preempted by federal copyright laws in certain cases. As a result, game providers could ignore DMCA takedown requests and merely advice players to file an abuse report under their internal forum for resolving disputes between residents and do nothing about it. <em>15 TX WLR 109.</em> This <a href="http://www.nbcnews.com/id/20214184/ns/technology_and_science-internet/">allegedly happened to Kevin Alderman</a>, a virtual sex toy maker for Second Life avatars. When Alderman learned that someone copied his animation system and sold counterfeited SexGen brand of virtual sex toys to Second Life players, Linden Lab refused to get involved and did not discontinue the perpetrator’s Second Life account despite compelling evidence.</p>
<p><strong>Conclusion:</strong>
Software license agreements are not effective in resolving disputes that arise in MMORPGs because license agreements are meant to protect the providers, not consumers. The courts must look at other applicable law instead of merely looking at the validity of clickwrap and shrinkwrap license agreements to determine the merits of each case. Although the virtual world is not real, deprivation of virtual goods and defamation of avatars have real-life monetary consequences to MMORPG players.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Learn Programming Basics Rolling Dice]]></title>
<link href="http://icelle.github.io/blog/2013/11/07/learn-programming-basics-rolling-dice/"/>
<updated>2013-11-07T19:34:00-05:00</updated>
<id>http://icelle.github.io/blog/2013/11/07/learn-programming-basics-rolling-dice</id>
<content type="html"><![CDATA[<p>I started dabbling into the coding arts a few months ago (hence Code Gazer). I read Chris Pine’s <em>Learn to Program</em>. This is my attempt to elaborate on Pine’s Die Example to further understand the relationships between objects, classes, variables and methods.</p>
<p><strong>Pine Example:</strong> Create a die that gives you a random number when you roll it.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Die</span> <span class="c1">#create a class</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">roll</span> <span class="c1">#create method</span>
</span><span class='line'> <span class="mi">1</span> <span class="o">+</span> <span class="nb">rand</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="c1">#method logic</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Everything is an object. Class is an object that allows us to make new classes of objects and defines how those classes behave. A method is the behavior of those objects.
A die is not a basic object in Ruby (i.e. range, integers, strings, arrays and hashes) so in this example, we need to create a new object. We can do that by creating a new class. To create a new class, we use a construct called <code>class</code> (which defines a class’s behavior) and a class name, <code>Die</code>. The first letter of a class name is always capitalized.</p>
<!-- more -->
<p>After creating a class, we need to define its behavior. We can do that by making a method (behavior) that applies to the <code>class Die</code>: use the word <code>def</code> followed by the method name, <code>roll</code>. We define <code>roll</code> through the logic <code>1 + rand(6)</code> which says “return a random number between 1 and 6.”</p>
<p>Simple enough? Let’s go to the next example.</p>
<p><strong>Code Gazer Die Problem 1:</strong> Create one die that gives you a random number and color when you roll it.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Die</span>
</span><span class='line'> <span class="kp">attr_accessor</span> <span class="ss">:num</span><span class="p">,</span> <span class="ss">:color</span>
</span><span class='line'>
</span><span class='line'> <span class="vc">@@colors</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="o">=></span><span class="s2">"yellow"</span><span class="p">,</span> <span class="mi">2</span><span class="o">=></span><span class="s2">"blue"</span><span class="p">,</span> <span class="mi">3</span><span class="o">=></span><span class="s2">"green"</span><span class="p">,</span> <span class="mi">4</span><span class="o">=></span><span class="s2">"black"</span><span class="p">,</span> <span class="mi">5</span><span class="o">=></span><span class="s2">"white"</span><span class="p">,</span> <span class="mi">6</span><span class="o">=></span><span class="s2">"red"</span><span class="p">}</span> <span class="c1">#class variable hash</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'> <span class="vi">@num</span> <span class="o">=</span> <span class="kp">nil</span>
</span><span class='line'> <span class="vi">@color</span> <span class="o">=</span> <span class="kp">nil</span>
</span><span class='line'> <span class="n">roll</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">roll</span>
</span><span class='line'> <span class="vi">@num</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="nb">rand</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="c1"># instance variable</span>
</span><span class='line'> <span class="vi">@color</span> <span class="o">=</span> <span class="vc">@@colors</span><span class="o">[</span><span class="vi">@num</span><span class="o">]</span> <span class="c1"># hash key look-up</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"You rolled a </span><span class="si">#{</span><span class="vi">@num</span><span class="si">}</span><span class="s2"> with color </span><span class="si">#{</span><span class="vi">@color</span><span class="si">}</span><span class="s2">"</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Let’s take it one step at a time. What do we need?</p>
<p><strong>We need a die.</strong>
To create an object, we need to create a new class: <code>class Die</code>.</p>
<p><strong>Each side of the die must have a number and color that correspond with each other.</strong>
We need a container (variable) that will hold our numbers and colors. A variable is a storage location containing values. Variables are very useful because we can use them as reference, allowing us to reuse those values at a later time. Here, we need to create a class variable because we want the <code>@@colors</code> to be available to all methods in the class <code>Die</code>. Notice the <code>@@</code> symbol before the word colors? That means colors is a class variable.</p>
<p> Inside the variable, we need slots to hold our numbers and colors. A <code>Hash</code> is a perfect object for that. A hash variable is like a dictionary: you have a word (the key) and its corresponding definition (the value). Here, we created a <code>Hash</code> containing a set of colors (values) that correspond to each number in the die (keys). Like this:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="vc">@@colors</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="o">=></span><span class="s2">"yellow"</span><span class="p">,</span> <span class="mi">2</span><span class="o">=></span><span class="s2">"blue"</span><span class="p">,</span> <span class="mi">3</span><span class="o">=></span><span class="s2">"green"</span><span class="p">,</span> <span class="mi">4</span><span class="o">=></span><span class="s2">"black"</span><span class="p">,</span> <span class="mi">5</span><span class="o">=></span><span class="s2">"white"</span><span class="p">,</span> <span class="mi">6</span><span class="o">=></span><span class="s2">"red"</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>When we create the object, die, it needs to roll.</strong>
Whenever we make a new object, the initialize method, <code>def initialize</code>, allows us to set up the object’s initial state: we can provide default values to the attributes of the object (instance variables <code>@num</code> and <code>@color</code>). Everything we put in the initialize method (like <code>roll</code>) becomes a general/default rule and is automatically executed when you create a new object: <code>d = Die.new</code>.</p>
<p><strong>When we roll the die, we get a random color and number.</strong>
The die will roll automatically because it’s being called inside the initialize method, but it doesn’t give us a random color or number yet. We need to create a new method to define the behavior of the Die when it rolls: <code>def roll</code>.</p>
<p> To see what color and number the die picks, we need to define variables we set up in the initialize method. We use the <code>@</code> symbol instead of <code>@@</code> because we want instance variables, not a class variable. Unlike a class variable (which is commonly available to the entire <code>class Die</code>; this means it’s available for class methods (more on that another time) and instance methods, i.e. <code>roll</code>), each instance of a class has its own set of instance variables. So here, <code>@num</code> and <code>@color</code> applies only to the specific instance created via <code>Die.new</code>.</p>
<p> We need some logic that allows us to get a random number between 1 – 6 every time the die rolls. We store the number picked in <code>@num</code> so we can reference back to <code>@@colors</code> to see which color corresponds with the number. To do that, we set <code>@color</code> as a key look-up on the <code>@@colors</code> hash.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="vi">@num</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="nb">rand</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
</span><span class='line'><span class="vi">@color</span> <span class="o">=</span> <span class="vc">@@colors</span><span class="o">[</span><span class="vi">@num</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Did we forget something?</strong>
If we create an object, <code>d = Die.new</code>, the program has access to the random color and number, but the object itself can’t retrieve variables. For example, <code>d.num</code> and <code>d.color</code> gives us:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="ss">NoMethodError</span><span class="p">:</span> <span class="n">undefined</span> <span class="nb">method</span> <span class="s1">'num'</span> <span class="k">for</span> <span class="c1">#<Die:0x007fdde294b778 @num=1, @color="yellow"></span>
</span></code></pre></td></tr></table></div></figure>
<p> We can see the object has <code>@num=1, @color="yellow"</code> (Ruby is calling the inspect method on the object, which irb is returning) in it, but we can’t retrieve and use it. To do that, we can use a Ruby construct: <code>attr_accessor</code>. This construct is a shortcut given to us by the Ruby Gods to help us with our code crafting.</p>
<p> <code>attr_accessor</code> provides two methods automatically for the developer: a ‘getter’ method and a ‘setter’ method. The sole purpose of the getter is to read/return the value of a particular instance variable. The setter, on the other hand, assigns/sets the value for a particular instance variable. When we write,</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="kp">attr_accessor</span> <span class="ss">:num</span><span class="p">,</span> <span class="ss">:color</span>
</span></code></pre></td></tr></table></div></figure>
<p> we’re telling our program to build these methods behind the scenes:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">#getters</span>
</span><span class='line'><span class="k">def</span> <span class="nf">num</span>
</span><span class='line'> <span class="vi">@num</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">color</span>
</span><span class='line'> <span class="vi">@color</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># setters</span>
</span><span class='line'><span class="k">def</span> <span class="nf">num</span><span class="o">=</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
</span><span class='line'> <span class="vi">@num</span> <span class="o">=</span> <span class="n">value</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">color</span><span class="o">=</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
</span><span class='line'> <span class="vi">@color</span> <span class="o">=</span> <span class="n">value</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p> I think we have everything we need. Let’s run the program and see what we get:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">2</span><span class="o">.</span><span class="mi">0</span><span class="o">.</span><span class="mi">0</span><span class="n">p247</span> <span class="p">:</span><span class="mo">04</span><span class="mi">9</span> <span class="o">></span> <span class="n">d</span> <span class="o">=</span> <span class="no">Die</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="no">You</span> <span class="n">rolled</span> <span class="n">a</span> <span class="mi">6</span> <span class="n">with</span> <span class="n">color</span> <span class="n">red</span>
</span><span class='line'> <span class="o">=></span> <span class="c1">#<Die:0x007fa7889321f0 @num=6, @color="red"></span>
</span></code></pre></td></tr></table></div></figure>
<p> Awesome. Let’s try it a couple of times to see if it gives us a random color and number.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">d</span><span class="o">.</span><span class="n">roll</span> <span class="o">=></span> <span class="s2">"You rolled a 4 with color black"</span>
</span><span class='line'><span class="n">d</span><span class="o">.</span><span class="n">roll</span> <span class="o">=></span> <span class="s2">"You rolled a 2 with color blue"</span>
</span><span class='line'><span class="n">d</span><span class="o">.</span><span class="n">roll</span> <span class="o">=></span> <span class="s2">"You rolled a 1 with color yellow"</span>
</span></code></pre></td></tr></table></div></figure>
<p> It works! Yay!</p>
<p>Phew! I hope I explained those concepts clearly and sufficiently.</p>
]]></content>
</entry>
</feed>