tablestakes 0.8.5 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f98f7f613b50b179880e3994802ee92e8f95592d
4
- data.tar.gz: 7b68027ba7ad45b2b010db95eebcee1d5d4dedf2
3
+ metadata.gz: 1f451acb82c56dcaba618c44b3451a849396c8e7
4
+ data.tar.gz: 5dec9da1d942d25d6def30a2fad6f1dc03f3a86a
5
5
  SHA512:
6
- metadata.gz: 08ceccfccfabab0f825192ed075124e1a1a6e301183de4060fb16f9b19fb7ba9d47be29888fe8033bd0c99404356fd4c88571e0f67016987021d008dc88812b7
7
- data.tar.gz: 5a9f004ff941df3caf43f7a533fcda9db1c2444f34b4a517694817d285c206bb523a8a675f892234d1e918d514bf43b73b74793b16eff5e7e88fd328010555d0
6
+ metadata.gz: 2423fe3a237012d2e9c0f0f8af6e96666aeafb862332e9ffaaacd93ef1bb50a7846f203ea4c4df17f52fbe2bfd87e4bf2e7e8e77b2fd65f423fda8b6b27ba538
7
+ data.tar.gz: 48a29ae8acdf6ad4265c1deab7e5f27c46c80f5f0c923a47d720c757d44effa4c45e46d703db4330c728c40a1872bdac0cb828831e8233b4076b740a46d4ae39
data/README.md CHANGED
@@ -1,8 +1,24 @@
1
- =========
2
1
  Tablestakes
3
2
  ===========
4
3
 
