Skip to content

Commit abfebc8

Browse files
committed
updating chapter 18 examples
1 parent e82ae4f commit abfebc8

9 files changed

+55
-59
lines changed

chapter_18/01_user_input.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
#
44
def setup
55
size 300, 200
6-
@typing = ""
7-
@saved = ""
8-
textFont createFont "Arial", 16, true # set the font for text
6+
@typing, @saved = "", ""
7+
text_font create_font "Arial", 16, true # set the font for text
98
end
109

1110
def draw
@@ -28,6 +27,6 @@ def keyPressed
2827
# Otherwise, concatenate the String
2928
# Each character typed by the user is added to the
3029
# end of the String variable.
31-
@typing = @typing + key
30+
@typing = @typing + key if key.is_a? String
3231
end
3332
end

chapter_18/02_graphing_comma_separated_numbers_from_a_text_file.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
def setup
55
size 200, 200
66
# The text from the file is loaded into an array.
7-
stuff = load_strings("data-1.txt")
7+
stuff = File.readlines("#{sketch_path}/data/data-1.txt")
88

99
# This array has one element because the file only has one line.
1010
# Convert String into an array of integers using ',' as a delimiter
11-
@data = int(split(stuff.first, ","))
11+
@data = stuff.first.split(',')
12+
@data.map! {|num| num.to_i }
1213
end
1314

1415
def draw

chapter_18/03_creating_object_from_a_text_file.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ def setup
66
smooth
77

88
# Load text file as an array of Strings
9-
data = load_strings("data-2.txt")
9+
data = File.readlines("#{sketch_path}/data/data-2.txt")
1010

1111
# The size of the array of Bubble objects is determined by the
1212
# total number of lines in the text file.
1313
@bubbles = []
1414
data.each do |datum|
1515
# Each line is split into an array of floating point numbers.
16-
values = float(split(datum, "," ))
16+
values = datum.split(',').map {|d| d.to_f }
1717
# The values in the array are passed into the Bubble class constructor.
1818
@bubbles << Bubble.new(*values)
1919
end
@@ -35,10 +35,8 @@ class Bubble
3535
# The constructor initializes color and size
3636
# Location is filled randomly
3737
def initialize(r, g, diameter)
38-
@x = $app.random($app.width)
39-
@y = $app.height
40-
@r = r
41-
@g = g
38+
@x, @y = $app.random($app.width), $app.height
39+
@r, @g = r, g
4240
@diameter = diameter
4341
end
4442

@@ -53,8 +51,6 @@ def display
5351
def drift
5452
@y += $app.random(-3, -0.1)
5553
@x += $app.random(-1, 1)
56-
if @y < -@diameter * 2
57-
@y = $app.height + @diameter * 2
58-
end
54+
@y = $app.height + @diameter * 2 if @y < -@diameter * 2
5955
end
6056
end

chapter_18/04_loading_and_saving_data_to_text_file.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
def setup
55
size 200, 200
66
smooth
7+
@file_path = "#{sketch_path}/data/data-2.txt"
78

89
# Load text file as an array of Strings
9-
data = load_strings("data-2.txt")
10+
data = File.readlines(@file_path)
1011

1112
# The size of the array of Bubble objects is determined by the
1213
# total number of lines in the text file.
1314
@bubbles = []
1415
data.each do |datum|
1516
# Each line is split into an array of floating point numbers.
16-
values = float(split(datum, ","))
17+
values = datum.split(',').map {|num| num.to_f }
1718
# The values in the array are passed into the Bubble class constructor.
1819
@bubbles << Bubble.new(*values)
1920
end
@@ -48,7 +49,7 @@ def save_data
4849

4950
# Save to File
5051
# The same file is overwritten by adding the data folder path to saveStrings().
51-
save_strings("data/data-2.txt", data)
52+
File.open(@file_path, 'w') {|file| file.write(data.join("\n")) }
5253
end
5354

5455
#

chapter_18/05_parsing_yahoos_xml_weather_feed_manually.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
def setup
55
size 200, 200
6-
textFont createFont("Georgia",16, true)
6+
text_font create_font("Georgia",16, true)
77

88
# Make a WeatherGrabber object
99
@counter = 0
@@ -73,7 +73,7 @@ def request_weather
7373

7474
# Searching for temperature
7575
lookfor = "temp=\""
76-
@temperature = $app.int(give_me_text_between(xml, lookfor, endmarker))
76+
@temperature = give_me_text_between(xml, lookfor, endmarker).to_i
7777
end
7878

7979
# A function that returns a substring between two substrings

chapter_18/06_analyzing_king_lear.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def setup
55
size 200, 200
66

77
# Load the font
8-
textFont loadFont("Georgia-Bold-16.vlw")
8+
text_font load_font("Georgia-Bold-16.vlw")
99

1010
# Where are we in the text (start later b/c Project Gutenberg has
1111
# licensing info at beginning)
@@ -20,12 +20,12 @@ def setup
2020

