|
1 | 1 | # |
2 | 2 | # Loading Tabular Data |
3 | | -# after Daniel Shiffman, by Martin Prout. |
4 | | -# |
| 3 | +# after Daniel Shiffman, by Martin Prout. |
| 4 | +# |
5 | 5 | # This example demonstrates how to use CSV |
6 | | -# to retrieve data from a CSV file and make objects |
| 6 | +# to retrieve data from a CSV file and make objects |
7 | 7 | # from that data. |
8 | 8 | # |
9 | 9 | # Here is what the CSV looks like: |
@@ -32,54 +32,36 @@ def draw |
32 | 32 | b.display |
33 | 33 | b.rollover(mouse_x, mouse_y) |
34 | 34 | end |
35 | | - |
36 | 35 | text_align(LEFT) |
37 | 36 | fill(0) |
38 | 37 | text('Click to add bubbles.', 10, height - 10) |
39 | 38 | end |
40 | 39 |
|
41 | 40 | def load_data |
42 | 41 | # Load CSV file into an Array of Hash objects |
43 | | - # :headers option indicates the file has a header row |
44 | | - @data = CSV.read('data/data.csv', :headers => true).map{|row| row.to_hash} |
45 | | - |
46 | | - # The size of the array of Bubble objects is determined by the total number of 'rows' in the CSV |
47 | | - @bubbles = [] |
48 | | - |
49 | | - data.each do |row| |
50 | | - # You access the values via their column name (set by using headers option above) |
| 42 | + # headers: option indicates the file has a header row |
| 43 | + @bubbles = [] |
| 44 | + CSV.foreach('data/data.csv', headers: true) do |row| |
51 | 45 | x = row['x'].to_f |
52 | 46 | y = row['y'].to_f |
53 | 47 | d = row['diameter'].to_f |
54 | | - n = row['name'] |
| 48 | + n = row['name'] |
55 | 49 | # Make a Bubble object out of the data read |
56 | 50 | bubbles << Bubble.new(x, y, d, n) |
57 | | - end |
58 | | - |
| 51 | + end |
59 | 52 | end |
60 | 53 |
|
61 | 54 | def mouse_pressed |
62 | | - # Create a new 'row' hash |
63 | | - row = {'x' => mouse_x.to_s, 'y' => mouse_y.to_s, 'diameter' => random(40, 80).to_s, 'name' => 'Blah'} |
64 | | - # add the row to the existing data array |
65 | | - data << row |
66 | | - |
67 | | - # If the table has more than 10 rows |
68 | | - if (data.size > 10) |
69 | | - # Delete the oldest row |
70 | | - data.shift |
71 | | - end |
72 | | - # read column names from data, and generate csv 'string' that can be written to file |
73 | | - column_names = data.first.keys |
74 | | - s = CSV.generate do |csv| |
75 | | - csv << column_names |
76 | | - data.each do |row| |
77 | | - csv << row.values |
| 55 | + bubbles << Bubble.new(mouse_x, mouse_y, rand(40..80), 'blah') |
| 56 | + # If there are more than 10 bubbles delete the oldest bubble |
| 57 | + bubbles.shift if bubbles.size > 10 |
| 58 | + # Writing the csv data back to the same file, (also specify UTF-8 format) |
| 59 | + CSV.open('data/data.csv', 'w:UTF-8') do |csv| |
| 60 | + csv << %w(x y diameter name) # create csv headers |
| 61 | + bubbles.each do |bubble| |
| 62 | + csv << bubble.to_a # write back bubble data |
78 | 63 | end |
79 | 64 | end |
80 | | - # Writing the csv data back to the same file, (also specify UTF-8 format) |
81 | | - File.open('data/data.csv', 'w:UTF-8') { |file| file.write(s)} |
82 | 65 | # And reloading it |
83 | 66 | load_data |
84 | 67 | end |
85 | | - |
0 commit comments