5
- [![Gem Version](https://badge.fury.io/rb/tablestakes.svg)](http://badge.fury.io/rb/tablestakes)
4
+ [![Gem Version][GV img]][Gem Version]
5
+ [![Build Status][BS img]][Build Status]
6
+ [![Dependency Status][DS img]][Dependency Status]
7
+ [![Code Climate][CC img]][Code Climate]
8
+ [![Coverage Status][CS img]][Coverage Status]
9
+
10
+ [Gem Version]: https://rubygems.org/gems/tablestakes
11
+ [Build Status]: https://travis-ci.org/jbfolkerts/tablestakes
12
+ [travis pull requests]: https://travis-ci.org/jbfolkerts/tablestakes/pull_requests
13
+ [Dependency Status]: https://gemnasium.com/jbfolkerts/tablestakes
14
+ [Code Climate]: https://codeclimate.com/github/jbfolkerts/tablestakes
15
+ [Coverage Status]: https://coveralls.io/r/jbfolkerts/tablestakes
16
+
17
+ [GV img]: https://badge.fury.io/rb/tablestakes.png
18
+ [BS img]: https://travis-ci.org/jbfolkerts/tablestakes.png
19
+ [DS img]: https://gemnasium.com/jbfolkerts/tablestakes.png
20
+ [CC img]: https://codeclimate.com/github/jbfolkerts/tablestakes.png
21
+ [CS img]: https://coveralls.io/repos/jbfolkerts/tablestakes/badge.png?branch=master
6
22
 
7
23
  Tablestakes is a gem for processing tabular data. It is for people who would rather not meddle with
8
24
  a spreadsheet, or load their data into a SQL database. You get the instant gratification of being
@@ -20,6 +36,7 @@ Contents
20
36
  - [Counting Data](#counting-data)
21
37
  - [Updating Data](#updating-data)
22
38
  - [Join, Union, and Intersect](#join-union-and-intersect)
39
+ - [Sorting Data](#sorting-data)
23
40
  - [Interacting with your Data](#interacting-with-your-data)
24
41
 
25
42
  How to Install
@@ -185,6 +202,26 @@ capitals.union(cities, "Capitals", "Cities") # returns an array of all cit
185
202
  capitals.intersect(cities, "Capitals", "Cities") # returns an array of only the cities in both tables
186
203
  ```
187
204
 
205
+ Sorting Data
206
+ ------------
207
+
208
+ Sorting your data table can be done on any given column (if not specified, it defaults to the first
209
+ column). Any blocks passed to the sort function are then used internally to sort the column. Here
210
+ are some examples:
211
+
212
+ ```ruby
213
+ cities.sort("State") # Re-orders the cities table based on State name
214
+ cities.sort { |a,b| b<=>a } # Reverse the order of the cities table
215
+ cities.sort("State") { |a,b| b<=>a } # Sort by State in reverse alpha order
216
+ ```
217
+
218
+ Of course you don't necessarily want to sort every column by it's String value. To sort using an
219
+ on-the-fly type conversion, supply the sort function with an options Hash as in the following:
220
+
221
+ ```ruby
222
+ cities.sort("2012 estimate", :Fixnum) # Sorts cities by 2012 population
223
+ ```
224
+
188
225
  Interacting with your Data
189
226
  --------------------------
190
227
 
@@ -212,14 +249,3 @@ file.
212
249
 
213
250
  Some methods, such as `Table#row` and `Table#column` return Arrays, and of course these are
214
251
  readily modified using their own native methods.
215
-
216
- Future Enhancements
217
- -------------------
218
-
219
- Some future enhancements that would make this gem better include:
220
-
221
- 1. Implement Ruby Enumerators
222
-
223
- 2. Include some concept of data type ... at least FixedNum and Date.
224
-
225
- 3. `Table#sort` method -- probably requires enumerators and data types to be effective.
@@ -0,0 +1,272 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
6
+
7
+ <title>class OrderedRow - RDoc Documentation</title>
8
+
9
+ <link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet">
10
+
11
+ <script type="text/javascript">
12
+ var rdoc_rel_prefix = "./";
13
+ </script>
14
+
15
+ <script type="text/javascript" charset="utf-8" src="./js/jquery.js"></script>
16
+ <script type="text/javascript" charset="utf-8" src="./js/navigation.js"></script>
17
+ <script type="text/javascript" charset="utf-8" src="./js/search_index.js"></script>
18
+ <script type="text/javascript" charset="utf-8" src="./js/search.js"></script>
19
+ <script type="text/javascript" charset="utf-8" src="./js/searcher.js"></script>
20
+ <script type="text/javascript" charset="utf-8" src="./js/darkfish.js"></script>
21
+
22
+
23
+ <body id="top" class="class">
24
+ <nav id="metadata">
25
+ <nav id="home-section" class="section">
26
+ <h3 class="section-header">
27
+ <a href="./index.html">Home</a>
28
+ <a href="./table_of_contents.html#classes">Classes</a>
29
+ <a href="./table_of_contents.html#methods">Methods</a>
30
+ </h3>
31
+ </nav>
32
+
33
+
34
+ <nav id="search-section" class="section project-section" class="initially-hidden">
35
+ <form action="#" method="get" accept-charset="utf-8">
36
+ <h3 class="section-header">
37
+ <input type="text" name="search" placeholder="Search" id="search-field"
38
+ title="Type to search, Up and Down to navigate, Enter to load">
39
+ </h3>
40
+ </form>
41
+
42
+ <ul id="search-results" class="initially-hidden"></ul>
43
+ </nav>
44
+
45
+
46
+
47
+
48
+ <div id="file-metadata">
49
+ <nav id="file-list-section" class="section">
50
+ <h3 class="section-header">Defined In</h3>
51
+ <ul>
52
+ <li>lib/tablestakes.rb
53
+ </ul>
54
+ </nav>
55
+
56
+
57
+ </div>
58
+
59
+ <div id="class-metadata">
60
+
61
+ <nav id="parent-class-section" class="section">
62
+ <h3 class="section-header">Parent</h3>
63
+
64
+ <p class="link">Object
65
+
66
+ </nav>
67
+
68
+
69
+
70
+ <!-- Method Quickref -->
71
+ <nav id="method-list-section" class="section">
72
+ <h3 class="section-header">Methods</h3>
73
+
74
+ <ul class="link-list">
75
+
76
+ <li ><a href="#method-c-new">::new</a>
77
+
78
+ <li ><a href="#method-i-3C-3D-3E">#&lt;=&gt;</a>
79
+
80
+ <li ><a href="#method-i-data">#data</a>
81
+
82
+ </ul>
83
+ </nav>
84
+
85
+ </div>
86
+
87
+ <div id="project-metadata">
88
+
89
+ <nav id="classindex-section" class="section project-section">
90
+ <h3 class="section-header">Class and Module Index</h3>
91
+
92
+ <ul class="link-list">
93
+
94
+ <li><a href="./OrderedRow.html">OrderedRow</a>
95
+
96
+ <li><a href="./Table.html">Table</a>
97
+
98
+ </ul>
99
+ </nav>
100
+
101
+ </div>
102
+ </nav>
103
+
104
+ <div id="documentation">
105
+ <h1 class="class">class OrderedRow</h1>
106
+
107
+ <div id="description" class="description">
108
+
109
+ <p>This class functions as a temporary representation of a row. The <a
110
+ href="OrderedRow.html">OrderedRow</a> contains information about which
111
+ column it should be sorted on, so that Comparable can be implemented.</p>
112
+
113
+ </div><!-- description -->
114
+
115
+
116
+
117
+
118
+ <section id="5Buntitled-5D" class="documentation-section">
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+ <!-- Methods -->
128
+
129
+ <section id="public-class-5Buntitled-5D-method-details" class="method-section section">
130
+ <h3 class="section-header">Public Class Methods</h3>
131
+
132
+
133
+ <div id="method-c-new" class="method-detail ">
134
+
135
+ <div class="method-heading">
136
+ <span class="method-name">new</span><span
137
+ class="method-args">(my_array, index)</span>
138
+
139
+ <span class="method-click-advice">click to toggle source</span>
140
+
141
+ </div>
142
+
143
+
144
+ <div class="method-description">
145
+
146
+ <p>Creates a new <a href="OrderedRow.html">OrderedRow</a>. Callers must
147
+ specify the index of the row element which will be used for order
148
+ comparisons.</p>
149
+
150
+ <h4 id="method-c-new-label-Attributes">Attributes<span><a href="#method-c-new-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
151
+ <dl class="rdoc-list note-list"><dt><code>my_array</code>
152
+ <dd>
153
+ <p>An array representing a row from <code>Table</code></p>
154
+ </dd><dt><code>index</code>
155
+ <dd>
156
+ <p>A Fixnum value which represents the comparison value</p>
157
+ </dd></dl>
158
+
159
+
160
+
161
+
162
+ <div class="method-source-code" id="new-source">
163
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 635</span>
164
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">my_array</span>, <span class="ruby-identifier">index</span>)
165
+ <span class="ruby-ivar">@data</span> = <span class="ruby-identifier">my_array</span>
166
+ <span class="ruby-ivar">@sort_index</span> = <span class="ruby-identifier">index</span>
167
+ <span class="ruby-keyword">end</span></pre>
168
+ </div><!-- new-source -->
169
+
170
+ </div>
171
+
172
+
173
+
174
+
175
+ </div><!-- new-method -->
176
+
177
+
178
+ </section><!-- public-class-method-details -->
179
+
180
+ <section id="public-instance-5Buntitled-5D-method-details" class="method-section section">
181
+ <h3 class="section-header">Public Instance Methods</h3>
182
+
183
+
184
+ <div id="method-i-3C-3D-3E" class="method-detail ">
185
+
186
+ <div class="method-heading">
187
+ <span class="method-name">&lt;=&gt;</span><span
188
+ class="method-args">(other)</span>
189
+
190
+ <span class="method-click-advice">click to toggle source</span>
191
+
192
+ </div>
193
+
194
+
195
+ <div class="method-description">
196
+
197
+ <p>Implements comparable</p>
198
+
199
+ <h4 id="method-i-3C-3D-3E-label-Attributes">Attributes<span><a href="#method-i-3C-3D-3E-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
200
+ <dl class="rdoc-list note-list"><dt><code>other</code>
201
+ <dd>
202
+ <p>The row to be compared</p>
203
+ </dd></dl>
204
+
205
+
206
+
207
+
208
+ <div class="method-source-code" id="3C-3D-3E-source">
209
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 652</span>
210
+ <span class="ruby-keyword">def</span> <span class="ruby-operator">&lt;=&gt;</span>(<span class="ruby-identifier">other</span>)
211
+ <span class="ruby-keyword">self</span>.<span class="ruby-identifier">data</span>[<span class="ruby-ivar">@sort_index</span>] <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">data</span>[<span class="ruby-ivar">@sort_index</span>]
212
+ <span class="ruby-keyword">end</span></pre>
213
+ </div><!-- 3C-3D-3E-source -->
214
+
215
+ </div>
216
+
217
+
218
+
219
+
220
+ </div><!-- 3C-3D-3E-method -->
221
+
222
+
223
+ <div id="method-i-data" class="method-detail ">
224
+
225
+ <div class="method-heading">
226
+ <span class="method-name">data</span><span
227
+ class="method-args">()</span>
228
+
229
+ <span class="method-click-advice">click to toggle source</span>
230
+
231
+ </div>
232
+
233
+
234
+ <div class="method-description">
235
+
236
+ <p>Returns the row elements in an <code>Array</code></p>
237
+
238
+ <h4 id="method-i-data-label-Attributes">Attributes<span><a href="#method-i-data-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
239
+
240
+ <p>none</p>
241
+
242
+
243
+
244
+
245
+ <div class="method-source-code" id="data-source">
246
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 644</span>
247
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">data</span>
248
+ <span class="ruby-keyword">return</span> <span class="ruby-ivar">@data</span>
249
+ <span class="ruby-keyword">end</span></pre>
250
+ </div><!-- data-source -->
251
+
252
+ </div>
253
+
254
+
255
+
256
+
257
+ </div><!-- data-method -->
258
+
259
+
260
+ </section><!-- public-instance-method-details -->
261
+
262
+ </section><!-- 5Buntitled-5D -->
263
+
264
+ </div><!-- documentation -->
265
+
266
+
267
+ <footer id="validator-badges">
268
+ <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
269
+ <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 4.0.0.
270
+ <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
271
+ </footer>
272
+
data/doc/Table.html CHANGED
@@ -65,7 +65,19 @@
65
65
 
66
66
  </nav>
67
67
 
68
-
68
+ <!-- Included Modules -->
69
+ <nav id="includes-section" class="section">
70
+ <h3 class="section-header">Included Modules</h3>
71
+
72
+ <ul class="link-list">
73
+
74
+
75
+ <li><span class="include">Enumerable</span>
76
+
77
+
78
+ </ul>
79
+ </nav>
80
+
69
81
 
70
82
  <!-- Method Quickref -->
71
83
  <nav id="method-list-section" class="section">
@@ -75,12 +87,26 @@
75
87
 
76
88
  <li ><a href="#method-c-new">::new</a>
77
89
 
90
+ <li ><a href="#method-i-add_column">#add_column</a>
91
+
92
+ <li ><a href="#method-i-add_row">#add_row</a>
93
+
94
+ <li ><a href="#method-i-add_rows">#add_rows</a>
95
+
78
96
  <li ><a href="#method-i-bottom">#bottom</a>
79
97
 
80
98
  <li ><a href="#method-i-column">#column</a>
81
99
 
82
100
  <li ><a href="#method-i-count">#count</a>
83
101
 
102
+ <li ><a href="#method-i-del_column">#del_column</a>
103
+
104
+ <li ><a href="#method-i-del_row">#del_row</a>
105
+
106
+ <li ><a href="#method-i-each">#each</a>
107
+
108
+ <li ><a href="#method-i-empty-3F">#empty?</a>
109
+
84
110
  <li ><a href="#method-i-get_columns">#get_columns</a>
85
111
 
86
112
  <li ><a href="#method-i-get_rows">#get_rows</a>
@@ -97,6 +123,10 @@
97
123
 
98
124
  <li ><a href="#method-i-size">#size</a>
99
125
 
126
+ <li ><a href="#method-i-sort">#sort</a>
127
+
128
+ <li ><a href="#method-i-sort-21">#sort!</a>
129
+
100
130
  <li ><a href="#method-i-sub">#sub</a>
101
131
 
102
132
  <li ><a href="#method-i-sub-21">#sub!</a>
@@ -127,6 +157,8 @@
127
157
 
128
158
  <ul class="link-list">
129
159
 
160
+ <li><a href="./OrderedRow.html">OrderedRow</a>
161
+
130
162
  <li><a href="./Table.html">Table</a>
131
163
 
132
164
  </ul>
@@ -202,17 +234,26 @@ in the <code>Table</code>. All headers are represented as
202
234
  <div class="method-description">
203
235
 
204
236
  <p>Instantiate a <code>Table</code> object using a tab-delimited file</p>
237
+
238
+ <h4 id="method-c-new-label-Attributes">Attributes<span><a href="#method-c-new-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
205
239
  <dl class="rdoc-list note-list"><dt><code>input</code>
206
240
  <dd>
207
241
  <p>OPTIONAL <code>Array</code> of rows or <code>String</code> to identify the
208
242
  name of the tab-delimited file to read</p>
209
243
  </dd></dl>
244
+
245
+ <h4 id="method-c-new-label-Examples">Examples<span><a href="#method-c-new-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
246
+
247
+ <pre>cities = Table.new() # empty table
248
+ cities = Table.new([ [&quot;City&quot;, &quot;State], [&quot;New York&quot;, &quot;NY&quot;], [&quot;Dallas&quot;, &quot;TX&quot;] ]) # create from Array of rows
249
+ cities = Table.new(&quot;cities.txt&quot;) # read from file
250
+ cities = Table.new(capitals) # create from table</pre>
210
251
 
211
252
 
212
253
 
213
254
 
214
255
  <div class="method-source-code" id="new-source">
215
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 32</span>
256
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 43</span>
216
257
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">input</span>=<span class="ruby-keyword">nil</span>)
217
258
  <span class="ruby-ivar">@headers</span> = []
218
259
  <span class="ruby-ivar">@table</span> = {}
@@ -220,14 +261,14 @@ name of the tab-delimited file to read</p>
220
261
 
221
262
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">input</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:fetch</span>)
222
263
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">input</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:fetch</span>)
223
- <span class="ruby-comment">#create +Table+ from rows</span>
264
+ <span class="ruby-comment">#create Table from rows</span>
224
265
  <span class="ruby-identifier">add_rows</span>(<span class="ruby-identifier">input</span>)
225
266
  <span class="ruby-keyword">end</span>
226
267
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">input</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:upcase</span>)
227
268
  <span class="ruby-comment"># a string, then read_file</span>
228
269
  <span class="ruby-identifier">read_file</span>(<span class="ruby-identifier">input</span>)
229
270
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">input</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:headers</span>)
230
- <span class="ruby-identifier">init</span>(<span class="ruby-identifier">input</span>)
271
+ <span class="ruby-identifier">input</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">add_row</span>(<span class="ruby-identifier">row</span>) }
231
272
  <span class="ruby-keyword">end</span>
232
273
  <span class="ruby-comment"># else create empty +Table+</span>
233
274
  <span class="ruby-keyword">end</span></pre>
@@ -247,6 +288,174 @@ name of the tab-delimited file to read</p>
247
288
  <h3 class="section-header">Public Instance Methods</h3>
248
289
 
249
290
 
291
+ <div id="method-i-add_column" class="method-detail ">
292
+
293
+ <div class="method-heading">
294
+ <span class="method-name">add_column</span><span
295
+ class="method-args">(*args)</span>
296
+
297
+ <span class="method-click-advice">click to toggle source</span>
298
+
299
+ </div>
300
+
301
+
302
+ <div class="method-description">
303
+
304
+ <p>Add a column to the <a href="Table.html">Table</a>. Raises ArgumentError if
305
+ the column name is already taken or there are not the correct number of
306
+ values.</p>
307
+
308
+ <h4 id="method-i-add_column-label-Attributes">Attributes<span><a href="#method-i-add_column-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
309
+ <dl class="rdoc-list note-list"><dt><code>args</code>
310
+ <dd>
311
+ <p>Array of <code>String</code> to identify the name of the column (see
312
+ examples)</p>
313
+ </dd></dl>
314
+
315
+ <h4 id="method-i-add_column-label-Examples">Examples<span><a href="#method-i-add_column-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
316
+
317
+ <pre>cities.add_column(&quot;City&quot;, [&quot;New York&quot;, &quot;Dallas&quot;, &quot;San Franscisco&quot;])
318
+ cities.add_column([&quot;City&quot;,&quot;New York&quot;, &quot;Dallas&quot;, &quot;San Franscisco&quot;])
319
+ cities.add_column(&quot;City&quot;, &quot;New York&quot;, &quot;Dallas&quot;, &quot;San Franscisco&quot;)</pre>
320
+
321
+
322
+
323
+
324
+ <div class="method-source-code" id="add_column-source">
325
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 114</span>
326
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">add_column</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
327
+ <span class="ruby-keyword">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Array</span>
328
+ <span class="ruby-identifier">args</span>.<span class="ruby-identifier">flatten!</span>
329
+ <span class="ruby-identifier">colname</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
330
+ <span class="ruby-identifier">column_vals</span> = <span class="ruby-identifier">args</span>
331
+ <span class="ruby-keyword">else</span>
332
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid Arguments to add_column&quot;</span>
333
+ <span class="ruby-keyword">end</span>
334
+ <span class="ruby-comment"># check arguments</span>
335
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Duplicate Column Name!&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
336
+ <span class="ruby-keyword">unless</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">empty?</span>
337
+ <span class="ruby-keyword">if</span> <span class="ruby-identifier">column_vals</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">length</span>
338
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Number of elements in column does not match existing table&quot;</span>
339
+ <span class="ruby-keyword">end</span>
340
+ <span class="ruby-keyword">end</span>
341
+ <span class="ruby-identifier">append_col</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">column_vals</span>)
342
+ <span class="ruby-keyword">end</span></pre>
343
+ </div><!-- add_column-source -->
344
+
345
+ </div>
346
+
347
+
348
+
349
+
350
+ </div><!-- add_column-method -->
351
+
352
+
353
+ <div id="method-i-add_row" class="method-detail ">
354
+
355
+ <div class="method-heading">
356
+ <span class="method-name">add_row</span><span
357
+ class="method-args">(*row)</span>
358
+
359
+ <span class="method-click-advice">click to toggle source</span>
360
+
361
+ </div>
362
+
363
+
364
+ <div class="method-description">
365
+
366
+ <p>Add a row to the <a href="Table.html">Table</a>, appending it to the end.
367
+ Raises ArgumentError if there are not the correct number of values.</p>
368
+
369
+ <h4 id="method-i-add_row-label-Attributes">Attributes<span><a href="#method-i-add_row-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
370
+ <dl class="rdoc-list note-list"><dt><code>row</code>
371
+ <dd>
372
+ <p><code>Array</code> to hold the row values</p>
373
+ </dd></dl>
374
+
375
+ <h4 id="method-i-add_row-label-Examples">Examples<span><a href="#method-i-add_row-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
376
+
377
+ <pre>cities = Table.new.add_row( [&quot;City&quot;, &quot;State&quot;] ) # create new Table with headers
378
+ cities.add_row(&quot;New York&quot;, &quot;NY&quot;) # add data row to Table</pre>
379
+
380
+
381
+
382
+
383
+ <div class="method-source-code" id="add_row-source">
384
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 158</span>
385
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">add_row</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">row</span>)
386
+ <span class="ruby-keyword">if</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Array</span>
387
+ <span class="ruby-identifier">row</span> = <span class="ruby-identifier">row</span>.<span class="ruby-identifier">flatten</span>
388
+ <span class="ruby-keyword">else</span>
389
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid Arguments to add_row&quot;</span>
390
+ <span class="ruby-keyword">end</span>
391
+ <span class="ruby-keyword">if</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">empty?</span>
392
+ <span class="ruby-ivar">@headers</span> = <span class="ruby-identifier">row</span>
393
+ <span class="ruby-keyword">else</span>
394
+ <span class="ruby-keyword">unless</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">length</span>
395
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Wrong number of fields in Table input&quot;</span>
396
+ <span class="ruby-keyword">end</span>
397
+ <span class="ruby-identifier">append_row</span>(<span class="ruby-identifier">row</span>)
398
+ <span class="ruby-keyword">end</span>
399
+ <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
400
+ <span class="ruby-keyword">end</span></pre>
401
+ </div><!-- add_row-source -->
402
+
403
+ </div>
404
+
405
+
406
+
407
+
408
+ </div><!-- add_row-method -->
409
+
410
+
411
+ <div id="method-i-add_rows" class="method-detail ">
412
+
413
+ <div class="method-heading">
414
+ <span class="method-name">add_rows</span><span
415
+ class="method-args">(array_of_rows)</span>
416
+
417
+ <span class="method-click-advice">click to toggle source</span>
418
+
419
+ </div>
420
+
421
+
422
+ <div class="method-description">
423
+
424
+ <p>Add one or more rows to the <a href="Table.html">Table</a>, appending it to
425
+ the end. Raises ArgumentError if there are not the correct number of
426
+ values. The first row becomes the table headers if currently undefined.</p>
427
+
428
+ <h4 id="method-i-add_rows-label-Attributes">Attributes<span><a href="#method-i-add_rows-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
429
+ <dl class="rdoc-list note-list"><dt><code>array_of_rows</code>
430
+ <dd>
431
+ <p><code>Array</code> of <code>Arrays</code> to hold the rows values</p>
432
+ </dd></dl>
433
+
434
+ <h4 id="method-i-add_rows-label-Examples">Examples<span><a href="#method-i-add_rows-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
435
+
436
+ <pre>cities.add_rows([ [&quot;New York&quot;, &quot;NY&quot;], [&quot;Austin&quot;, &quot;TX&quot;] ])</pre>
437
+
438
+
439
+
440
+
441
+ <div class="method-source-code" id="add_rows-source">
442
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 141</span>
443
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">add_rows</span>(<span class="ruby-identifier">array_of_rows</span>)
444
+ <span class="ruby-identifier">array_of_rows</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
445
+ <span class="ruby-identifier">add_row</span>(<span class="ruby-identifier">r</span>.<span class="ruby-identifier">clone</span>)
446
+ <span class="ruby-keyword">end</span>
447
+ <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
448
+ <span class="ruby-keyword">end</span></pre>
449
+ </div><!-- add_rows-source -->
450
+
451
+ </div>
452
+
453
+
454
+
455
+
456
+ </div><!-- add_rows-method -->
457
+
458
+
250
459
  <div id="method-i-bottom" class="method-detail ">
251
460
 
252
461
  <div class="method-heading">
@@ -260,10 +469,12 @@ name of the tab-delimited file to read</p>
260
469
 
261
470
  <div class="method-description">
262
471
 
263
- <p>Counts the number of instances of a particular string, given a column name,
264
- and returns an integer &gt;= 0. Returns <code>nil</code> if the column is
265
- not found. If no parameters are given, returns the number of rows in the
266
- table.</p>
472
+ <p>Returns counts of the least frequent values found in a given column in the
473
+ form of a <a href="Table.html">Table</a>. Raises ArgumentError if the
474
+ column is not found. If no limit is given to the number of values, only
475
+ the least frequent value will be returned.</p>
476
+
477
+ <h4 id="method-i-bottom-label-Attributes">Attributes<span><a href="#method-i-bottom-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
267
478
  <dl class="rdoc-list note-list"><dt><code>colname</code>
268
479
  <dd>
269
480
  <p><code>String</code> to identify the column to count</p>
@@ -271,15 +482,20 @@ table.</p>
271
482
  <dd>
272
483
  <p>OPTIONAL <code>String</code> number of values to return</p>
273
484
  </dd></dl>
485
+
486
+ <h4 id="method-i-bottom-label-Examples">Examples<span><a href="#method-i-bottom-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
487
+
488
+ <pre>cities.bottom(&quot;State&quot;) # returns a Table with the least frequent state in the cities Table
489
+ cities.bottom(&quot;State&quot;, 10) # returns a Table with the 10 least frequent states in the cities Table</pre>
274
490
 
275
491
 
276
492
 
277
493
 
278
494
  <div class="method-source-code" id="bottom-source">
279
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 152</span>
495
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 318</span>
280
496
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">bottom</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">num</span>=<span class="ruby-value">1</span>)
281
497
  <span class="ruby-identifier">freq</span> = <span class="ruby-identifier">tally</span>(<span class="ruby-identifier">colname</span>).<span class="ruby-identifier">to_a</span>[<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>].<span class="ruby-identifier">sort_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">v</span> }
282
- <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">freq</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">num</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>].<span class="ruby-identifier">unshift</span>([<span class="ruby-string">&quot;State&quot;</span>,<span class="ruby-string">&quot;Count&quot;</span>]))
498
+ <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">freq</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">num</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>].<span class="ruby-identifier">unshift</span>([<span class="ruby-identifier">colname</span>,<span class="ruby-string">&quot;Count&quot;</span>]))
283
499
  <span class="ruby-keyword">end</span></pre>
284
500
  </div><!-- bottom-source -->
285
501
 
@@ -305,7 +521,9 @@ table.</p>
305
521
  <div class="method-description">
306
522
 
307
523
  <p>Return a copy of a column from the table, identified by column name.
308
- Returns <code>nil</code> if column name not found.</p>
524
+ Returns empty Array if column name not found.</p>
525
+
526
+ <h4 id="method-i-column-label-Attributes">Attributes<span><a href="#method-i-column-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
309
527
  <dl class="rdoc-list note-list"><dt><code>colname</code>
310
528
  <dd>
311
529
  <p><code>String</code> to identify the name of the column</p>
@@ -315,12 +533,14 @@ Returns <code>nil</code> if column name not found.</p>
315
533
 
316
534
 
317
535
  <div class="method-source-code" id="column-source">
318
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 55</span>
536
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 80</span>
319
537
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">column</span>(<span class="ruby-identifier">colname</span>)
320
- <span class="ruby-comment"># check arguments</span>
321
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
322
-
323
- <span class="ruby-constant">Array</span>(<span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>])
538
+ <span class="ruby-comment"># return empty Array if column name not found</span>
539
+ <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
540
+ <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>()
541
+ <span class="ruby-keyword">else</span>
542
+ <span class="ruby-constant">Array</span>(<span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>])
543
+ <span class="ruby-keyword">end</span>
324
544
  <span class="ruby-keyword">end</span></pre>