2121
# All the lines in King Lear are first joined as one big String and
2222
# then split up into an array of individual words.
23-
# Note the use of splitTokens() since we are using spaces and
23+
# Note the use of split() since we are using spaces and
2424
# punctuation marks all as delimiters.
25-
delimiters = " ,.?!;:[]"
26-
@kinglear = split_tokens(everything, delimiters)
25+
delimiters = /[ ,.?!;:\[\]]/
26+
@kinglear = everything.split(delimiters)
2727

28-
frameRate 5
28+
frame_rate 5
2929
end
3030

3131
def draw

chapter_18/07_loading_a_url_with_simpleml.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def setup
1212

1313
# Create and make an asynchronous request using
1414
# the Request object from the library
15-
@htmlRequest = HTMLRequest.new(self, "http://www.yahoo.com")
16-
@htmlRequest.makeRequest
15+
@request = HTMLRequest.new(self, "http://www.yahoo.com")
16+
@request.makeRequest
1717
@timer = Timer.new(5000)
1818
@timer.start
1919
background 0
@@ -25,7 +25,7 @@ def draw
2525
# A request is made every 5s.
2626
# The data is not received here, however, this is only the request.
2727
if @timer.finished?
28-
@htmlRequest.makeRequest
28+
@request.make_request
2929
# XXX: was println("Making request!");
3030
puts "Making request!"
3131
@timer.start
@@ -34,10 +34,10 @@ def draw
3434
# When a request is finished the data the available
3535
# flag is set to true - and we get a chance to read
3636
# the data returned by the request
37-
if @htmlRequest.available?
38-
@html = @htmlRequest.readRawSource # Read the raw data
39-
@back = 255 # Reset background
40-
puts "Request completed!" # Print message
37+
if @request.available?
38+
@html = @request.read_raw_source # Read the raw data
39+
@back = 255 # Reset background
40+
puts "Request completed!" # Print message
4141
end
4242

4343
# Draw some lines with colors based on characters from data retrieved
@@ -72,23 +72,23 @@ def draw
7272
# Timer Class from Chapter 10
7373
#
7474
class Timer
75-
def initialize(tempTotalTime)
76-
@totalTime = tempTotalTime
77-
@running = false
75+
def initialize(total_time)
76+
@total_time = total_time
77+
@running = false
7878
end
7979

8080
def start
81-
@running = true
82-
@savedTime = $app.millis
81+
@running = true
82+
@saved_time = $app.millis
8383
end
8484

8585
def finished?
86-
passedTime = $app.millis - @savedTime
87-
if @running && (passedTime > @totalTime)
86+
finished = $app.millis - @saved_time > @total_time
87+
if @running && finished
8888
@running = false
89-
return true;
89+
return true
9090
else
91-
return false;
91+
return false
9292
end
9393
end
9494
end

chapter_18/08_loading_xml_with_simpleml.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ def setup
1010
# Creating and starting the request
1111
# An array of XML elements can be retrieved using getElementArray.
1212
# This only works for elements with the same name that appear multiple times in the XML document.
13-
@xmlRequest = XMLRequest.new(self, "http://rss.news.yahoo.com/rss/topstories")
14-
@xmlRequest.makeRequest
13+
@request = XMLRequest.new(self, "http://rss.news.yahoo.com/rss/topstories")
14+
@request.makeRequest
1515
end
1616

1717
def draw
1818
# When a request is finished the data the available
1919
# flag is set to true - and we get a chance to read
2020
# the data returned by the request
21-
if @xmlRequest.available?
21+
if @request.available?
2222
# Retrieving an array of all XML elements inside " title* " tags
23-
headlines = @xmlRequest.getElementArray("title")
24-
headlines.each do |headline|
25-
puts headline # XXX: was println(headlines[i]);
26-
end
27-
noLoop # Nothing to see here
23+
headlines = @request.get_element_array("title")
24+
# XXX: was println(headlines[i]);
25+
headlines.each {|line| puts line }
26+
no_loop # Nothing to see here
2827
end
2928
end
3029

chapter_18/data/data-2.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
12, 39, 72
2-
0.0, 21.0, 69.0
3-
37, 1, 69
4-
1, 46, 66
5-
23, 22, 70
6-
8, 7, 72
7-
38.0, 25.0, 68.0
8-
14, 139, 72
9-
42, 124, 71
10-
33, 54, 66
1+
12.0, 39.0, 72.0
2+
18.0, 53.0, 69.0
3+
51.0, 24.0, 72.0
4+
0.0, 23.0, 72.0
5+
0.0, 25.0, 72.0
6+
8.0, 7.0, 72.0
7+
0.0, 0.0, 72.0
8+
14.0, 139.0, 72.0
9+
27.0, 106.0, 71.0
10+
103.0, 7.0, 72.0

0 commit comments

Comments
 (0)