Skip to content

Commit 1790c2f

Browse files
author
monkstone
committed
create runnable module
1 parent 74b1d75 commit 1790c2f

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

samples/processing_app/topics/advanced_data/load_save_table.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ def setup
2828
def draw
2929
background(255)
3030
# Display all bubbles
31-
bubbles.each do |b|
32-
b.display
33-
b.rollover(mouse_x, mouse_y)
34-
end
31+
bubbles.run
3532
text_align(LEFT)
3633
fill(0)
3734
text('Click to add bubbles.', 10, height - 10)
@@ -40,7 +37,7 @@ def draw
4037
def load_data
4138
# Load CSV file into an Array of Hash objects
4239
# headers: option indicates the file has a header row
43-
@bubbles = []
40+
@bubbles = BubbleData.new
4441
CSV.foreach('data/data.csv', headers: true) do |row|
4542
x = row['x'].to_f
4643
y = row['y'].to_f
@@ -65,3 +62,22 @@ def mouse_pressed
6562
# And reloading it
6663
load_data
6764
end
65+
66+
# A run module
67+
module Runnable
68+
def run
69+
each(&:display)
70+
each { |item| item.rollover(mouse_x, mouse_y) }
71+
end
72+
end
73+
74+
# Enumerable class holds bubble data
75+
class BubbleData
76+
extend Forwardable
77+
def_delegators(:@bubbles, :each, :<<, :size, :shift)
78+
include Enumerable, Runnable
79+
80+
def initialize
81+
@bubbles = []
82+
end
83+
end

0 commit comments

Comments
 (0)