325
545
  </div><!-- column-source -->
326
546
 
@@ -349,6 +569,8 @@ Returns <code>nil</code> if column name not found.</p>
349
569
  and returns an integer &gt;= 0. Returns <code>nil</code> if the column is
350
570
  not found. If no parameters are given, returns the number of rows in the
351
571
  table.</p>
572
+
573
+ <h4 id="method-i-count-label-Attributes">Attributes<span><a href="#method-i-count-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
352
574
  <dl class="rdoc-list note-list"><dt><code>colname</code>
353
575
  <dd>
354
576
  <p>OPTIONAL <code>String</code> to identify the column to count</p>
@@ -356,20 +578,28 @@ table.</p>
356
578
  <dd>
357
579
  <p>OPTIONAL <code>String</code> value to count</p>
358
580
  </dd></dl>
581
+
582
+ <h4 id="method-i-count-label-Examples">Examples<span><a href="#method-i-count-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
583
+
584
+ <pre>cities.count # returns number of rows in cities Table
585
+ cities.size # same as cities.count
586
+ cities.length # same as cities.count
587
+ cities.count(&quot;State&quot;, &quot;NY&quot;) # returns the number of rows with State == &quot;NY&quot;</pre>
359
588
 
360
589
 
361
590
 
362
591
 
