Skip to content

Commit 162867f

Browse files
author
monkstone
committed
modified: samples/processing_app/basics/data/characters_strings.rb
modified: samples/processing_app/basics/data/datatype_conversion.rb modified: samples/processing_app/basics/data/integers_floats.rb modified: samples/processing_app/basics/data/true_false.rb
1 parent e2ed81e commit 162867f

4 files changed

Lines changed: 36 additions & 79 deletions

File tree

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,30 @@
1-
#
2-
# Characters Strings.
3-
#
4-
# The character datatype, abbreviated as char, stores letters and
5-
# symbols in the Unicode format, a coding system developed to support
6-
# a variety of world languages. Characters are distinguished from other
7-
# symbols by putting them between single quotes ('P').<br />
8-
# <br />
9-
# A string is a sequence of characters. A string is noted by surrounding
10-
# a group of letters with double quotes ("Processing").
11-
# Chars and strings are most often used with the keyboard methods,
12-
# to display text to the screen, and to load images or files.<br />
13-
# <br />
14-
# The String datatype must be capitalized because it is a complex datatype.
15-
# A String is actually a class with its own methods, some of which are
16-
# featured below.
17-
#
18-
19-
attr_reader :letter, :words
1+
attr_reader :letter, :words, :fkey, :fstring
202

213
def setup
224
size 640, 360
23-
@words = "Begin..."
5+
@words = 'Begin...'
246
# Create the font
25-
text_font create_font("Georgia", 36)
7+
text_font create_font('Georgia', 36)
8+
@fkey = 'Current key: %s'
9+
@fstring = 'The String is %s characters long'
2610
end
2711

2812
def draw
2913
background(0) # Set background to black
30-
3114
# Draw the letter to the center of the screen
3215
text_size(14)
33-
text("Click on the program, then type to add to the String", 50, 50)
34-
text("Current key: #{letter}", 50, 70)
35-
text("The String is #{words.length()} characters long", 50, 90)
36-
16+
text('Click on the program, then type to add to the String', 50, 50)
17+
text(format(fkey, letter), 50, 70)
18+
text(format(fstring, words.length), 50, 90)
3719
text_size(36)
3820
text(words, 50, 120, 540, 300)
3921
end
4022

4123
def key_pressed
42-
# The variable "key" always contains the value
24+
# The variable 'key' always contains the value
4325
# of the most recent key pressed.
44-
if (('A'..'z').include?(key) || key == ' ')
45-
@letter = key
46-
@words = words + key
47-
puts(key)
48-
end
26+
return unless ('A'..'z').include?(key) || key == ' '
27+
@letter = key
28+
@words = words + key
29+
puts(key)
4930
end
50-

samples/processing_app/basics/data/datatype_conversion.rb

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,23 @@
66
# "to_f" (to Float, which would be "double" in Processing, not "float")
77
# "to_a" (to Array, i.e. from a Range or Hash)
88

9-
109
def setup
1110
size 200, 200
1211
# Ruby has no primitive datatypes, everything is an object!
13-
# See:
12+
fstring = "%s \t... is a %s object\n"
1413
[1, 2.0, 'a', "B", nil, false].each do |element|
15-
puts
16-
puts "#{element.inspect} ... is a #{element.class.name} object"
14+
puts format(fstring, element.inspect.to_s, element.class.name)
1715
end
18-
1916
c = 'A' # String (!) as there is no char datatype in Ruby.
2017
# Single quotes are parsed without substitutions (i.e. "It is #{Time.now}.")
21-
22-
f = c.ord.to_f # Sets f = 65.0
23-
# was "[c].to_f" in Ruby 1.8
24-
18+
f = c.ord.to_f # Sets f = 65.0
2519
i = (f * 1.4).to_i # Sets i to 91
26-
27-
b = (c.ord.to_i / 2) # Integer or FixNum as there is no byte in Ruby
28-
20+
b = (c.ord / 2) # Integer or FixNum as there is no equivalent byte in Ruby
2921
background 51
3022
no_stroke
31-
3223
rect f, 0, 40, 66
33-
3424
fill 204
3525
rect i, 67, 40, 66
36-
3726
fill 255
3827
rect b, 134, 40, 66
3928
end
40-
41-
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
# Integers and floats are two different kinds of numerical data.
2-
# An integer (more commonly called an int) is a number without
3-
# a decimal point. A float is a floating-point number, which means
4-
# it is a number that has a decimal place. Floats are used when
5-
# more precision is needed.
6-
1+
# ruby integers are objects of class Fixnum or Bignum. The Fixnum and Bignum
2+
# classes represent integers of differing sizes. Both classes descend from
3+
# Integer (and therefore Numeric). The floating-point numbers are objects of
4+
# class Float. Floats are used when more precision is needed. In java int and
5+
# float are primitives, so you can't send them messages unlike ruby.
76

87
def setup
9-
size 200, 200
8+
size 200, 200
109
stroke 255
1110
frame_rate 30
12-
1311
@a = 0 # Create an instance variable "a" of class Integer
1412
@b = 0.0 # Create an instance variable "b" of class Float (because of "0.0")
1513
end
1614

1715
def draw
1816
background 51
19-
2017
@a += 1
2118
@b += 0.2
22-
23-
line @a, 0, @a, height/2
24-
line @b, height/2, @b, height
25-
19+
line @a, 0, @a, height / 2
20+
line @b, height / 2, @b, height
2621
@a = 0 if @a > width
2722
@b = 0 if @b > width
2823
end
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Boolean data is one bit of information. True or false.
2-
# It is common to use Booleans with control statements to
1+
# Boolean data is one bit of information. True or false.
2+
# It is common to use Booleans with control statements to
33
# determine the flow of a program. In this example, when the
44
# boolean value "x" is true, vertical black lines are drawn and when
55
# the boolean value "x" is false, horizontal gray lines are drawn.
@@ -8,24 +8,19 @@
88
# that will fail an "if" test. Absolutely everything else passes "if".
99

1010
def setup
11-
size 200, 200
11+
size(640, 360)
1212
background 0
1313
stroke 0
14-
15-
(1..width).step(2) do |i|
16-
17-
x = i < (width/2) # Evaluates to true or false, depending on i
18-
14+
middle = width / 2
15+
d = 20
16+
(d..width).step(d) do |i|
17+
x = i < middle # Evaluates to true or false, depending on i
1918
if x
2019
stroke 255
21-
line i, 1, i, height-1
20+
line i, 1, i, height - 1
2221
end
23-
24-
if !x
25-
stroke 126
26-
line width/2, i, width-2, i
27-
end
28-
22+
next if x # favour guard clause in ruby to a wrapped loop
23+
stroke 126
24+
line middle, i - middle + d, width - d, i - middle + d
2925
end
3026
end
31-

0 commit comments

Comments
 (0)