363
592
  <div class="method-source-code" id="count-source">
364
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 111</span>
593
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 264</span>
365
594
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">count</span>(<span class="ruby-identifier">colname</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">value</span>=<span class="ruby-keyword">nil</span>)
366
595
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">colname</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span>
367
596
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
368
597
  <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">each_key</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-keyword">return</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-identifier">e</span>).<span class="ruby-identifier">length</span> }
369
598
  <span class="ruby-keyword">else</span>
370
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span>
599
+ <span class="ruby-keyword">return</span> <span class="ruby-value">0</span>
371
600
  <span class="ruby-keyword">end</span>
372
601
  <span class="ruby-keyword">end</span>
602
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">colname</span>)
373
603
 
374
604
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>]
375
605
  <span class="ruby-identifier">result</span> = <span class="ruby-value">0</span>
@@ -395,6 +625,181 @@ table.</p>
395
625
  </div><!-- count-method -->
396
626
 
397
627
 
628
+ <div id="method-i-del_column" class="method-detail ">
629
+
630
+ <div class="method-heading">
631
+ <span class="method-name">del_column</span><span
632
+ class="method-args">(colname)</span>
633
+
634
+ <span class="method-click-advice">click to toggle source</span>
635
+
636
+ </div>
637
+
638
+
639
+ <div class="method-description">
640
+
641
+ <p>Delete a column from the <a href="Table.html">Table</a>. Raises
642
+ ArgumentError if the column name does not exist.</p>
643
+
644
+ <h4 id="method-i-del_column-label-Attributes">Attributes<span><a href="#method-i-del_column-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
645
+ <dl class="rdoc-list note-list"><dt><code>colname</code>
646
+ <dd>
647
+ <p><code>String</code> to identify the name of the column</p>
648
+ </dd></dl>
649
+
650
+ <h4 id="method-i-del_column-label-Examples">Examples<span><a href="#method-i-del_column-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
651
+
652
+ <pre>cities.del_column(&quot;State&quot;) # returns table without &quot;State&quot; column</pre>
653
+
654
+
655
+
656
+
657
+ <div class="method-source-code" id="del_column-source">
658
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 182</span>
659
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">del_column</span>(<span class="ruby-identifier">colname</span>)
660
+ <span class="ruby-comment"># check arguments</span>
661
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Column name does not exist!&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
662
+
663
+ <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">colname</span>)
664
+ <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">colname</span>)
665
+ <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
666
+ <span class="ruby-keyword">end</span></pre>
667
+ </div><!-- del_column-source -->
668
+
669
+ </div>
670
+
671
+
672
+
673
+
674
+ </div><!-- del_column-method -->
675
+
676
+
677
+ <div id="method-i-del_row" class="method-detail ">
678
+
679
+ <div class="method-heading">
680
+ <span class="method-name">del_row</span><span
681
+ class="method-args">(rownum)</span>
682
+
683
+ <span class="method-click-advice">click to toggle source</span>
684
+
685
+ </div>
686
+
687
+
688
+ <div class="method-description">
689
+
690
+ <p>Delete a row from the <a href="Table.html">Table</a>. Raises ArgumentError
691
+ if the row number is not found</p>
692
+
693
+ <h4 id="method-i-del_row-label-Attributes">Attributes<span><a href="#method-i-del_row-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
694
+ <dl class="rdoc-list note-list"><dt><code>rownum</code>
695
+ <dd>
696
+ <p><code>FixNum</code> to hold the row number</p>
697
+ </dd></dl>
698
+
699
+ <h4 id="method-i-del_row-label-Examples">Examples<span><a href="#method-i-del_row-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
700
+
701
+ <pre>cities.del_row(3) # deletes row with index 3 (4th row)
702
+ cities.del_row(-1) # deletes last row (per Ruby convention)</pre>
703
+
704
+
705
+
706
+
707
+ <div class="method-source-code" id="del_row-source">
708
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 200</span>
709
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">del_row</span>(<span class="ruby-identifier">rownum</span>)
710
+ <span class="ruby-comment"># check arguments</span>
711
+ <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">rownum</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">length</span>
712
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Row number does not exist!&quot;</span>
713
+ <span class="ruby-keyword">end</span>
714
+ <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
715
+ <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>].<span class="ruby-identifier">delete_at</span>(<span class="ruby-identifier">rownum</span>)
716
+ <span class="ruby-keyword">end</span>
717
+ <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
718
+ <span class="ruby-keyword">end</span></pre>
719
+ </div><!-- del_row-source -->
720
+
721
+ </div>
722
+
723
+
724
+
725
+
726
+ </div><!-- del_row-method -->
727
+
728
+
729
+ <div id="method-i-each" class="method-detail ">
730
+
731
+ <div class="method-heading">
732
+ <span class="method-name">each</span><span
733
+ class="method-args">() { |nextrow| ... }</span>
734
+
735
+ <span class="method-click-advice">click to toggle source</span>
736
+
737
+ </div>
738
+
739
+
740
+ <div class="method-description">
741
+
742
+ <p>Defines an iterator for <code>Table</code> which produces rows of data
743
+ (headers omitted) for its calling block.</p>
744
+
745
+
746
+
747
+
748
+ <div class="method-source-code" id="each-source">
749
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 65</span>
750
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">each</span>
751
+ <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
752
+ <span class="ruby-identifier">nextrow</span> = []
753
+ <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
754
+ <span class="ruby-identifier">nextrow</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">index</span>].<span class="ruby-identifier">clone</span>
755
+ <span class="ruby-keyword">end</span>
756
+ <span class="ruby-keyword">yield</span> <span class="ruby-identifier">nextrow</span>
757
+ <span class="ruby-keyword">end</span>
758
+ <span class="ruby-keyword">end</span></pre>
759
+ </div><!-- each-source -->
760
+
761
+ </div>
762
+
763
+
764
+
765
+
766
+ </div><!-- each-method -->
767
+
768
+
769
+ <div id="method-i-empty-3F" class="method-detail ">
770
+
771
+ <div class="method-heading">
772
+ <span class="method-name">empty?</span><span
773
+ class="method-args">()</span>
774
+
775
+ <span class="method-click-advice">click to toggle source</span>
776
+
777
+ </div>
778
+
779
+
780
+ <div class="method-description">
781
+
782
+ <p>Return true if the <a href="Table.html">Table</a> is empty, false
783
+ otherwise.</p>
784
+
785
+
786
+
787
+
788
+ <div class="method-source-code" id="empty-3F-source">
789
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 100</span>
790
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">empty?</span>
791
+ <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
792
+ <span class="ruby-keyword">end</span></pre>
793
+ </div><!-- empty-3F-source -->
794
+
795
+ </div>
796
+
797
+
798
+
799
+
800
+ </div><!-- empty-3F-method -->
801
+
802
+
398
803
  <div id="method-i-get_columns" class="method-detail method-alias">
399
804
 
400
805
  <div class="method-heading">
@@ -455,7 +860,7 @@ table.</p>
455
860
 
456
861
  <div class="method-heading">
457
862
  <span class="method-name">intersect</span><span
458
- class="method-args">(table2, colname, col2name=nil)</span>
863
+ class="method-args">(table2, colname, col2name=colname)</span>
459
864
 
460
865
  <span class="method-click-advice">click to toggle source</span>
461
866
 
@@ -464,8 +869,10 @@ table.</p>
464
869
 
465
870
  <div class="method-description">
466
871
 
467
- <p>Return the intersection of columns from different tables, eliminating
468
- duplicates. Return nil if a column is not found.</p>
872
+ <p>Return an Array with the intersection of columns from different tables,
873
+ eliminating duplicates. Return nil if a column is not found.</p>
874
+
875
+ <h4 id="method-i-intersect-label-Attributes">Attributes<span><a href="#method-i-intersect-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
469
876
  <dl class="rdoc-list note-list"><dt><code>table2</code>
470
877
  <dd>
471
878
  <p><code>Table</code> to identify the secondary table in the intersection</p>
@@ -477,20 +884,21 @@ duplicates. Return nil if a column is not found.</p>
477
884
  <p>OPTIONAL <code>String</code> to identify the column in the second table to
478
885
  intersection</p>
479
886
  </dd></dl>
887
+
888
+ <h4 id="method-i-intersect-label-Examples">Examples<span><a href="#method-i-intersect-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
889
+
890
+ <pre>cities.intersect(capitals, &quot;City&quot;, &quot;Capital&quot;) # returns Array with all capitals that are also in the cities table</pre>
480
891
 
481
892
 
482
893
 
483
894
 
484
895
  <div class="method-source-code" id="intersect-source">
485
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 316</span>
486
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">intersect</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-keyword">nil</span>)
896
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 513</span>
897
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">intersect</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-identifier">colname</span>)
487
898
  <span class="ruby-comment"># check arguments</span>
488
899
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid table!&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Table</span>)
489
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
490
- <span class="ruby-keyword">if</span> <span class="ruby-identifier">col2name</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-comment"># Assume colname applies for both tables</span>
491
- <span class="ruby-identifier">col2name</span> = <span class="ruby-identifier">colname</span>
492
- <span class="ruby-keyword">end</span>
493
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col2name</span>)
900
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
901
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col2name</span>)
494
902
 
495
903
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">colname</span>) <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">col2name</span>)
496
904
  <span class="ruby-keyword">end</span></pre>
@@ -508,7 +916,7 @@ intersection</p>
508
916
 
509
917
  <div class="method-heading">
510
918
  <span class="method-name">join</span><span
511
- class="method-args">(table2, colname, col2name=nil)</span>
919
+ class="method-args">(table2, colname, col2name=colname)</span>
512
920
 
513
921
  <span class="method-click-advice">click to toggle source</span>
514
922
 
@@ -522,6 +930,8 @@ intersection</p>
522
930
  the common column once, under the column name of the first table (if
523
931
  different from the name of thee second). All columns from both tables are
524
932
  returned. Returns <code>nil</code> if the column is not found.</p>
933
+
934
+ <h4 id="method-i-join-label-Attributes">Attributes<span><a href="#method-i-join-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
525
935
  <dl class="rdoc-list note-list"><dt><code>table2</code>
526
936
  <dd>
527
937
  <p><code>Table</code> to identify the secondary table in the join</p>
@@ -533,28 +943,29 @@ returned. Returns <code>nil</code> if the column is not found.</p>
533
943
  <p>OPTIONAL <code>String</code> to identify the column in the second table to
534
944
  join on</p>
535
945
  </dd></dl>
946
+
947
+ <h4 id="method-i-join-label-Examples">Examples<span><a href="#method-i-join-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
948
+
949
+ <pre>cities.join(capitals, &quot;City&quot;, &quot;Capital&quot;) # returns a Table of cities that are also state capitals
950
+ capitals.join(cities, &quot;State&quot;) # returns a Table of capital cities with populations info from the cities table</pre>
536
951
 
537
952
 
538
953
 
539
954
 
540
955
  <div class="method-source-code" id="join-source">
541
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 237</span>
542
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">join</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-keyword">nil</span>)
956
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 425</span>
957
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">join</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-identifier">colname</span>)
543
958
  <span class="ruby-comment"># check arguments</span>
544
959
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid table!&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Table</span>)
545
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
546
- <span class="ruby-keyword">if</span> <span class="ruby-identifier">col2name</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-comment"># Assume colname applies for both tables</span>
547
- <span class="ruby-identifier">col2name</span> = <span class="ruby-identifier">colname</span>
548
- <span class="ruby-keyword">end</span>
960
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
961
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col2name</span>)
549
962
  <span class="ruby-identifier">t2_col_index</span> = <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">col2name</span>)
550
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">t2_col_index</span> <span class="ruby-comment"># is not nil</span>
551
-
552
963
 
553
964
  <span class="ruby-comment"># ensure no duplication of header values</span>
554
- <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">h</span><span class="ruby-operator">|</span>
555
- <span class="ruby-keyword">if</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">h</span>)
556
- <span class="ruby-identifier">update_header</span>(<span class="ruby-identifier">h</span>, <span class="ruby-string">&#39;_&#39;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">h</span> )
557
- <span class="ruby-keyword">if</span> <span class="ruby-identifier">h</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">colname</span>
965
+ <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">header</span><span class="ruby-operator">|</span>
966
+ <span class="ruby-keyword">if</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">header</span>)
967
+ <span class="ruby-identifier">update_header</span>(<span class="ruby-identifier">header</span>, <span class="ruby-string">&#39;_&#39;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">header</span> )
968
+ <span class="ruby-keyword">if</span> <span class="ruby-identifier">header</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">colname</span>
558
969
  <span class="ruby-identifier">colname</span> = <span class="ruby-string">&#39;_&#39;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">colname</span>
559
970
  <span class="ruby-keyword">end</span>
560
971
  <span class="ruby-keyword">end</span>
@@ -626,6 +1037,8 @@ join on</p>
626
1037
 
627
1038
  <p>Return a copy of a row from the table as an <code>Array</code>, given an
628
1039
  index (i.e. row number). Returns empty Array if the index is out of bounds.</p>
1040
+
1041
+ <h4 id="method-i-row-label-Attributes">Attributes<span><a href="#method-i-row-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
629
1042
  <dl class="rdoc-list note-list"><dt><code>index</code>
630
1043
  <dd>
631
1044
  <p><code>FixNum</code> indicating index of the row.</p>
@@ -635,7 +1048,7 @@ index (i.e. row number). Returns empty Array if the index is out of bounds.</p>
635
1048
 
636
1049
 
637
1050
  <div class="method-source-code" id="row-source">
638
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 66</span>
1051
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 94</span>
639
1052
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">row</span>(<span class="ruby-identifier">index</span>)
640
1053
  <span class="ruby-constant">Array</span>(<span class="ruby-identifier">get_row</span>(<span class="ruby-identifier">index</span>))
641
1054
  <span class="ruby-keyword">end</span></pre>
@@ -663,40 +1076,45 @@ index (i.e. row number). Returns empty Array if the index is out of bounds.</p>
663
1076
  <div class="method-description">
664
1077
 
665
1078
  <p>Select columns from the table, given one or more column names. Returns an
666
- instance of <code>Table</code> with the results. Returns nil if any column
667
- is not valid.</p>
1079
+ instance of <code>Table</code> with the results. Raises ArgumentError if
1080
+ any column is not valid.</p>
1081
+
1082
+ <h4 id="method-i-select-label-Attributes">Attributes<span><a href="#method-i-select-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
668
1083
  <dl class="rdoc-list note-list"><dt><code>columns</code>
669
1084
  <dd>
670
1085
  <p>Variable <code>String</code> arguments to identify the columns to select</p>
671
1086
  </dd></dl>
1087
+
1088
+ <h4 id="method-i-select-label-Examples">Examples<span><a href="#method-i-select-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1089
+
1090
+ <pre>cities.select(&quot;City&quot;, &quot;State&quot;) # returns a Table of &quot;City&quot; and &quot;State&quot; columns
1091
+ cities.select(cities.headers) # returns a new Table that is a duplicate of cities</pre>
672
1092
 
673
1093
 
674
1094
 
675
1095
 
676
1096
  <div class="method-source-code" id="select-source">
677
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 178</span>
1097
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 355</span>
678
1098
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">select</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">columns</span>)
679
1099
  <span class="ruby-comment"># check arguments</span>
1100
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name(s)&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">columns</span>
1101
+ <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">flatten!</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
680
1102
  <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span>
681
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">c</span>)
1103
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">c</span>)
682
1104
  <span class="ruby-keyword">end</span>
683
1105
 
684
1106
  <span class="ruby-identifier">result</span> = []
685
1107
  <span class="ruby-identifier">result_headers</span> = []
686
1108
  <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">result_headers</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">col</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span> }
687
1109
  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">result_headers</span>
688
- <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">length</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
1110
+ <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
689
1111
  <span class="ruby-identifier">this_row</span> = []
690
1112
  <span class="ruby-identifier">result_headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
691
- <span class="ruby-identifier">this_row</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">row</span>]
1113
+ <span class="ruby-identifier">this_row</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">index</span>]
692
1114
  <span class="ruby-keyword">end</span>
693
1115
  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">this_row</span>
694
1116
  <span class="ruby-keyword">end</span>
695
- <span class="ruby-keyword">unless</span> <span class="ruby-identifier">result_headers</span>.<span class="ruby-identifier">empty?</span>
696
- <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>)
697
- <span class="ruby-keyword">else</span>
698
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span>
699
- <span class="ruby-keyword">end</span>
1117
+ <span class="ruby-identifier">result_headers</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>() <span class="ruby-operator">:</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>)
700
1118
  <span class="ruby-keyword">end</span></pre>
701
1119
  </div><!-- select-source -->
702
1120
 
@@ -740,6 +1158,110 @@ is not valid.</p>
740
1158
  </div><!-- size-method -->
741
1159
 
742
1160
 
1161
+ <div id="method-i-sort" class="method-detail ">
1162
+
1163
+ <div class="method-heading">
1164
+ <span class="method-name">sort</span><span
1165
+ class="method-args">(column=nil, &block)</span>
1166
+
1167
+ <span class="method-click-advice">click to toggle source</span>
1168
+
1169
+ </div>
1170
+
1171
+
1172
+ <div class="method-description">
1173
+
1174
+ <p>Sort the table based on given column. Uses precedence as defined in the
1175
+ column. By default will sort by the value in the first column.</p>
1176
+
1177
+ <h4 id="method-i-sort-label-Attributes">Attributes<span><a href="#method-i-sort-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1178
+ <dl class="rdoc-list note-list"><dt><code>args</code>
1179
+ <dd>
1180
+ <p>OPTIONAL <code>String</code> to identify the column on which to sort</p>
1181
+ </dd></dl>
1182
+
1183
+ <h4 id="method-i-sort-label-Options">Options<span><a href="#method-i-sort-label-Options">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1184
+
1185
+ <pre class="ruby"><span class="ruby-identifier">datatype</span> =<span class="ruby-operator">&gt;</span> :<span class="ruby-constant">Fixnum</span>
1186
+ <span class="ruby-identifier">datatype</span> =<span class="ruby-operator">&gt;</span> :<span class="ruby-constant">Float</span>
1187
+ <span class="ruby-identifier">datatype</span> =<span class="ruby-operator">&gt;</span> :<span class="ruby-constant">Date</span>
1188
+ </pre>
1189
+
1190
+ <h4 id="method-i-sort-label-Examples">Examples<span><a href="#method-i-sort-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1191
+
1192
+ <pre class="ruby"><span class="ruby-identifier">cities</span>.<span class="ruby-identifier">sort</span>(<span class="ruby-string">&quot;State&quot;</span>) <span class="ruby-comment"># Re-orders the cities table based on State name</span>
1193
+ <span class="ruby-identifier">cities</span>.<span class="ruby-identifier">sort</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>,<span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">b</span><span class="ruby-operator">&lt;=&gt;</span><span class="ruby-identifier">a</span> } <span class="ruby-comment"># Reverse the order of the cities table</span>
1194
+ <span class="ruby-identifier">cities</span>.<span class="ruby-identifier">sort</span>(<span class="ruby-string">&quot;State&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>,<span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">b</span><span class="ruby-operator">&lt;=&gt;</span><span class="ruby-identifier">a</span> } <span class="ruby-comment"># Sort by State in reverse alpha order</span>
1195
+ </pre>
1196
+
1197
+
1198
+
1199
+
1200
+ <div class="method-source-code" id="sort-source">
1201
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 540</span>
1202
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">sort</span>(<span class="ruby-identifier">column</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
1203
+ <span class="ruby-identifier">col_index</span> = <span class="ruby-value">0</span>
1204
+ <span class="ruby-keyword">if</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">String</span>
1205
+ <span class="ruby-identifier">col_index</span> = <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">column</span>)
1206
+ <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Fixnum</span>
1207
+ <span class="ruby-identifier">col_index</span> = <span class="ruby-identifier">column</span>
1208
+ <span class="ruby-keyword">end</span>
1209
+ <span class="ruby-comment"># return empty Table if empty</span>
1210
+ <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">empty?</span>
1211
+ <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>()
1212
+ <span class="ruby-keyword">end</span>
1213
+
1214
+ <span class="ruby-identifier">neworder</span> = []
1215
+ <span class="ruby-keyword">self</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">neworder</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">OrderedRow</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">row</span>,<span class="ruby-identifier">col_index</span>) }
1216
+
1217
+ <span class="ruby-identifier">result</span> = [<span class="ruby-identifier">neworder</span>.<span class="ruby-identifier">shift</span>.<span class="ruby-identifier">data</span>] <span class="ruby-comment"># take off headers</span>
1218
+ <span class="ruby-identifier">block_given?</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">neworder</span>.<span class="ruby-identifier">sort!</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">neworder</span>.<span class="ruby-identifier">sort!</span>
1219
+ <span class="ruby-identifier">neworder</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">data</span> }
1220
+
1221
+ <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>)
1222
+ <span class="ruby-keyword">end</span></pre>
1223
+ </div><!-- sort-source -->
1224
+
1225
+ </div>
1226
+
1227
+
1228
+ <div class="aliases">
1229
+ Also aliased as: <a href="Table.html#method-i-sort-21">sort!</a>
1230
+ </div>
1231
+
1232
+
1233
+
1234
+ </div><!-- sort-method -->
1235
+
1236
+
1237
+ <div id="method-i-sort-21" class="method-detail method-alias">
1238
+
1239
+ <div class="method-heading">
1240
+ <span class="method-name">sort!</span><span
1241
+ class="method-args">(column=nil, &block)</span>
1242
+
1243
+ </div>
1244
+
1245
+
1246
+ <div class="method-description">
1247
+
1248
+
1249
+
1250
+
1251
+
1252
+
1253
+ </div>
1254
+
1255
+
1256
+
1257
+
1258
+ <div class="aliases">
1259
+ Alias for: <a href="Table.html#method-i-sort">sort</a>
1260
+ </div>
1261
+
1262
+ </div><!-- sort-21-method -->
1263
+
1264
+
743
1265
  <div id="method-i-sub" class="method-detail ">
744
1266
 
745
1267
  <div class="method-heading">
@@ -757,6 +1279,8 @@ is not valid.</p>
757
1279
  replacement string, update the table such that it substitutes the column
758
1280
  data with the replacement string. Returns <code>nil</code> if the column is
759
1281
  not found.</p>
1282
+
1283
+ <h4 id="method-i-sub-label-Attributes">Attributes<span><a href="#method-i-sub-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
760
1284
  <dl class="rdoc-list note-list"><dt><code>colname</code>
761
1285
  <dd>
762
1286
  <p><code>String</code> to identify the column to join on</p>
@@ -768,17 +1292,22 @@ not found.</p>
768
1292
  <p><code>String</code> to specify the replacement text for the given
769
1293
  <code>Regexp</code></p>
770
1294
  </dd></dl>
1295
+
1296
+ <h4 id="method-i-sub-label-Examples">Examples<span><a href="#method-i-sub-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1297
+
1298
+ <pre>cities.sub(&quot;Population&quot;, /(.*?),(.*?)/, &#39;\1\2&#39;) # eliminate commas
1299
+ capitals.sub(&quot;State&quot;, /NY/, &quot;New York&quot;) # replace acronym with full name</pre>
771
1300
 
772
1301
 
773
1302
 
774
1303
 
775
1304
  <div class="method-source-code" id="sub-source">
776
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 280</span>
1305
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 470</span>
777
1306
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">sub</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">re</span>, <span class="ruby-identifier">replace</span>)
778
1307
  <span class="ruby-comment"># check arguments</span>
779
1308
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;No regular expression to match against&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">re</span>
780
1309
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;No replacement string specified&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">replace</span>
781
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
1310
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
782
1311
 
783
1312
  <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">item</span><span class="ruby-operator">|</span>
784
1313
  <span class="ruby-identifier">item</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-identifier">re</span>, <span class="ruby-identifier">replace</span>)
@@ -841,21 +1370,27 @@ not found.</p>
841
1370
  <div class="method-description">
842
1371
 
843
1372
  <p>Count instances in a particular field/column and return a
844
- <code>Table</code> of the results. Returns <code>nil</code> if the column
845
- is not found.</p>
1373
+ <code>Table</code> of the results. Raises ArgumentError if the column is
1374
+ not found.</p>
1375
+
1376
+ <h4 id="method-i-tally-label-Attributes">Attributes<span><a href="#method-i-tally-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
846
1377
  <dl class="rdoc-list note-list"><dt><code>colname</code>
847
1378
  <dd>
848
1379
  <p><code>String</code> to identify the column to tally</p>
849
1380
  </dd></dl>
1381
+
1382
+ <h4 id="method-i-tally-label-Examples">Examples<span><a href="#method-i-tally-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1383
+
1384
+ <pre>cities.tally(&quot;State&quot;) # returns each State in the cities Table with number of occurences</pre>
850
1385
 
851
1386
 
852
1387
 
853
1388
 
854
1389
  <div class="method-source-code" id="tally-source">
855
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 163</span>
1390
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 334</span>
856
1391
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">tally</span>(<span class="ruby-identifier">colname</span>)
857
1392
  <span class="ruby-comment"># check arguments</span>
858
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
1393
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
859
1394
 
860
1395
  <span class="ruby-identifier">result</span> = {}
861
1396
  <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
@@ -886,7 +1421,10 @@ is not found.</p>
886
1421
 
887
1422
  <div class="method-description">
888
1423
 
889
- <p>Converts a <code>Table</code> object to an array of arrays (each row)</p>
1424
+ <p>Converts a <code>Table</code> object to an array of arrays (each row). The
1425
+ first entry are the table headers.</p>
1426
+
1427
+ <h4 id="method-i-to_a-label-Attributes">Attributes<span><a href="#method-i-to_a-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
890
1428
 
891
1429
  <p>none</p>
892
1430
 
@@ -894,14 +1432,14 @@ is not found.</p>
894
1432
 
895
1433
 
896
1434
  <div class="method-source-code" id="to_a-source">
897
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 92</span>
1435
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 237</span>
898
1436
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">to_a</span>
899
1437
  <span class="ruby-identifier">result</span> = [ <span class="ruby-constant">Array</span>(<span class="ruby-ivar">@headers</span>) ]
900
1438
 
901
- <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">length</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
1439
+ <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
902
1440
  <span class="ruby-identifier">items</span> = []
903
1441
  <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
904
- <span class="ruby-identifier">items</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">row</span>]
1442
+ <span class="ruby-identifier">items</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">index</span>]
905
1443
  <span class="ruby-keyword">end</span>
906
1444
  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">items</span>
907
1445
  <span class="ruby-keyword">end</span>
@@ -932,19 +1470,21 @@ is not found.</p>
932
1470
 
933
1471
  <p>Converts a <code>Table</code> object to a tab-delimited string.</p>
934
1472
 
1473
+ <h4 id="method-i-to_s-label-Attributes">Attributes<span><a href="#method-i-to_s-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1474
+
935
1475
  <p>none</p>
936
1476
 
937
1477
 
938
1478
 
939
1479
 
940
1480
  <div class="method-source-code" id="to_s-source">
941
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 73</span>
1481
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 216</span>
942
1482
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">to_s</span>
943
1483
  <span class="ruby-identifier">result</span> = <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;\t&quot;</span>) <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;\n&quot;</span>
944
1484
 
945
- <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">length</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
1485
+ <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
946
1486
  <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
947
- <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">row</span>].<span class="ruby-identifier">to_s</span>
1487
+ <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">index</span>].<span class="ruby-identifier">to_s</span>
948
1488
  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">col</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">last</span>
949
1489
  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;\t&quot;</span>
950
1490
  <span class="ruby-keyword">else</span>
@@ -977,10 +1517,12 @@ is not found.</p>
977
1517
 
978
1518
  <div class="method-description">
979
1519
 
980
- <p>Counts the number of instances of a particular string, given a column name,
981
- and returns an integer &gt;= 0. Returns <code>nil</code> if the column is
982
- not found. If no parameters are given, returns the number of rows in the
983
- table.</p>
1520
+ <p>Returns counts of the most frequent values found in a given column in the
1521
+ form of a <a href="Table.html">Table</a>. Raises ArgumentError if the
1522
+ column is not found. If no limit is given to the number of values, only
1523
+ the top value will be returned.</p>
1524
+
1525
+ <h4 id="method-i-top-label-Attributes">Attributes<span><a href="#method-i-top-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
984
1526
  <dl class="rdoc-list note-list"><dt><code>colname</code>
985
1527
  <dd>
986
1528
  <p><code>String</code> to identify the column to count</p>
@@ -988,15 +1530,20 @@ table.</p>
988
1530
  <dd>
989
1531
  <p>OPTIONAL <code>String</code> number of values to return</p>
990
1532
  </dd></dl>
1533
+
1534
+ <h4 id="method-i-top-label-Examples">Examples<span><a href="#method-i-top-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1535
+
1536
+ <pre>cities.top(&quot;State&quot;) # returns a Table with the most frequent state in the cities Table
1537
+ cities.top(&quot;State&quot;, 10) # returns a Table with the 10 most frequent states in the cities Table</pre>
991
1538
 
992
1539
 
993
1540
 
994
1541
 
995
1542
  <div class="method-source-code" id="top-source">
996
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 140</span>
1543
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 300</span>
997
1544
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">top</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">num</span>=<span class="ruby-value">1</span>)
998
1545
  <span class="ruby-identifier">freq</span> = <span class="ruby-identifier">tally</span>(<span class="ruby-identifier">colname</span>).<span class="ruby-identifier">to_a</span>[<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>].<span class="ruby-identifier">sort_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">v</span> }.<span class="ruby-identifier">reverse</span>
999
- <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">freq</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">num</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>].<span class="ruby-identifier">unshift</span>([<span class="ruby-string">&quot;State&quot;</span>,<span class="ruby-string">&quot;Count&quot;</span>]))
1546
+ <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">freq</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">num</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>].<span class="ruby-identifier">unshift</span>([<span class="ruby-identifier">colname</span>,<span class="ruby-string">&quot;Count&quot;</span>]))
1000
1547
  <span class="ruby-keyword">end</span></pre>
1001
1548
  </div><!-- top-source -->
1002
1549
 
@@ -1012,7 +1559,7 @@ table.</p>
1012
1559
 
1013
1560
  <div class="method-heading">
1014
1561
  <span class="method-name">union</span><span
1015
- class="method-args">(table2, colname, col2name=nil)</span>
1562
+ class="method-args">(table2, colname, col2name=colname)</span>
1016
1563
 
1017
1564
  <span class="method-click-advice">click to toggle source</span>
1018
1565
 
@@ -1021,8 +1568,10 @@ table.</p>
1021
1568
 
1022
1569
  <div class="method-description">
1023
1570
 
1024
- <p>Return the union of columns from different tables, eliminating duplicates.
1025
- Return nil if a column is not found.</p>
1571
+ <p>Return Array with the union of elements columns in the given tables,
1572
+ eliminating duplicates. Raises an ArgumentError if a column is not found.</p>
1573
+
1574
+ <h4 id="method-i-union-label-Attributes">Attributes<span><a href="#method-i-union-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1026
1575
  <dl class="rdoc-list note-list"><dt><code>table2</code>
1027
1576
  <dd>
1028
1577
  <p><code>Table</code> to identify the secondary table in the union</p>
@@ -1034,20 +1583,21 @@ Return nil if a column is not found.</p>
1034
1583
  <p>OPTIONAL <code>String</code> to identify the column in the second table to
1035
1584
  union</p>
1036
1585
  </dd></dl>
1586
+
1587
+ <h4 id="method-i-union-label-Examples">Examples<span><a href="#method-i-union-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1588
+
1589
+ <pre>cities.union(capitals, &quot;City&quot;, &quot;Capital&quot;) # returns Array with all cities in both tables</pre>
1037
1590
 
1038
1591
 
1039
1592
 
1040
1593
 
1041
1594
  <div class="method-source-code" id="union-source">
1042
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 298</span>
1043
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">union</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-keyword">nil</span>)
1595
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 493</span>
1596
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">union</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-identifier">colname</span>)
1044
1597
  <span class="ruby-comment"># check arguments</span>
1045
1598
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid table!&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Table</span>)
1046
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
1047
- <span class="ruby-keyword">if</span> <span class="ruby-identifier">col2name</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-comment"># Assume colname applies for both tables</span>
1048
- <span class="ruby-identifier">col2name</span> = <span class="ruby-identifier">colname</span>
1049
- <span class="ruby-keyword">end</span>
1050
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col2name</span>)
1599
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
1600
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col2name</span>)
1051
1601
 
1052
1602
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">colname</span>) <span class="ruby-operator">|</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">col2name</span>)
1053
1603
  <span class="ruby-keyword">end</span></pre>
@@ -1076,8 +1626,10 @@ union</p>
1076
1626
 
1077
1627
  <p>Given a particular condition for a given column field/column, return a
1078
1628
  subtable that matches the condition. If no condition is given, a new
1079
- <code>Table</code> is returned with all records. Returns <code>nil</code>
1080
- if the condition is not met or the column is not found.</p>
1629
+ <code>Table</code> is returned with all records. Returns an empty table if
1630
+ the condition is not met or the column is not found.</p>
1631
+
1632
+ <h4 id="method-i-where-label-Attributes">Attributes<span><a href="#method-i-where-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1081
1633
  <dl class="rdoc-list note-list"><dt><code>colname</code>
1082
1634
  <dd>
1083
1635
  <p><code>String</code> to identify the column to tally</p>
@@ -1085,26 +1637,32 @@ if the condition is not met or the column is not found.</p>
1085
1637
  <dd>
1086
1638
  <p>OPTIONAL <code>String</code> containing a ruby condition to evaluate</p>
1087
1639
  </dd></dl>
1640
+
1641
+ <h4 id="method-i-where-label-Examples">Examples<span><a href="#method-i-where-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1642
+
1643
+ <pre>cities.where(&quot;State&quot;, &quot;==&#39;NY&#39;&quot;) # returns a Table of cities in New York state
1644
+ cities.where(&quot;State&quot;, &quot;=~ /New.*/&quot;) # returns a Table of cities in states that start with &quot;New&quot;
1645
+ cities.where(&quot;Population&quot;, &quot;.to_i &gt; 1000000&quot;) # returns a Table of cities with population over 1 million</pre>
1088
1646
 
1089
1647
 
1090
1648
 
1091
1649
 
1092
1650
  <div class="method-source-code" id="where-source">
1093
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 211</span>
1651
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 393</span>
1094
1652
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">where</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">condition</span>=<span class="ruby-keyword">nil</span>)
1095
1653
  <span class="ruby-comment"># check arguments</span>
1096
- <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
1654
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid Column Name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">colname</span>)
1097
1655
 
1098
1656
  <span class="ruby-identifier">result</span> = []
1099
1657
  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@headers</span>
1100
- <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
1658
+ <span class="ruby-keyword">self</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
1101
1659
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">condition</span>
1102
- <span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;&#39;#{@table[colname][index]}&#39; #{condition}&quot;</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">get_row</span>(<span class="ruby-identifier">index</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
1660
+ <span class="ruby-identifier">eval</span>(<span class="ruby-string">%q[&quot;#{row[headers.index(colname)]}&quot;]</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;#{condition}&quot;</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
1103
1661
  <span class="ruby-keyword">else</span>
1104
- <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">get_row</span>(<span class="ruby-identifier">index</span>)
1662
+ <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span>
1105
1663
  <span class="ruby-keyword">end</span>
1106
1664
  <span class="ruby-keyword">end</span>
1107
- <span class="ruby-identifier">result</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
1665
+ <span class="ruby-identifier">result</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>) <span class="ruby-operator">:</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>()
1108
1666
  <span class="ruby-keyword">end</span></pre>
1109
1667
  </div><!-- where-source -->
1110
1668
 
@@ -1135,6 +1693,8 @@ if the condition is not met or the column is not found.</p>
1135
1693
 
1136
1694
  <p>Write a representation of the <code>Table</code> object to a file (tab
1137
1695
  delimited).</p>
1696
+
1697
+ <h4 id="method-i-write_file-label-Attributes">Attributes<span><a href="#method-i-write_file-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
1138
1698
  <dl class="rdoc-list note-list"><dt><code>filename</code>
1139
1699
  <dd>
1140
1700
  <p><code>String</code> to identify the name of the file to write</p>
@@ -1144,7 +1704,7 @@ delimited).</p>
1144
1704
 
1145
1705
 
1146
1706
  <div class="method-source-code" id="write_file-source">
1147
- <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 333</span>
1707
+ <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 568</span>
1148
1708
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">write_file</span>(<span class="ruby-identifier">filename</span>)
1149
1709
  <span class="ruby-identifier">file</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">filename</span>, <span class="ruby-string">&quot;w&quot;</span>)
1150
1710
  <span class="ruby-identifier">file</span>.<span class="ruby-identifier">print</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">to_s</span>